JAVA constructor is used and this (memory illustration)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_42947972/article/details/101478230

The constructor is similar to when we define a default already exists.
For example:

class Perso{
	 private String name;    //私有属性 名字
     private int age;        //私有属性 年龄
      //功能
    public void speak()
    {
        System.out.println(name+":"+age);
    }
}

Then we have a new target time

Perso p1 = new Perso();

Java virtual machine and then found that we did not use a constructor, it will help us add go in.
It can be said that the default initialization objects

class Perso{
	 private String name;    //私有属性 名字
     private int age;        //私有属性 年龄
      //功能
     Perso(){}
    public void speak()
    {
        System.out.println(name+":"+age);
    }
}

But we can also customize your own constructors
such as:

class Perso{
	 private String name;    //私有属性 名字
     private int age;        //私有属性 年龄
      //功能
     Perso()	//构造函数
     {
     	System.out.printf("hello");
     }
    public void speak()
    {
        System.out.println(name+":"+age);
    }
}

Then we have constructed the function, the virtual machine does not automatically help us add.
Here we have a new object if they will first output of hellothis sentence.

If this is the case we

class Perso{
	 private String name;    //私有属性 名字
     private int age;        //私有属性 年龄
      //功能
     Perso(System n)	//构造函数
     {
     	name = n;
     }
    public void speak()
    {
        System.out.println(name+":"+age);
    }
}

Then we have a new target when it is necessary to add parameters Perso p1 = new Perso("小明");
which also corresponds to our default initialize the object, and if the output, then the output will 小明:0
not preceded by a constructor, set parameters,属性都会自动初始化

Graphic memory

class Perso{
	 private String name;    //私有属性 名字
     private int age;        //私有属性 年龄
      //功能
     Perso()	//构造函数
     {
     	System.out.printf("hello");
     }
    public void speak()
    {
        System.out.println(name+":"+age);
    }
}
//这里的类就不写了
public static void main(String[] args){
        Perso p = new Perso();

Here Insert Picture DescriptionHere Insert Picture DescriptionHere Insert Picture Description
If this is the case, the memory is so

class Perso{
	 private String name;    //私有属性 名字
     private int age;        //私有属性 年龄
      //功能
     Perso(String n)	//构造函数
     {
     	name = n;
     }
    public void speak()
    {
        System.out.println(name+":"+age);
    }
}
//这里的类就不写了
public static void main(String[] args){
        Perso p = new Perso("小明");

In front are the same
Here Insert Picture Description
Here Insert Picture Description
as for the "Bob" is how to assign heap memory, which involves thiskeyword of.

In order to facilitate us to understand the code we generally define meaningful names
then, has led to the name, and the name of the conflict, so our this comes in handy

Perso(String name)	//构造函数
     {
     	name = name;	//这样导致name是为null的
     }

Then this would not be the

Perso(String name)	//构造函数
     {
     	this.name = name;	//这样导致name是为null的
     }

In fact, in the constructor, there is a default of this, a point that will create this object addresses
such as we said above, Xiao Ming is assigned to name,
in fact, this is the reason there is
Here Insert Picture Description
Here Insert Picture Description
a class among its members so, you want to be execution, there must object to call, but generally omitted this

thi may also be used in the constructor, call the constructor
for example:

Perso(String name){        //构造重载函数,字符串参数.
        this.name = name;
    }

    Perso(String n, int age){        //构造重载函数,字符串与整型参数.
        this(name);
        this.age = age;
    }

Here Insert Picture Description
Then popped
Here Insert Picture Description
then be popped
Here Insert Picture Description

 Perso(String n, int age){        //构造重载函数,字符串与整型参数.
        this.age = age;
         this(name);
    }

Not like this !!! will complain
with this constructor call other constructors must be placed on the first line !!!
because the initialization action must be executed

当成员变量和局部变量重名,可以用关键字this来区分.

this:代表对象.代表那个对象呢?当前对象
this就是所在函数所属对象的引用.
简单说:哪个对象调用lthis所在的函数,this就代表那个对象.

Guess you like

Origin blog.csdn.net/weixin_42947972/article/details/101478230