super返回不过来

简介:

class Fruit 

    String color = "未确定颜色"; 
    //定义一个方法,该方法返回调用该方法的实例 
    public Fruit getThis() 
    { 
        return this; 
    } 
    public void info() 
    { 
        System.out.println("Fruit方法"); 
    } 

public class Apple extends Fruit 

    //重写父类的方法 
    @Override 
    public void info() 
    { 
        System.out.println("Apple方法"); 
    } 
    //通过super调用父类的Info()方法 
    public void AccessSuperInfo() 
    { 
        super.info(); 
    } 
    //尝试返回super关键字代表的内容 
    public Fruit getSuper() 
    { 
        return super.getThis(); 
    } 
    String color = "红色"; 
    public static void main(String[] args) 
    { 
        //创建一个Apple对象 
        Apple a = new Apple(); 
        //调用getSuper()方法获取Apple对象关联的super引用 
        Fruit f = a.getSuper(); 
        //判断a和f的关系 
        System.out.println("a和f所引用的对象是否相同:" + (a == f)); 
        System.out.println("访问a所引用对象的color实例变量:" + a.color); 
        System.out.println("访问f所引用对象的color实例变量:" + f.color); 
        //分别通过a、f两个变量来调用info方法 
        a.info(); 
        f.info(); 
        //调用AccessSuperInfo来调用父类的info()方法 
        a.AccessSuperInfo(); 
    } 
}

 

在上面的代码中,return this 可以将当前对象的引用返回过来,但是return super确实做不到的。

我还说不清楚为什么?

 本文转自二郎三郎博客园博客,原文链接:http://www.cnblogs.com/haore147/p/4214699.html,如需转载请自行联系原作者

相关文章
this和super的区别
this和super的区别
118 0
|
10月前
|
Java
.equal()和==的区别 怎样判断字符串为空问题: Illegal invoke-super to void nio.file.AccessDeniedException
.equal()和==的区别 怎样判断字符串为空问题: Illegal invoke-super to void nio.file.AccessDeniedException
72 1
定义类,super的使用,super的使用
要求: a.需要有一个类变量 b.需要有>=2个的对象变量 c.定义一个方法:打印类变量和对象变量 d.使用print打印对象->输出为This is a object e.实例化两个对象:且两个对象相加等于2 f.为对象添加一个临时变量temp_var
86 0
14-self与super以及isMemberOfClass与isKindOfClass的区别
14-self与super以及isMemberOfClass与isKindOfClass的区别
73 0
|
Java Maven
super.parseUnknownField()找不到
super.parseUnknownField()找不到
299 0
|
存储
为何要有return返回值?
为何要有return返回值?
140 0
|
C++
一个函数两个return
一个函数两个return
236 0
|
JSON 数据格式
09准备将Handler的返回值写入ServletResponse
在RequestMappingHandlerAdapter初始化完成后设置默认的HandlerMethodReturnValueHandler HandlerMethodReturnValueHandler体系介绍 HandlerMethodReturnValueHandler的执行流程
215 0
|
Python
Flask - 访问返回字典的接口报错:The view function did not return a valid response. The return type must be a string, tuple, Response instance, or WSGI callable, but it was a dict.
Flask - 访问返回字典的接口报错:The view function did not return a valid response. The return type must be a string, tuple, Response instance, or WSGI callable, but it was a dict.
1569 0
Flask - 访问返回字典的接口报错:The view function did not return a valid response. The return type must be a string, tuple, Response instance, or WSGI callable, but it was a dict.