[java] this、super 学习笔记

 

1.解决重名问题

(1)this是当前对象的引用,就是说当前用构造函数建的对象是谁,这个this就代表谁,它是一个引用

(2)this代表当前对象的一个引用。所谓当前对象,指的是调用类中方法或属性的那个对象。谁调用类中方法或属性,this指的就是谁

//例如

class Person {
    private String name;
    private int age;
    public Person(String name, int age) {
    // 局部变量name,age把Person类的属性屏蔽
        this.name = name;
        this.age = age;
    }
}

Person  p = new Person(xi,20);
//当前构造函数创建的对象是 "p" ,所以构造方法里的this 指的就是 “p”

这是因为它是先在局部找变量,如果找到了就用局部变量,没找到就去找全局变量。 

 

 2.在构造方法中调用构造方法(在前人的笔记上加了自己的注释)

package p1;

public class Test {
	  int petalCount = 0;
	  String s = "initial value";
	  //构造函数1
	  Test(int petals) {
	    petalCount = petals;
	    System.out.println("Constructor w/ int arg only, petalCount= "
	      + petalCount);
	  }
	  
	//构造函数2
	  Test(String ss) {
		  s = ss;//此处形参和实参名字不同,所以形参的值直接赋给当前对象里面的 “s” 
	    System.out.println("Constructor w/ String arg only, s = " + s );
//	    s=ss;  要是将上面的语句放到下面,这输出s的值就是initial  value;
	  }
	  
	//构造函数3
	  Test(String s, int petals) {
	    this(petals);// Can't call two!
	    this(s); //调用构造函数2
//	    上面语句是调用另外一个构造器,下面是将由于参数s的名称和数据成员s的名字相同,所以会产生歧义
//	    因此使用this.s来代表数据成员就能解决这个问题
	    this.s = s; // Another use of "this"  形参s赋值给 当前对象里面的s
	    System.out.println("String & int args");
	  }
	  
	//构造函数4
	  Test() {
	    this("hi", 47);
	    System.out.println("default constructor (no args)");
	  }
	  void printPetalCount() {
	//! this(11); // Not inside non-constructor!
	    System.out.println("petalCount = " + petalCount + " s = "+ s);
	  }
	  
	  //程序入口
	  public static void main(String[] args) {
		  //在类中可以定义多个 构造函数,在对象创建并初始化的时候,可以只调用一个构造函数
	    Test x = new Test();//调用的是 无参的构造函数
	    x.printPetalCount();
	  }
}

3.普通的直接引用

this相当于是指向当前对象本身

4. this在构造方法中使用时的注意事项

package p1;
public class Test {  
  
    private int i=0;  
  
    //第一个构造器:有一个int型形参  
    Test(int i)
    {        
       System.out.println("i:"+this.i);           
       this.i=i+1;//此时this表示引用成员变量i,而非函数参数i          
       //下面的i表示形参i,this.i表示数据成员  
       System.out.println("Int constructor i:"+i+"  ——  this.i:"+this.i);          
       System.out.println("this.i:"+this.i);   
       System.out.println("  i-1:  "+(i-1)+"  this.i+1:  "+(this.i+1));   
       //从两个输出结果充分证明了i和this.i是不一样的!   
    }  
  
    //  第二个构造器:有一个String型形参   
    Test(String s)
    {            
//      重新调用新的构造器之后,数据成员的值不再受到上一个的影响  
//      难道是因为每个构造器都有自己独立内存空间吗?  
       System.out.println("2i:"+this.i);           
       System.out.println("String constructor:  "+s);   
    }  
    
    //  第三个构造器:有一个int型形参和一个String型形参  
    Test(int i,String s)
    {  
       this(s);//this调用第二个构造器  
       /*this(i);*/  
  
       /*此处不能用,因为其他任何方法都不能调用构造器,只有构造方法能调用他。 
       但是必须注意:就算是构造方法调用构造器,也必须为于其第一行,构造方法也只能调 
       用一个且仅一次构造器! 
       http://blog.sina.com.cn/s/blog_8612e75d0100ze1m.html 
       */  
         
       System.out.println("3i:"+this.i);           
       this.i=i++;//this以引用该类的成员变量   此处的i是形参,赋的值 
       System.out.println("Int constructor:  "+i+"\n"+"String constructor:  "+s);    
    }  
  
/*    Test(int i ,String s,double a){ 
         
        this(i); 
         System.out.println("5i:"+this.i); 
    }*/  
      
    public Test increment() //返回类型为Test类
    {    
        System.out.println("4i:"+this.i);  
        this.i++;  
       return this;//返回的是当前的对象,该对象属于(Test)   
    }  
  
    public static void main(String[] args)
    {    
       Test tt0=new Test(10);    
       Test tt1=new Test("ok");    
       Test tt2=new Test(20,"ok again!");  
//       Test tt3=new Test(15, "okhushid", 0.12345);
  
     //由于increment()通过this关键字返回了当前对象的引用,所以很容易在一条语句里面对同一个对象执行多次操作  
//      System.out.println(tt0.increment().increment().increment().i);  
//       输出的是this.i,就相当于输出其数据成员  
//       System.out.println(tt0.increment().increment().increment());  
//       输出的是test.Test@2b0a141e,这是什么?这是java中的内存地址吗?  
         
         
       //http://www.cnblogs.com/java-class/archive/2012/12/19/2825463.html  
  
    }  

}

super()和this ()不能共存,否则编译时会报异常。 
Constructor call must be the first statement in a constructor 
换句话说就是super()和this ()都必须在构造方法的第一行。 
this(有参数/无参数) 用于调用本类相应的构造函数 
super(有参数/无参数) 用于调用父类相应的构造函数 
而且在构造函数中,调用必须写在构造函数定义的第一行,不能在构造函数的后面使用。 
一个构造函数定义中不能同时包括this调用和super调用,如果想同时包括的话,可以在一每构造器中使用一个。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值