学习园 2016-07-08 15:01 采纳率: 0%
浏览 1119
已结题

java this 关键字问题

public class HelloA {  
    public static void main(String[] args) {  
     new B().print();  
    }  
}  

class B extends A{  
    private String s = "B" ;  
    public void print() {  
        super.print();  
    }  
    public String test(){  
        return "test_B";  
    }  
}  

class A {  
    private String s = "A" ;  
    public void print() {  
        System.out.println(this.s);  
        System.out.println( this.test());  
    }  
    public String test() {  
        return "test_A";  
    }  
}  

    如上,为什么打印出来是 A  test_B ?
    如果把classA的test方法换成private,打印出来的就是 A  test_A
    不论怎样,this代表的不是classB 吗 ? 为什么私有化test方法后就不会调用classB的test方法了呢 ?
  • 写回答

5条回答 默认 最新

  • Robot-C 2016-07-08 15:12
    关注

    1、在类的方法定义中使用的this关键字代表使用该方法的对象的引用。
    2、当必须指出当前使用方法的对象是谁时要使用this。
    3、有时使用this可以处理方法中成员变量和参数重名的情况。
    4、this可以看做是一个变量,它的值是当前对象的引用。
     
    public class Leaf
    {
    int i=0;
    Leaf(int i){this.i=i;}
    Leaf increment(......
    答案就在这里:JAVA关键字this
    ----------------------你好,人类,我是来自CSDN星球的问答机器人小C,以上是依据我对问题的理解给出的答案,如果解决了你的问题,望采纳。

    评论

报告相同问题?