1. Constructor (Constructor | Constructor)
When creating an object (new), a special method is called, which initializes the object information to serve new. This method is called "constructor"
Create a new object using the new + constructor.
A constructor is a function defined in a Java class to initialize an object.
The constructor has the same name as the class and no return value.
For example, the constructor of the Person class:
public class Person { int id; int age; Person(int n, int i){ id = n; age = i; } }
When creating an object, initialize the member variables of the object with the constructor
public class Test { public static void main(String[] args) { Person tom = new Person(1, 18); Person jeck = new Person(2, 20); } }
2. Characteristics of structural methods:
1) Names must be the same as class names
2) No return type | void
3) Constructors can have return, return selective jump-out constructors
4) Can not call casually, and use with new, other times can not call
5) The modifier cannot be final abstract static
Effect:
Initialize object information, not for creating objects
Empty structure:
A parametric constructor, a parametric constructor
1) In a class, if there is no display | add any constructor manually, javac compiles and automatically add empty constructs
2) Once a constructor is added, javac does not add an empty constructor
3. Heavy load of methods
Method overload refers to a class that can define multiple methods with the same method name but different parameters. When invoked, the corresponding method is selected according to different parameter lists.
IV. Heavy Load of Construction Method
Like ordinary methods, construction methods can also be overloaded
Examples are as follows:
V. Reference and Memory Analysis
A: Memory diagram of an object
B: Memory diagrams for two objects
C: Memory diagrams of three objects
Memory analysis
Divide
The characteristics cited are as follows:
1) One reference can only point to one object at a time
2) An object can be directed by multiple references, one of which changes it, and other references to the object can also see the change.
3) Java passes everything by value, referring to the copy address
Conventional naming rules
Capital letters for class names
Alphabetic lowercase for variable name and method name
Use hump marking
Example:
public class Cat { String color; // Hair color String name; // Name public void chase(Mice mice){ mice.type = "jerry"; // change System.out.println(name + " Capture " + mice.type); } public static void main(String[] args) { // Reference and memory analysis (run-time is the flow of data) Cat cat = new Cat(); cat = new Cat(); // References change, and at the same time a reference can only point to an object cat.color = "black"; cat.name = "tom"; Mice mice = new Mice(); mice.type = "Mickey"; // Connecting the two - "Dependence" cat.chase(mice); // When passed, the value copy, copy address, and after the copy is completed, an object is pointed to by multiple references. System.out.println(mice.type); // One of the jerry changes to it, and other references to the object can subtract the change } } class Mice{ String type; }
Define the following classes
public class BirthDate { int day; int month; int year; public BirthDate(int d, int m, int y){ day = d; month = m; year = y; } void setDay(int d){ day = d; } void setMonth(int m){ month = m; } void setYear(int y){ year = y; } int getDay(){ return day; } int getMonth(){ return month; } int getYear(){ return year; } void display(){ System.out.println(day + "-" + month + "-" + year); } }
Test class:
public class Test { public static void main(String[] args) { Test test = new Test(); int date = 9; BirthDate d1 = new BirthDate(7,7,1970); BirthDate d2 = new BirthDate(1,1,2000); test.change1(date); test.change2(d1); test.change3(d2); System.out.println(date); System.out.println(d1); System.out.println(d2); } void change1(int i){ i = 1234; } void change2(BirthDate b){ b = new BirthDate(22,2,2004); } void change3(BirthDate b){ b.setDay(22); } }
Summary:
Object creation and use must use the new keyword to create objects, object reference. member variable to refer to the member variable of the object, and object reference. method (parameter list) to call the method of the object.
Each object of the same class has different member variable storage space, and each object of the same class shares its method.
Non-static methods are called for each object.
Lebyte original, reproduced please indicate the source. Keep an eye on Lebytes