package OutInnerClassPakage;
//普通 匿名 内部类 形式1 主要是类与类之间
public class TestAnon {
public String str="1";
public void showInfo()
{
System.out.println("-------------TestAnon----------");
}
/*
* 在本类中不可定义 自己的 匿名 内部类 检查通过,
* 但运行时 会出错 其出错的原因是 递归
*
* TestAnon ta1=new TestAnon(){
//super this //在这里是------可用
TestAnon tanon=new TestAnon();
public String str1="2";
public void showInfo()
{
System.out.println("-------------test----------");
tanon.showInfo();
}
public void showInfo1()
{
System.out.println("-------------test1----------");
}
};
*/
/*
* 此段代码可用
* 在 同个文件中 多个类中 每个类 均可以存在 main 函数
*
* public static void main(String[] args) {
//该操作完成了 声明一个--匿名-- 的类 该类 为 TestAnon 的子类
//并且 这个匿名类重写了 父类TestAnon 中的showInfo 方法
TestAnon ta=new TestAnon(){
//super. //在这里是不可用的
//this. //在这里是不可用的
// 因为这是在 static main 函数中声明的
public String str1="2";
public void showInfo()
{
System.out.println("-------------test----------");
}
public void showInfo1()
{
System.out.println("-------------test1----------");
}
};
System.out.println("----------"+ta.str+"---------");
ta.showInfo();
// System.out.println("----------"+ta.str1+"---------");
// 无法访问在 匿名内部类中的新创建的新的成员变量
// 其无法 访问匿名类中的 其他新建的方法
// 但在匿名类 内部是可以自由的使用的
// 这种现象其主要原因是 引用的 一个特性 (只可看见 引用所属类中的方法)
}*/
}
class test{
public static void main(String[] args) {
//该操作完成了 声明一个--匿名-- 的类 该类 为 TestAnon 的子类
//并且 这个匿名类重写了 父类TestAnon 中的showInfo 方法
TestAnon ta=new TestAnon(){
//super. //在这里是------不可用的
//this. //在这里是--------不可用的
//------ 因为这是在 static main 函数中声明的
TestAnon tanon=new TestAnon();
public String str1="2";
public void showInfo()
{
System.out.println("-------------test----------");
tanon.showInfo();
}
public void showInfo1()
{
System.out.println("-------------test1----------");
}
};
System.out.println("----------"+ta.str+"---------");
ta.showInfo();
// System.out.println("----------"+ta.str1+"---------");
// 无法访问在 匿名内部类中的新创建的新的成员变量
// 其无法 访问匿名类中的 其他新建的方法
// 但在匿名类 内部是可以自由的使用的
// 这种现象其主要原因是 引用的 一个特性 (只可看见 引用所属类中的方法)
TestAnon ta1=new TestAnon(){
public void showInfo()
{
System.out.println("-------------ta1----------");
}
};
ta1.showInfo();
}
}