The main purpose of the construction method is to initialize the properties of the class with the definition of the constructor method class Name {access permission class name {Type 1 parameter 1, type 2 parameter 2, ... {program statement; ...//constructor method does not return a value}}
in the declaration of the construction method, the reader must keep the following points in mind:· The name of the construction method must be the same as the class name · The declaration of a constructed method cannot have any declaration of a return value type · You cannot use return to return a value in a construction method
classPerson {PrivateString name;//declaring name Attributes Private intAge//declaring age attributes PublicPerson (String name,intAge) {//defining constructor methods for property initialization This. SetName (name);//Assigning a value to the Name property This. Setage (age);//Assigning a value to the Age property } Public voidTell () {//methods of obtaining informationSystem.out.println ("Name:" + getName () + ", Age:" +getage ()); } ... Public voidSetage (intA) {//Set Age if(A >= 0 && a < 150) {//Add the validation code hereAge =A; } }} Public classConsDemo02 { Public Static voidMain (String args[]) {person per=NewPerson ("Zhang San", 30);//call a constructor method, passing two parametersPer.tell ();//Output Information }}Third, the construction method 3.1, the default constructor method there must be a constructor in each class if a class does not declare an explicit constructor, it automatically generates a constructor that does nothing without a parameter.
PackageCom.pb.demo2;/** Human*/ Public classperson {//persion for Class name /** Properties, Name,sex,age*/ PublicString name;//name PublicString sex;//Sex Public intAge//Age /** Method (behavior)*/ //Eat Public voidEat (String name) {//Pass in a string type parameterSystem.out.println ( This. name+ "Invite" +name+ "to dine Together"); } //work Public voidWork () {System.out.println (name+ "The work of the idea is that work to earn money have food to eat!"); } //work Public voidWork (String contect) {System.out.println (name+ "The Work philosophy is:" +contect); }}
The above class does not define a constructor method, but the parameterless constructor is called by default in the object that created the class.
Public Person () { }
Public Static void Main (string[] args) { //Create object of person class hanbing=new person (); Calling a constructor without arguments}
The overloaded construction method of the constructor method is the same as the normal method:the type or number of arguments is different4.1 Overloaded example of a construction method
Public Person () {// no parameters } Public Person (String name) { // A parameter of this . Name=name ; } Public Person (String name,int. age ) { // two parameter this . Name=name; This . age=age; }
4.2, Example 2
PackageCom.pb.demo2;/** Film category*/ Public classFilm { PublicString type;//genre, horror film, Love, etc. PublicString name;//Movie Name PublicString Director;//Director PublicString actor;//starring PublicFilm () {//method of constructing without parameters } PublicFilm (String type) {//a parameter This. type=type; } PublicFilm (String type, string name, string director, string actor) {//how to construct all parameters This. Type =type; This. Name =name; This. Director =Director; This. actor =actor; } //Introduction Method Publicstring Display () {string msg= "Movie type:" +type+ "\ n" \ +name+ "\ n Director:" +director+ "\ n Starring:" +actor; returnmsg; }}
Test class
PackageCom.pb.demo2;/** Film Test class*/ Public classFilmtest { Public Static voidMain (string[] args) {System.out.println ("******* normal call ******"); //declaring a Movie objectFilm film=NewFilm (); //for the type of object, name, director, starring assignmentFilm.type= "Shootout film"; Film.name= "Let the Bullets Fly"; Film.director= "Jiang Wen"; Film.actor= "Chow Yun, ge you, Jiang Wen"; //Calling MethodsSystem.out.println (Film.display ()); System.out.println ("************** using construction method ****************"); //declaring a movie object and passing in a parameter by constructing a methodFilm two=NewFilm ("Love film", "Can't Tell The Secret", "Jay Chou", "Jay Chou, Anthony Wong, Gui Lun magnesium"); System.out.println (Two.display ()); }}
Java Starts from scratch 12 (construction method)