Files
javar/NewClass.java
2025-09-24 01:22:00 +09:00

38 lines
1.6 KiB
Java
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import java.util.*;
/*------------- Пример 3. Файл NewClass.java -------------*/
/*------------- 3.1. Объявление нового типа -------------*/
public class NewClass // первичный класс
{
static class MyType // объявляется класс
{ public int myData=5; // переменная-элемент класса
public void myMethod() // метод класса
{ // печать в методе
System.out.print("myMethod!");
System.out.println(" myData="+myData);
}
MyType() // конструктор без параметров
{ // печать в конструкторе
System.out.println("Constructor without parameters");
}
MyType(int v) // конструктор с одним параметром
{ // печать в конструкторе
System.out.print("Constructor with one parameter");
System.out.println(" Setting myData="+v);
myData=v;
}
}
/*------------- 3.2. Реализация объектов и действия с ними -------------*/
public static void main(String args[])
{ // объект obj1 - реализация класса MyType
MyType obj1=new MyType();
obj1.myMethod(); // использование метода
// доступ к открытой переменной
System.out.println("---------obj1.myData="+obj1.myData);
// объект obj2 - реализация класса MyType
MyType obj2=new MyType(100);
// доступ к открытой переменной
System.out.println("---------obj2.myData="+obj2.myData);
}
} /*----------------------------------------------------------------*/