天之道

享受编程的乐趣。
posts - 118, comments - 7, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

浅谈Java内部类

Posted on 2013-09-28 14:26 hoshelly 阅读(223) 评论(0)  编辑 收藏 引用 所属分类: Java

什么是内部类呢?顾名思义,内部类就是在类中可以定义另一个的类。内部类是外部对象的一个成员,同样具有public , private 和 protected的访问权限。

public class Test
{
    public static void main(String[] args)
    {
      Family my = new Family(“Jonh",130);
      my.getMsg();
    }
}
class Family
{
     Family(String name,int weight){ this.f = new Father(name,weight); }
     private class Father
     {
       private int weight;
       private String name;
       Father(String n,int w)
       {
          this.name = n;
          this.weight = w;
       }
       public int getWeight()
       {
          return weight;
       }
       public String getName()
       {
          return name;
       }
      }

     private Father f;
     public void getMsg()
     {
          System.out.println("Name: "+f.getName()+"\nWeight: "+f.getWeight());
     }
}
类Family 中有一个Father类,声明为private,表明在Family类中可以创建一个实例对象,这个Father专属于Family类。普通的(非内部)类不能声明为private或者protected,只能声明为public。因此这样的代码是不被允许的:
Family.Father a = my.new Father("Jonh",150);

如果要使用上述代码,只要修改内部类的访问权限就可以了,如去掉private权限,则默认为包访问权限,同一个包中的类都可以访问它,而不是只能从Family类才能访问。

public class test
{
    public static void main(String[] args)
    {
      Family my = new Family("Jonh",130);
      my.getMsg();
      Family.Father a = my.new Father("Jonh",150);
      System.out.println("Name: "+a.getName()+"\nWeight: "+a.getWeight());
     
    }
}
在上述代码中,在创建内部类对象时,语法为:Family.Father a = my.new Father("Jonh",150); 说明内部类对象的创建是基于一个外部类对象(my),也就是说内部类对象必须依附于一个外部类对象。

内部类可以出现在所属类的方法内或任意作用域内。像下面的代码将内部类嵌入在方法内:

public class test
{
    public static void main(String[] args)
    {
      Family my = new Family();
      my.getMsg("Jonh",150);
     
    }
}
class Family
{
     Family(){ }
     public void getMsg(String s,int n)
     {
       class Father
      {
       private int weight;
       private String name;
       Father(String n,int w)
       {
          this.name = n;
          this.weight = w;
       }
       public int getWeight()
       {
          return weight;
       }
       public String getName()
       {
          return name;
       }
      }
      Father f = new Father(s,n);
      System.out.println("Name: "+f.getName()+"\nWeight: "+f.getWeight());
     }
}


闭包

内部类可以访问外部类的成员变量(即使是private),如在Family类中添加成员变量height,在Father类中定义一个方法 getHeight(),则能成功访问这个height变量,而在外部类中不能访问内部类中的成员变量。


class Family
{
     private int height = 180; /*定义外部类属性height*/
     Family(String name,int weight){ this.f = new Father(name,weight); }
     private class Father
     {
       private int weight;
       private String name;
       Father(String n,int w)
       {
          this.name = n;
          this.weight = w;
       }
       public int getWeight()
       {
          return weight;
       }
       public String getName()
       {
          return name;
       }
       public int getHeight()
       {
          return height;   /*访问外部类的属性height*/
       }
      }

     private Father f;
     public void getMsg()
     {
          System.out.println("Name: "+f.getName()+"\nWeight: "+f.getWeight()+"\nHeight: "+f.getHeight());
     }
}
public class test
{
    public static void main(String[] args)
    {
      Family my = new Family("Jonh",130);
      my.getMsg();
        /* not allowed */
  /* System.out.println(my.weight); */
    }
}

如果把内部类单独拿到外面来声明,那么要使用外部类的属性如height,就要先创建外部类的对象,再由对象调用其height属性,现在由于内部类处于外部类中,所以在程序调用时不必再创建外部类的对象,直接就可以使用height,这样减少了一部分内存空间的开销。

嵌套static类

在类内部定义static类,称为嵌套static类。
我们可以直接创建嵌套类,而不必依赖于某个外部类对象。嵌套类无法调用外部对象的方法、也无法读取或修改外部对象的数据。
如:
public class Test{
    public static void main(String[] args){
        Father.Son John = new Father.Son();
        John.display();
     }
}

class Father{
  /*嵌套类*/
 static class Son{
     public void display(){
        System.out.println("I am his son.");
     }
   }
}

总结
1. 内部类丰富了类的组织形式;
2. 内部类实现了闭包。




































只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理