일요일, 12월 22
Shadow

#003 Object Detail

Point originOne = new Point(23, 94);
Rectangle rectOne = new Rectangle(originOne, 100, 200);
Rectangle rectTwo = new Rectangle(50, 100);
first line creates an object from the Point class and the second and third lines each create an object from the Rectangle class.
Declaration: The code set in bold are all variable declarations that associate a variable name with an object type.
Instantiation: The new keyword is a Java operator that creates the object. As discussed below, this is also known as instantiating a class.
Initialization: The new operator is followed by a call to a constructor. For example, Point(23, 94) is a call to Point’s only constructor. The constructor initializes the new object.
The declared type matches the class of the object:
MyClass myObject = new MyClass();

The declared type is a parent class of the object’s class:
MyParent myObject = new MyClass();

The declared type is an interface which the object’s class implements:
MyInterface myObject = new MyClass();

MyClass myObject;
The new operator instantiates a class by allocating memory for a new object

Referencing an Object’s Variables
The following is known as a qualified name:
objectReference.variableName

program uses similar code to display information about rectTwo. Objects of the same type have their own copy of the same instance variables. Thus, each Rectangle object has variables named origin, width, and height. When you access an instance variable through an object reference, you reference that particular object’s variable. The two objects rectOne and rectTwo in the CreateObjectDemo program have different origin, width, and height variables.
int height = new Rectangle().height;

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

이 사이트는 스팸을 줄이는 아키스밋을 사용합니다. 댓글이 어떻게 처리되는지 알아보십시오.