Java object-oriented - constructor (the constructor)

Constructor is the class name of the same type but without the method returns. Or for the current instance of an object, and an object or a current return. To get an instance of a class, they tend to run its constructor. This blog on Java object-oriented constructor (constructor usage).


Overview constructor

☃ learning Java constructor is a very important concept, the constructor can provide many special ways, as a constructor method, in charge of a class member variables (domain) initialization. Examples of configuration and the default constructor is divided into non-default constructor.

修饰符 类名(参数列表){
       初始化语句;
}

Characterized constructor

☃ it has the same name of the class

☃ it does not declare the return type. (Declared with different void, void can be considered a type of return value, return null type)

☃ can not be static, final, synchronized, abstract, native modification rights can be modifier (public, protected, default (default), private) modification, can not have
return statement returns the value

The role of the constructor

Most useful ☃ constructor initialization is executed when the object is created when an object is created, the system will default to initialize an instance of this object. If you want to change this default initialization can be achieved by a custom builder.

➥ constructor is a special method, it is overloaded

Classification constructor

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

☃ no implicit argument constructor (default provided)

☃ explicitly define one or more builders (Overload constructor)

note:

➥ Java language, every class has at least one constructor (do not write, then the system default provided)

➥ default constructor is consistent with the privileges of their class modifiers modifiers

Once ➥ explicitly define a constructor, the default configuration is no longer available

➥ create a plurality of class constructor overloads

➥ parent class constructor is not inherited by subclasses

Overloaded constructor

☃ general constructor to initialize the object while the object is created

☃ overloaded constructor allows objects to create a more flexible, easy to create a variety of objects

Consistent ☃ overloaded constructor overloading, the parameter list must, and methods

Examples of using the constructor

public class ConstructorTest {
    public static void main(String[] args) {
        /*
           在创建Dog类的实例时:Dog d = new Dog();
           系统自动调用构造器将属性初始化
        */
        Dog d1 = new Dog();
        System.out.println("狗狗的名字是" + d1.getName() + "有 " + d1.getLegs() + "条腿");
        Dog d2 = new Dog("圆圆" , 4);
        System.out.println("狗狗的名字是" + d2.getName() + "有 " + d2.getLegs() + "条腿");
    }
}

class Dog{
     private int legs;
        private String name;
        //Dog类的构造器,权限为public
        public Dog(){
            //初始化成员变量
            legs = 4;
            name = "小黑";
        }
        //构造器的重载
        protected Dog(String s,int i) {
            legs = i;
            name = s;
        }
        //成员变量的set和get方法
        public void setLegs(int i) {
            legs = i;
        }
        public int getLegs(){
            return legs;
        }
        public void setName(String s) {
            name = s;
        }
        public String getName(){
            return name;
        }
}
/*输出:
  狗狗的名字是小黑有 4条腿
  狗狗的名字是圆圆有 4条腿
*/

This blog and CSDN blog (ཌ ་.Asio Jun ་. ད) simultaneous release

Guess you like

Origin www.cnblogs.com/asio/p/12387369.html