Definition of class and creation of objects
In Java , everything is an object. Writing a Java Program is the process of defining a class. A class is a template that specifies a prototype of a data structure.
The class contains two parts: Variables and methods.
Variables defined in a class are called data members or member variables.
A method defined in a class is called a member method or a member function.
Once you have defined a class, you cannot do anything to him, and you must make this template materialized. The process of making a template materialize is the process of creating an instance. Depending on the class template, you can create specific instances of these instances, called objects. The instantiation process involves allocating the necessary memory space for it, setting the appropriate initial value, and so on. In each object, the data members defined for the class are allowed to have their own values that describe the properties of the object. It can be said that an object is an instance of a class with its own property values.
The definition format of the class:
Modifier class class name [extends parent class name ]{
Type member variable 1;
Type member variable 2;
......
Modifier return value Member method 1 ( parameter list ) {
Type local variables ;
Method body
}
Modifier return value Member Method 2 ( parameter list ) {
Type local variables ;
Method body
}
......
}
Note: Bothclass and extends are keywords, and member variables and member methods can have more than one.
Access modifier: Public Common, private Privately,protected in between,friendly The default modifier, which is only within the package, is not visible for the out-of-package component.
Creation and initialization of objects:
A class is also a type, which, like the basic data type, defines the type after it has been defined, and it can be used to describe the variable. This variable is called an instance of the class and becomes an object.
1. Creation of objects
There are two types of objects in Java, objects with basic data types, and two of class types, as well as the creation process.
The base data type is the system-predefined data type, and the class type is the programmer's own definition.
Basic data types
When you describe a variable of a basic type, the system automatically establishes an object and allocates memory for that variable at a predetermined size.
Class type
It is necessary to explain the variables of the class, that is, to define the variable of this type, and then create the object, both of which are indispensable.
Sets the variable that already describes the class, the following statement creates a concrete object, and the variable name represents the object, sometimes referred to as a reference to the object.
Variable name = new class type ( parameter list );
If the variable is not yet described, you can also complete the description of the variable with the creation of the object, as follows:
Class type variable name = new class type ( parameter list );
Now to illustrate variables of the Date type:
Date Mybirthday, Yourbirthday;
Then create the object:
Mybirthday = new Date ();
Yourbirthday = new Date ();
Edge Description Edge creation: Date mybirthday = new Date ();
New is the keyword used by Java to allocate memory, which indicates that the system is actually allocating memory to the object.
2. Initialization of objects
There are two types of initialization: explicit initialization and use. "To assign a value
Display initialization: Assigns the initial value at the same time as the member variable definition
public class demo{
Private int a = 25;
Private float B = 6.6f;
Private String name = "Zhangsan";
}
“.” To assign a value: After you have described a reference to a class type, use ". "To assign an initial value, format:
The reference to the object . member Data = Initial value
Can be understood as (variable name . member Data = initial value) because the variable name is a reference to the object during the creation of the object!
Date Mybirthday = new Date ();
Mybirthday.day = 26;
Mybirthday.month= 11;
Yourbirthday.year = 2009;
The mybirthday is a reference to the variable name, which is the object, andday,month, and year are member data
Definition of class and creation of objects