详解Java中的this关键字

xiaoxiao2026-06-04  15

使用this调用本类中的属性现在观察以下代码,看会有那些问题:public void setName(String name){name = name ;} 这里面的两个name都是setName方法中的name参数。此时,特别希望可以通过一个指定的标识明确的表示要把传入的name参数的值给类中的属性,所以此时就需要使用this关键字,使用this.name就表示类中的属性。class Person{private String name ;private int age ;public Person(String name,int age){this.setName(name) ;this.setAge(age) ;}public void setName(String name){this.name = name ;}public void setAge(int age){this.age = age ;}public String getName(){return this.name ;}public int getAge(){return this.age ;}public void print(){System.out.println("姓名:"+this.name+",年龄:"+this.age) ;}};public class Demo35{public static void main(String args[]){Person p1 = new Person("张三",30) ;p1.print() ;}}; 使用this还可以从一个构造方法中调用其他构造方法。例如:有以下一个要求,一个类中存在了三个构造方法,但是要求, 不管怎么调用,最终都要求可以在对象实例化的时候打印一个“新的对象产生了”的提示。class Person{private String name ;private int age ;public Person(){System.out.println("新的对象产生了。。。") ;}public Person(String name){System.out.println("新的对象产生了。。。") ;this.setName(name) ;}public Person(String name,int age){System.out.println("新的对象产生了。。。") ;this.setName(name) ;this.setAge(age) ;}public void setName(String name){this.name = name ;}public void setAge(int age){this.age = age ;}public String getName(){return this.name ;}public int getAge(){return this.age ;}public void print(){System.out.println("姓名:"+this.name+",年龄:"+this.age) ;}}; 以上代码虽然可以实现功能,但是同样的代码出现了三次,而且后面的两次出现纯属多余吧。用this()的形式可以调用类中的无参构造方法。class Person{private String name ;private int age ;public Person(){System.out.println("新的对象产生了。。。") ;}public Person(String name){// 最终都是调用无参构造方法this() ;this.setName(name) ;}public Person(String name,int age){this(name) ;this.setAge(age) ;}public void setName(String name){this.name = name ;}public void setAge(int age){this.age = age ;}public String getName(){return this.name ;}public int getAge(){return this.age ;}public void print(){System.out.println("姓名:"+this.name+",年龄:"+this.age) ;}};public class Demo36{public static void main(String args[]){Person p1 = new Person("张三",30) ;p1.print() ;}}; 注意点1:如果使用了this调用其他构造方法,则此语句,必须写在构造方法的首行。public void fun(){// 发现在调用fun方法的时候,必须先设置name的值this("zhangsan") ;} public Person(String name,int age){this.setAge(age) ;this(name) ; //--> 必须放在首行} 注意点2:使用this可以调用本类中的其他构造方法,但是至少留一个构造方法,作为程序的出口。public Person(){this("a",10) ;System.out.println("新的对象产生了。。。") ;}public Person(String name){// 最终都是调用无参构造方法this() ;this.setName(name) ;}public Person(String name,int age){this(name) ; //--> 必须放在首行this.setAge(age) ;} this最重要的特性 —— 表示当前对象当前对象在程序中用以下形式体现:· 当前操作此方法的对象,就称为当前对象。class Demo{public void print(){System.out.println(this) ;}};public class Demo38{public static void main(String args[]){Demo d1 = new Demo() ;System.out.println(d1) ;d1.print() ;System.out.println("---------------------") ;Demo d2 = new Demo() ;System.out.println(d2) ;d2.print() ;}}; 回顾:之前讲解的两个对象比较的程序。// 在类的内部增加一个比较的方法public boolean compare(Person p){Person p1 = this ;Person p2 = p ;if(p1.name.equals(p2.name)&&p1.age==p2.age){return true ;}else{return false ;}} 分析程序的运行过程:class A{private B b = null ;public A(){this.b = new B(this) ;this.b.fun() ;}public void hello(){System.out.println("Hello World!!!") ;}};class B{private A a = null ;public B(A a){this.a = a ;}public void fun(){this.a.hello() ;}};public class OODemo40{public static void main(String args[]){A aa = new A() ;}};
转载请注明原文地址: https://www.6miu.com/read-5049602.html

最新回复(0)