how2j.cn


工具版本兼容问题
this这个关键字,相当于普通话里的“

小明说 “我吃了” 这个时候,“” 代表小明
小红说 “我吃了” 这个时候,“” 代表小红
""代表当前人物

this这个关键字,相当于普通话里的“
this即代表当前对象


本视频是解读性视频,所以希望您已经看过了本知识点的内容,并且编写了相应的代码之后,带着疑问来观看,这样收获才多。 不建议一开始就观看视频



12分37秒
本视频采用html5方式播放,如无法正常播放,请将浏览器升级至最新版本,推荐火狐,chrome,360浏览器。 如果装有迅雷,播放视频呈现直接下载状态,请调整 迅雷系统设置-基本设置-启动-监视全部浏览器 (去掉这个选项)。 chrome 的 视频下载插件会影响播放,如 IDM 等,请关闭或者切换其他浏览器



步骤 1 : this代表当前对象   
步骤 2 : 通过this访问属性   
步骤 3 : 通过this调用其他的构造方法   
步骤 4 : 练习-构造方法(this)   
步骤 5 : 答案-构造方法(this)   

步骤 1 :

this代表当前对象

edit
this代表当前对象
public class Hero { String name; //姓名 float hp; //血量 float armor; //护甲 int moveSpeed; //移动速度 //打印内存中的虚拟地址 public void showAddressInMemory(){ System.out.println("打印this看到的虚拟地址:"+this); } public static void main(String[] args) { Hero garen = new Hero(); garen.name = "盖伦"; //直接打印对象,会显示该对象在内存中的虚拟地址 //格式:Hero@c17164 c17164即虚拟地址,每次执行,得到的地址不一定一样 System.out.println("打印对象看到的虚拟地址:"+garen); //调用showAddressInMemory,打印该对象的this,显示相同的虚拟地址 garen.showAddressInMemory(); Hero teemo = new Hero(); teemo.name = "提莫"; System.out.println("打印对象看到的虚拟地址:"+teemo); teemo.showAddressInMemory(); } }
步骤 2 :

通过this访问属性

edit
通过this关键字访问对象的属性
public class Hero { String name; //姓名 float hp; //血量 float armor; //护甲 int moveSpeed; //移动速度 //参数名和属性名一样 //在方法体中,只能访问到参数name public void setName1(String name){ name = name; } //为了避免setName1中的问题,参数名不得不使用其他变量名 public void setName2(String heroName){ name = heroName; } //通过this访问属性 public void setName3(String name){ //name代表的是参数name //this.name代表的是属性name this.name = name; } public static void main(String[] args) { Hero h =new Hero(); h.setName1("teemo"); System.out.println(h.name); h.setName2("garen"); System.out.println(h.name); h.setName3("死歌"); System.out.println(h.name); } }
步骤 3 :

通过this调用其他的构造方法

edit
如果要在一个构造方法中,调用另一个构造方法,可以使用this()
public class Hero { String name; //姓名 float hp; //血量 float armor; //护甲 int moveSpeed; //移动速度 //带一个参数的构造方法 public Hero(String name){ System.out.println("一个参数的构造方法"); this.name = name; } //带两个参数的构造方法 public Hero(String name,float hp){ this(name); System.out.println("两个参数的构造方法"); this.hp = hp; } public static void main(String[] args) { Hero teemo = new Hero("提莫",383); System.out.println(teemo.name); } }
步骤 4 :

练习-构造方法(this)

edit  姿势不对,事倍功半! 点击查看做练习的正确姿势
参考练习-构造方法 设计一个构造方法,但是参数名称不太一样,分别是
String name
float hp
float armor
int moveSpeed

不仅如此,在这个构造方法中,调用这个构造方法
public Hero(String name,float hp)
步骤 5 :

答案-构造方法(this)

edit
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
查看本答案会花费2个积分,您目前总共有点积分。查看相同答案不会花费额外积分。 积分增加办法 或者一次性购买JAVA 基础总计0个答案 (总共需要0积分)
查看本答案会花费2个积分,您目前总共有点积分。查看相同答案不会花费额外积分。 积分增加办法 或者一次性购买JAVA 基础总计0个答案 (总共需要0积分)
账号未激活 账号未激活,功能受限。 请点击激活


HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。


问答区域    
2022-02-10 使用this后,转换报错
muzainansiqian0




构造方法中的参数为btye类型,类属性level无论是为int还是byte都会报错


17 个答案

Zzz112138
答案时间:2024-08-22
huazhang
答案时间:2024-07-10
public class Hero { String name; //姓名 float hp; //血量 float armor; //护甲 int moveSpeed; //移动速度 public Hero(String name,float hp) { this.name=name; this.hp=hp; System.out.println("这是第一个方法"); } public Hero(String name,float hp,float armor,int moveSpeed){ this(name, hp); this.armor=armor; this.moveSpeed=moveSpeed; System.out.println("这是第二个方法"); } public static void main(String[] args) { Hero teemo = new Hero("提莫",383,2.2f,34); System.out.println(teemo.name); } }

hncj指尖风雨
答案时间:2023-12-28
package demo9; public class Hero { private String name; private float hp; private float armor; private int moveSpeed; public Hero(String name,float hp){ this.name=name; this.hp=hp; System.out.println("第一种构造方法被执行 "); } public Hero(String name,float hp,float armor,int moveSpeed){ this(name,hp); this.armor=armor; this.moveSpeed=moveSpeed; System.out.println("第二种构造方法被调用 "); } public static void main(String[] args) { Hero h1=new Hero("张三",12.2F,22.0F,3); } }

wangha
答案时间:2023-06-22
learn杜
答案时间:2023-01-14
lanren9
答案时间:2022-11-11
你好163
答案时间:2022-11-02
我是地瓜
答案时间:2022-10-24
public class This { String name; float hp; float armor; int moveSpeed; public This(String name, float hp, float armor, int moveSpeed){ this.name = name; this.hp = hp; this.armor = armor; this.moveSpeed = moveSpeed; } public static void main(String[] args) { This garlen = new This("盖伦",3000,300,350); System.out.println(garlen.name); } }

阿文6
答案时间:2022-10-20
public class Hero{ String name; float Hp; float Armor; float speed; public Hero(String name,float hp){ this.name = name; this.Hp = hp; } public Hero(float armor,float moveSpeed){ this.Armor = armor; this.speed = moveSpeed; } public static void main(String[] args){ Hero xx = new Hero("夏洛特",10000); Hero ss = new Hero(345,6666); System.out.println(xx.name+'\n'+xx.Hp+'\n'+ss.Armor+'\n'+ss.speed); } }

给你买粉包
答案时间:2022-10-18
13464339979Y
答案时间:2022-08-24
youHan
答案时间:2022-07-04
jk-java
答案时间:2022-05-05
芬达味橘猫
答案时间:2022-04-29
public class Test09 { String name; //姓名 float hp; //血量 float armor; //护甲 int moveSpeed; //移动速度 public Test09(){ System.out.println("一个参数的构造方法"); } public Test09(String name,float hp){ System.out.println("两个参数的构造方法"); this.name = name; } public Test09(String name, float hp,float armor,int moveSpeed){ this(name,hp); System.out.println("四个参数的构造方法"); this.armor = armor; this.moveSpeed = moveSpeed; } public static void main(String[] args) { Test09 teemo = new Test09("提莫",400,15,350); }

dyf学java
答案时间:2022-04-17
hoolich
答案时间:2022-03-08
public class Hero { String name; //姓名 float hp; //血量 float armor; //护甲 int moveSpeed; //移动速度 public Hero(){ System.out.println("无参数的构造方法"); } //带一个参数的构造方法 public Hero(String name){ System.out.println("一个参数的构造方法"); this.name = name; } //带两个参数的构造方法 public Hero(String name,float hp){ this(name); System.out.println("两个参数的构造方法"); this.hp = hp; } //带四个参数的构造方法 public Hero(String name,float hp,float armor,int moveSpeed){ this(name,hp); System.out.println("4个参数的构造方法"); this.armor = armor; this.moveSpeed= moveSpeed; } public static void main(String[] args) { Hero teemo = new Hero("提莫",383); System.out.println(teemo.name); Hero sige = new Hero("死歌",33,22,111); System.out.println(sige.moveSpeed); } }

GGP
答案时间:2022-02-11
!



回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢


2021-11-23 构造方法(this)练习
sysucjf




运行成功


11 个答案

27hrs
答案时间:2024-05-16
Leslie_sakura
答案时间:2024-04-11
起个名
答案时间:2023-03-19
111

JACKBLACK
答案时间:2022-07-17
public class Hero { String name; //姓名 float hp; //血量 float armor; //护甲 int moveSpeed; //移动速度 public Hero(String name,float hp) { this.name=name; this.hp=hp; } public Hero(String name,float hp,float armor,int moveSpeed){ this(name,hp); this.armor=armor; this.moveSpeed=moveSpeed; } public void a() { System.out.print(name+hp+armor+moveSpeed); } public static void main(String[] args) { Hero garen = new Hero("盖伦",500,400,10); garen.a(); } }

yuxiaoping12
答案时间:2022-03-31
KiraJuliet
答案时间:2022-03-19
public class Hero { String name; //姓名 float hp; //血量 float armor; //护甲 int moveSpeed; //移动速度 //带一个参数的构造方法 public Hero(String name){ System.out.println("一个参数的构造方法"); this.name = name; } //带两个参数的构造方法 public Hero(String name,float hp){ this(name); System.out.println("两个参数的构造方法"); this.hp = hp; } //带4个参数的构造方法 public Hero(String name,float hp,float armor,int moveSpeed){ this(name,hp); System.out.println("4个参数的构造方法"); this.armor = armor; this.moveSpeed=moveSpeed; } public static void main(String[] args) { Hero teemo = new Hero("提莫",383,4,12); System.out.println(teemo.name); } }

木木12
答案时间:2022-02-25
木玄灵
答案时间:2022-02-19
tongJJ
答案时间:2022-01-14
zoe22111
答案时间:2022-01-12
Mondaylivesmatter
答案时间:2021-11-30



回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢


2021-10-09 带不同参数的构造方法之间的相互调用
2021-07-08 2021-7-8-AM-11-40
2021-06-11 构造方法中,用this(参数列表)来调用对应参数列表的构造方法,不过要注意:这个要写在第一行,并且只能有一个this.


提问太多,页面渲染太慢,为了加快渲染速度,本页最多只显示几条提问。还有 55 条以前的提问,请 点击查看

提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢