java+this+内存_java this 隐式函数和内存分析

this 一般用于方法里面。

普通方法中,加不加this 无所谓

this 被称作隐式参数

创建对象时,内存会把this 当成参数传给一个对象,作为参数,因为this是一个隐式参数。比如public void study() 在内存中是public void study(this)

static方法里面不能用this,内存结构中,static方法里没有对象,static 方法也不能传递this对象,static方法不能调用普通属性和方法

32e568a1ad460dc024fc1c6988a91a9e.png

5c720052ac745ff4ae9a67d2647db922.png

ba9b1ee07bf1dc8be72c119f0f813cee.png

this()使用

ppackage com.bjsxt.testthis;

public class Student {

String name;

int id;

public void study() {

this.name="zhangsan";

System.out.println(name);

}

public void sayHello(String aname) {

System.out.println(name + "say hello to " + aname);

}

public Student(String name,int id){

//this();//this调用其他构造方法,必须位于第一句

this(name);//this调用其他构造方法,必须位于第一句

this.name=name;

this.id=id;

}

public Student(String name) {

// TODO 自动生成的构造函数存根

this.name=name;

}

public Student() {

// TODO 自动生成的构造函数存根

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值