We extract classes from objects in the real world, and then write classes in Software Systems Based on classes in the real world. During the running process, the program interacts with each other and needs to constantly create objects, the following describes how to create an object.
1. Object Instantiation
The object is created by calling the constructor of the class through the keyword new, also known as Object Instantiation. For example, to create an object of person, you can use the following code:
New person ();
If the constructor requires parameters, you must provide the parameters during the call, for example:
New person ("James ");
2. Object Reference
To access this object in the future, you usually need to create an object reference to point to this object. For example, the following code:
Person P = new person ("James ");
What is the role of person P? Similar to int A, it defines a Variable P, whose type is person. We usually call this P as an object reference, while person is the type referenced by this object, sometimes the object we call refers to the reference of this object. The access to the object is completed through this reference, which is similar to the pointer in C ++.
3. Object method access
Next, you can access the method of the newly created object.
P. setheight (165 );
As long as the person method permits access, you can add "." To the P method for access.
Note that when accessing an object, make sure that the object reference points to the object instance. For example, the following code will produce an error:
Person P;
P. setname ("Li Si ");
At this time, an error is reported. The error is: nullpointexception. Although the object references p, p is not pointed to a specific object (also called an instance), so the value of P is null, therefore, nullpointexception is generated when the P method is called.
4. Object member Initialization
The constructor is used to initialize member variables. If a member variable is not initialized in the constructor, the default value is given. If it is a member variable of the basic type, the default value is as follows:
Char type: 0 characters encoded
Number (byte short int long): 0
Float and double types: 0.0
Boolean: false
If it is a member variable of the reference type (non-basic type), the default value is null. Generally, the member variable can be assigned a value after the object is instantiated and can be completed through the set method. Sometimes special initialization accesses, such as initialize, are also provided. For example:
Person P = new person ();
P. setname ("Li Si ");
P. setheight (175 );
When calling the object's reference type member variable, make sure that it is not null.
5. complete code
The complete code is as follows:
Package test;
Import java. Text. simpledateformat;
Import java. util. date;
Public class person {
Private int [];
Private int height;
Private date birthday;
Private string name;
Public date getbirthday (){
Return birthday;
}
Public void setbirthday (date birthday ){
This. Birthday = birthday;
}
Public String getname (){
Return name;
}
Public void setname (string name ){
This. Name = Name;
}
Public Person (){
}
Public Person (string name ){
This. Name = Name;
}
Public Person (string name, int height ){
This (name );
This. Height = height;
}
Public int getheight (){
Return height;
}
Public void setheight (INT height ){
This. Height = height;
}
Public static void main (string ARGs []) {
// Create and instantiate an object
Person P = new person ();
// Assign values to member variables
P. setname ("Li Si ");
P. setheight (175 );
String Birthday = "1989-3-11 ";
Simpledateformat df = new simpledateformat ("yyyy-mm-dd ");
Try {
P. setbirthday (DF. parse (birthday ));
} Catch (exception e ){
}
// Output the value of the member variable
System. Out. println ("name:" + P. getname ());
System. Out. println ("height:" + P. getheight ());
System. Out. println ("birthday:" + DF. Format (P. getbirthday ()));
}
}
For exception handling, we will introduce it later. For date, see:16th use of date and time
Last time:
29th member MethodsNext time:
31st constant MemberLi xucheng csdn blog: Why? U= 124362 & C = 7be8ba2b6f3b6cc5