首页 > 编程语言 > 详细

【java in think】嵌套类

时间:2015-03-23 17:27:25      阅读:111      评论:0      收藏:0      [点我收藏+]
 1 public class Parce5
 2 {
 3 
 4     private static class ParcelContents implements Contents
 5     {
 6 
 7         private int i = 11;
 8 
 9         @Override
10         public int value()
11         {
12             return i;
13         }
14 
15     }
16     protected static class ParcelDestination implements Destination
17     {
18         private String label;
19         
20         private ParcelDestination(String whereTo)
21         {
22             label=whereTo;
23         }
24 
25         @Override
26         public String readLabel()
27         {
28             return label;
29         }
30         
31         public static void f(){}
32         static int x=10;
33         static class AnotherLevel
34         {
35             public static void f(){}
36             static int x=10;
37         }
38         
39     }
40     public static Destination getDestination(String s)
41     {
42         return new ParcelDestination(s);
43     }
44     public static Contents getContents()
45     {
46         return new ParcelContents();
47     }
48     public static void main(String[] args)
49     {
50         Contents contents = getContents();
51         Destination destination = getDestination("泥煤紫的");
52 
53     }
54 
55 }

在main()方法中,没有任何Parce5的对象时必须的,而是使用选取static成员的普通语法来调用方法--这些方法返回对Contents和Destination的引用。

在一个普通的(非static)内部类中,通过一个特殊的this引用可以链接到期外围类对象。嵌套类没有这个特殊的this引用,使得它类似于一个static方法。

接口内部的类

正常情况下,不能再接口内部放置任何代码,但是嵌套类可以作为接口的一部分,你放到接口中的任何类都自动的是public和static的。因为类似static,只是将嵌套类置于接口的命名空间内,这并不违反接口的规则。你甚至可以在内部类中实现其外围接口。如下:

 1 public interface ClassInInterface
 2     {
 3         void howdy();
 4         class Test implements ClassInInterface
 5         {
 6 
 7             @Override
 8             public void howdy()
 9             {
10                 System.out.println("howdy()被调用啦!");
11             }
12             public static void main(String[] args)
13             {
14                 new Test().howdy();
15             }
16         }
17     }

 

如何你想要创建某些公共代码,使得它们可以被某个接口的所有实现所公用,那么使用接口内部的嵌套类会更方便。

用来测试你写的类,也可以使用嵌套类来测试。测试好了便可删除,非常方便。

从多层嵌套类中访问外部类的成员:

 1 class MNA
 2     {
 3         private void f()
 4         {
 5         }
 6 
 7         class A
 8         {
 9             private void g()
10             {
11             }
12 
13             public class B
14             {
15                 void h()
16                 {
17                     g();
18                     h();
19                 }
20             }
21         }
22     }
23 
24     public class Test
25     {
26         public static void main(String[] args)
27         {
28             MNA mna = new MNA();
29             MNA.A a = mna.new A();
30             MNA.A.B b = a.new B();
31             b.h();
32         }
33     }

 

【java in think】嵌套类

原文:http://www.cnblogs.com/linlin-meimei/p/4360314.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!