In the early stages of programming, programmers write programs based on the process of the program, which is called structured programming. Now also a lot of structured programming language is widely used, the most prominent is the C language.
Slowly people find that there are some common attributes and behaviors in these programs, and then extract the concepts of these objects with the same properties and behaviors out of class, by designing objects around the problem to be solved, and writing a class for each object that describes the behavior and properties of the object. Such programming is called object-oriented programming. A class is a description of an object, and similarly an object is an instance of a class. So what kind of a class is it?
Public classHuman {//Properties://properties are defined in the same way as variablesString name;//nameString gender;//Sex intAge//Age//Behavior://and behavior is the action that the object in the class has--Methods//Example: Running behavior Public voidrun () {System.out.println (Running); }}
A class created by a human template. It has properties that are also known as member variables, and it behaves as a method. Then the class has to instantiate the object through the constructor (a special method of constructing the class, each class having at least one constructor).
New Human ();
Procedure for creating an object: 1. Partition the memory space of an object in the heap 2. Allocate memory space to properties and methods such as 3. Assigns the initial value 4. Executes the statement in the constructor (this is the default constructor so it is not executed) the properties of the created object are the initial values. So what if we want the object to be assigned the value we want in the process of creating the object? Then we need to use a parametric constructor.
Public int Age ) { this. Name = name; this. gender = gender; this. Age = Age ;}
Objects with different initialization values are created from different constructors.
Object-oriented Java