A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 絮缘小默 中级黑马   /  2014-3-5 22:09  /  1184 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

学习了匿名内部类,但是我自己写不出来,所以想请同学们给我写一下内部类的样式

评分

参与人数 1技术分 +1 收起 理由
朱神必 + 1

查看全部评分

5 个回复

倒序浏览
interface A{
public void fun1();
}
class B{
int i = 10;
public void get(A a)// 声明了一个方法get,接受一个类型为A的参数
       {
         a.fun1();
}
public void test(){
     this.get(new A()//这里就是匿名的内部类,实现了A这个接口的类,只是是匿名的,编译的时候会自动生成一个
//类名为B$1的类,这一点你可以用cmd手动编译一下,就能看见B$1.class这样一个文件,相当于你有一个B$1
//的类实现了A这个接口,然后作为get的参数传了进去
  {
   public void fun1() {
    System.out.println(i);
     }
  });
}
}
class TestNonameDemo{
public static void main(String args[]){
  B b = new B();
  b.test();
}
}

评分

参与人数 1技术分 +1 收起 理由
朱神必 + 1

查看全部评分

回复 使用道具 举报
  1. package com.uc;


  2. /**
  3. * <p>What's the nested class ?</p>
  4. * <p>define a class within another class</p>
  5. * <p>Why to use nested class ?</p>
  6. * <li>It is a way of logically grouping classes that are only used in one place.</li>
  7. * <li>It increases encapsulation.</li>
  8. * <li>Nested classes can lead to more readable and maintainable code.</li>
  9. *
  10. *
  11. */
  12. public class NestClassT {

  13.         public static void main(String[] args) {
  14.                 //test();
  15.                 test1();
  16.         }
  17.        
  18.         /**
  19.          * 测试instance nested class的用法
  20.          */
  21.         private static void test1() {
  22.                 // TODO Auto-generated method stub
  23.                 Outer outerObj = new Outer();
  24.                 Outer.InnerClass obj = outerObj.new InnerClass();
  25.                 obj.showInfo(" Called by Create context");
  26.                
  27.                 //查看inner class 是否可修改value of enclosing class variable
  28.                 System.out.println(outerObj.getInstanceVal());
  29.         }

  30.         /**
  31.          * 测试static nested class 与  static variable of enclosing class 的访问关系
  32.          */
  33.         private static void test() {
  34.                 // TODO Auto-generated method stub
  35.                 Outer.StaticInner obj = new Outer.StaticInner();
  36.                
  37.                 //可以访问static variable of enclosing class
  38.                 obj.showInfo();
  39.                
  40.                 //测试 static method of static nested class 对enclosing class的访问性
  41.                 Outer.StaticInner.staticMethod();
  42.                
  43.                 //从上面的测试可以看出 static nested class 与 static method of enclosing class
  44.                 //的访问性一致
  45.                 //还有一点需要注意的是,the modifiers of static nested class
  46.                 //{public, protected, private and packet private}
  47.         }       
  48. }

  49. class Outer {
  50.        
  51.         /**
  52.          * static nested class modifiers can be
  53.          * {public protected private and packet private}
  54.          */
  55.         public static class StaticInner {
  56.                
  57.                 public void showInfo () {
  58.                        
  59.                         //查看static nested class 是否可访问Enclosing class
  60.                         //static variable
  61.                         System.out.println(info + " in instance method of static nested class");
  62.                         System.out.println(instanceVal + " in instance method of static nested class");
  63.                         System.out.println(outerInfo);
  64.                        
  65.                         //variable shadow
  66.                         //我们可以通过完整的名称引用来解决命名冲突的问题
  67.                         System.out.println(Outer.info);
  68.                        
  69.                         //调用static private method of enclosing class
  70.                         System.out.println("Call static private method of enclosing class: ");
  71.                         System.out.println(staticMethodOfOuter(" called by instance method of static nested class call "));
  72.                 }
  73.                
  74.                 public static void staticMethod() {
  75.                        
  76.                         //访问外部变量,实际上是编译器在中间做了手脚,这个可以通过 javap命令查看
  77.                         System.out.println(outerInfo);
  78.                         System.out.println(info + " in static method of static nested class");
  79.                        
  80.                         //variable shadow
  81.                         //我们可以通过完整的名称引用来解决命名冲突的问题
  82.                         System.out.println(Outer.info);
  83.                        
  84.                         //calling static private method of enclosing class
  85.                         System.out.println("Calling static private method of enclosing class: ");
  86.                         System.out.println(staticMethodOfOuter(" called by static method of static nested class call "));
  87.                        
  88.                 }
  89.                
  90.                 //the static variable of static nested class
  91.                 private static String info = "static val";
  92.                
  93.                 //the instance variable of static nested class
  94.                 private String instanceVal = "instanceVal";
  95.         }
  96.        
  97.         //test the variable shadow
  98.         //there is a 'info' variable in the static nested class
  99.         private static String info = "private static info of enclosing class";
  100.         private static String outerInfo = "static val of enclosing class";
  101.         private static String staticMethodOfOuter(String val) {
  102.                 return "I am static private method of enclosing class" + val;
  103.         }
  104.        
  105.         //////////////////////////////////////////////////////////////////////
  106.         //下面测试instance nested class 对 enclosing class 的访问性
  107.         //
  108.         //As with instance methods and variables,
  109.         //an inner class is associated with an instance of its enclosing class
  110.         //and has direct access to that object's methods and fields.
  111.         //
  112.         //Also, because an inner class is associated with an instance,
  113.         //it cannot define any static members itself.
  114.         //
  115.         //To instantiate an inner class, you must first instantiate the outer class.
  116.         //Then, create the inner object within the outer object with this syntax:
  117.         //                OuterClass.InnerClass innerObject = outerObject.new InnerClass();
  118.         //Additionally, there are two special kinds of inner classes:
  119.         //local classes and anonymous classes.
  120.         //////////////////////////////////////////////////////////////////////
  121.        
  122.         private String instanceOuterInfo = "I am Instance variable of enclosing class";
  123.         private String instanceVal = "I am instance variable of enclosing method";
  124.        
  125.         public String getInstanceVal() {
  126.                 return this.instanceOuterInfo;
  127.         }
  128.         private String instanceMethod(String val) {
  129.                 return "I am instance method of enclosing class " + val;
  130.         }
  131.        
  132.         class InnerClass {
  133.                
  134.                 //The field info cannot be declared static in a non-static inner type,
  135.                 //unless initialized with a constant expression
  136.                 static final String info = "test";
  137.                 private String instanceVal = "I am instance varaible of instance nested method";
  138.                
  139.                 /**
  140.                  * The method staticMethod cannot be declared static;
  141.                  * static methods can only be declared in a static or top level type
  142.                  */
  143. //                public static void staticMethod() {
  144. //                       
  145. //                }
  146.                
  147.                 public void showInfo(String instanceVal) {
  148.                        
  149.                         //visit private variable of enclosing class
  150.                         System.out.println("visit the private static variable of enclosing class: ");
  151.                         System.out.println(Outer.info);
  152.                         System.out.println("visit the private instance variable of enclosing class: ");
  153.                         System.out.println(instanceOuterInfo);
  154.                        
  155.                        
  156.                         //visit private method of enclosing class
  157.                         System.out.println(staticMethodOfOuter(
  158.                                         " Called by private instance method of instance nested class") );
  159.                         System.out.println(instanceMethod(
  160.                                         " Called by private instance method of instance nested class") );
  161.                        
  162.                        
  163.                         //命名冲突解决方案
  164.                         System.out.println("Checking name conflict: ");
  165.                         System.out.println("The parameter of method: " + instanceVal);
  166.                         System.out.println("The instance variable of inner class: " + this.instanceVal);
  167.                         System.out.println("The instance variable of enclosing class:" + Outer.this.instanceVal);
  168.                        
  169.                         //try to modify the the value of enclosing class variable
  170.                         instanceOuterInfo = "haha";
  171.                         System.out.println(instanceOuterInfo);
  172.                        
  173.                 }
  174.         }
  175. }
复制代码
回复 使用道具 举报

评分

参与人数 1技术分 +2 收起 理由
朱神必 + 2

查看全部评分

回复 使用道具 举报
外部类{

     内部类{}


就是这样

评分

参与人数 1技术分 +1 收起 理由
朱神必 + 1

查看全部评分

回复 使用道具 举报
new InterfaceClass()
{
   //代码块
    // InterfaceClass是接口,抽象类时,实现抽象方法,Override这个类的其它方法
    //InterfaceClass是普通类时,Override这个类的方法
}



enum Lamp {
         //生成一个匿名类
        YELLEW {
                @Override
                public void sayColor() {
                        System.out.println("");               
                }
        },
         //再生成一个匿名类
        RED
        {
                public void sayColor() {
                        System.out.println("");                       
                }
        }
        ;
        //抽象方法
        public abstract void sayColor();     
}






评分

参与人数 1技术分 +1 收起 理由
朱神必 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马