//Java Constructor method: All objects in Java are created, and the object's automatic initialization process is done by the constructor of the class. //constructor method overloading: the same name but with different parameters and definitions. //1. No parameter construction method /**
Public Horse () {}*/ //2, with a parameter of the construction method /**Public Horse (String name) {this.name=name; } */ //3. Construction method with multiple parameters /**Public Horse (String name,int age) {this.name=name; This.age=age; } */ Public classtest{ Public Static voidMain (String args[]) {Cat C1=NewCat ();//This call method is the constructor method, which initializes the propertyC1.name= "Xiaodeng"; C1.age=28; C1.print (); System.out.println ("---------------"); C1.print ("MM"); System.out.println ("---------------"); C1.print ("Fengmei", 250); }}classcat{String name; intAge ; //Overloaded Method: The method name is the same but the parameters are different, no return value is required//No Parameters Public voidprint () {System.out.println ("Name:" +name+ "\nage:" +Age ); } //a parameter Public voidprint (String name) {System.out.println ("Name:" +name+ "\nage:" +Age ); } //Multiple Parameters Public voidPrint (String name,intAge ) {System.out.println ("Name:" +name+ "\nage:" +Age ); }}
Java Construction methods