Constructor like java

Constructor features:

  • Having the same name and class;
  • Do not declare the return type;
  • Can not be static, final, synchronized, abstract, native modification, can not have a return statement returns the value;

The role of constructor: create an object, initialized to the object;

Syntax:

Modification of the name of the class name (parameter list) {

  Initialization statement;

}

public  class Animal {
     Private  int Age;
     public Animal () { 
        Age =. 4 ; 
    }   // constructor 
    public  void the setAge ( int I) Age = { I;}
     public  int getAge () { return Age;} 
} 

When creating a class Animal the instance, the constructor is invoked, when the initial value is given age. 4 
Animal a = new new Animal ()

Depending on the parameters, the configuration can be divided into two categories:

  • No implicit argument constructor (the system provides default);
  • Defining one or more display constructor;

note:

  • java language, every class has at least one builder;
  • The default constructor modifiers modifier such uniform belongs;
  • Once the display configuration is defined, the system is no longer using the default constructor;
  • A class can create multiple constructor overloads;
  • Parent class constructor is not inherited by subclasses;

Overloaded constructor: the creation of objects more flexible and convenient to create a variety of objects.

// Java can not simultaneously have two file a public class, but there may be multiple classes 
public  class the Person { 
    String name; 
    int Age;
     public the Person (String name, int Age, a Date D) {...}
     public the Person (String name, int Age) {...}
     public the Person (String name ,, a Date D) {...}
     public the Person () {...} 
}        

Guess you like

Origin www.cnblogs.com/xiximayou/p/12043927.html