/**
* Description:
* <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
* <br/>Copyright (C), 2001-2010, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
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();
}
}
/*
a和f所引用的对象是否相同:true
访问a所引用对象的color实例变量:红色
访问f所引用对象的color实例变量:未确定颜色
Apple方法
Apple方法
Fruit方法
请按任意键继续. . .
*/
getThis() { return this;}//getSuper() { return super.getThis();
最新推荐文章于 2022-03-15 15:46:40 发布
