JAVA 匿名 内部类 形式1

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();
 }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值