【Java的this 07】
一、第一个用法
this是一个引用,指向当前对象
局部变量和成员变量重名,用this
this可以用来区分局部变量和成员变量
package com.company.StaticMothod; /** * @Created by IntelliJ IDEA. * @Author tk * @Date 2019/12/24 * @Time 16:24 */ class StudentTest { //学号 private int id; //姓名 private String name; //性别 private String sex; //年龄 private int age; //地址 private String address; //设置学号 public void setId(int id){ this.id = id; } //读取学号 public int getId(){ return id; } public void setName(String name){ this.name = name; } public String getName(){ return name; } public void setSex(String sex){ this.sex = sex; } public String getSex(){ return sex; } public void setAge(int age){ this.age = age; } public int getAge(){ return age; } public void setAddress(String address){ this.address = address; } public String getAddress(){ return address; } } public class Student{ public static void main(String[] args){ StudentTest stu1 = new StudentTest(); stu1.setId(1); stu1.setName("张三"); stu1.setSex("男"); stu1.setAge(18); stu1.setAddress("北京"); System.out.println("Id:"+stu1.getId()); System.out.println("name:"+stu1.getName()); System.out.println("sex:"+stu1.getSex()); System.out.println("age:"+stu1.getAge()); System.out.println("Address:"+stu1.getAddress()); } }
二、第二个用法
this可以出现在构造方法的第一行
通过一个构造方法去调用另外一个构造方法
目的:代码重用
语法:this(实参)
package com.company.StaticMothod; /** * @Created by IntelliJ IDEA. * @Author tk * @Date 2019/12/24 * @Time 16:24 */ class StudentTest { //学号 private int id; //姓名 private String name; //性别 private Boolean sex; //年龄 private int age; //地址 private String address; public StudentTest(){ this(1,"张三",true,18,"北京");//无参的这个构造方法调用有参的这个构造方法 } public StudentTest(int id,String name,Boolean sex,int age,String address){ this.id = id; this.name = name; this.sex = sex; this.age = age; this.address = address; } public StudentTest(int d){ this();//有参的构造方法调用无参的构造方法 } //设置学号 public void setId(int id){ this.id = id; } //读取学号 public int getId(){ return id; } public void setName(String name){ this.name = name; } public String getName(){ return name; } public void setSex(Boolean sex){ this.sex = sex; } public Boolean getSex(){ return sex; } public void setAge(int age){ this.age = age; } public int getAge(){ return age; } public void setAddress(String address){ this.address = address; } public String getAddress(){ return address; } } public class Student{ public static void main(String[] args){ StudentTest stu1 = new StudentTest(); System.out.println("--------------------------------"); System.out.println("Id:"+stu1.getId()); System.out.println("name:"+stu1.getName()); System.out.println("sex:"+stu1.getSex()); System.out.println("age:"+stu1.getAge()); System.out.println("Address:"+stu1.getAddress()); StudentTest stu2 =new StudentTest(1,"张三",true,18,"北京"); System.out.println("--------------------------------"); System.out.println("Id:"+stu2.getId()); System.out.println("name:"+stu2.getName()); System.out.println("sex:"+stu2.getSex()); System.out.println("age:"+stu2.getAge()); System.out.println("Address:"+stu2.getAddress()); StudentTest stu3 =new StudentTest(1); System.out.println("--------------------------------"); System.out.println("Id:"+stu3.getId()); System.out.println("name:"+stu3.getName()); System.out.println("sex:"+stu3.getSex()); System.out.println("age:"+stu3.getAge()); System.out.println("Address:"+stu3.getAddress()); } }
package com.company.ThisTest01; //需求:外部程序调用无参构造方法,获取有参构造方法的日期1970.1.1 class Mydate{ int year; int month; int day; Mydate(){ this(1970,1,1); } Mydate(int year,int month,int day){ this.year = year; this.month = month; this.day = day; } } public class Test002 { public static void main(String[] args){ Mydate mydate = new Mydate(); System.out.println(+mydate.year+"年"+mydate.month+"月"+mydate.day+"日"); } }
三、第三个用法
this关键字:
1、this是一个引用,保存内存地址,指向自身
2、在堆中的每个java对象都有this
3、this可以出现在成员方法中,谁去调用这个成员方法,this就是谁,this指向的就是当前对象
4、在成员方法中“this.”是可以省略的。
5、this不能再static的方法中使用
注意:"this."可以出现在成员方法 内也可以出现在构造方法内,但是“this()”只能出现在构造方法内并且只能出现在第一行,不能出现在成员方法内
package com.JavaStudy.study0423; /** * @Created by IntelliJ IDEA. * @Author wufq * @Date 2020/4/27 11:10 * this在成员方法中,谁调用成员方法,this就是谁,指向本身 */ public class Demo3 { public static void main(String[] args){ Test2 test2 = new Test2(); System.out.println("test2对象内存地址:"+test2); test2.worker(); System.out.println("-----------------"); Test2 test3 = new Test2("张三"); System.out.println("test3内存地址:"+test3); test3.worker(); } } class Test2{ String name; Test2(){} Test2(String name){ this.name = name;//this引用,在堆中指向自己,区分成员变量和局部变量 } public void worker(){ System.out.println("this调用:"+this);//验证this在成员方法中,谁调用成员方法,谁就是this this.run();//这两个验证this在成员方法中可以省略 run(); } public void run(){ //成员方法中使用this.成员变量 或者 this.成员方法都可以直接省掉this System.out.println(this.name+" is runing!"); System.out.println(name+" is runing!"); } } ===执行结果=== test2对象内存地址:com.JavaStudy.study0423.Test2@2503dbd3 this调用:com.JavaStudy.study0423.Test2@2503dbd3 null is runing! null is runing! null is runing! null is runing! ----------------- test3内存地址:com.JavaStudy.study0423.Test2@4b67cf4d this调用:com.JavaStudy.study0423.Test2@4b67cf4d 张三 is runing! 张三 is runing! 张三 is runing! 张三 is runing! 通过打印可以看出内存地址一样,说明this在成员方法中,谁调用就是谁,指向当前的对象
四、练习题
package com.company.ThisTest01; /* 梯形: 设计一个梯形类,要求:三个成员变量,上底、下底和高,a b h全部私有化 构造方法: 缺省构造器 成员方法: (公开的)能改变梯形的上底,下底和高 (公开的)能得到高 (私有的)能计算梯形的面积,返回值类型是double (公开的)打印梯形的面积是xxx */ public class TiXin { private double a; private double b; private double h; TiXin(){} public void setABH(double a ,double b,double h){ this.a = a; this.b = b; this.h = h; } public double getH(){ return h; } private double calcArea(){ return (a+b)*h/2; } public void print(){ System.out.println("梯形的面积是:"+calcArea()); } public static void main(String[] args){ TiXin t = new TiXin(); t.setABH(3,4,5); t.print(); } }