现在的位置: 首页 > 综合 > 正文

java内部类的试验

2013年01月03日 ⁄ 综合 ⁄ 共 621字 ⁄ 字号 评论关闭
  1. package innertest;
  2. interface product{
  3.    void price();
  4. }
  5. public class test1 {
  6.  private int i=1;
  7.  private String ts="test1";
  8.     private class shoe implements product{
  9.        public void price(){
  10.         System.out.println("i am excuted at "+ts+" innerclass!");
  11.         test();//内部类可以访问类的成员和方法
  12.        }
  13.     }
  14.     public shoe getshoe(){
  15.      return new shoe();
  16.     }
  17.     private void test(){
  18.      System.out.println(i);
  19.     }
  20.     public static void main(String[] args){
  21.      test1 t=new test1();
  22.      product p=t.getshoe();
  23.         test1.shoe s=t.new shoe();
  24.         s.price();
  25.      p.price();
  26.      
  27.      
  28.     }
  29. }
  30. 使用内部类,可以完全隐藏实现细节。某个类的内部类和其成员,方法具有同等的权利

抱歉!评论已关闭.