免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 999 | 回复: 0
打印 上一主题 下一主题

non-static class [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-06-23 16:30 |只看该作者 |倒序浏览
非静态内部类:
特点:
内部类不能够使用静态成员
内部类能够访问外部类的所有属性

继续看例子:
package self.study.test;
public class Outer {
int i=100;
int j=200;
static int a=300;
void sayhello(){
  Inner inner=new Inner();  //需要实例化
  System.out.println(inner.m+inner.n);
}
class Inner{
  int m=1;
  int n=2;
  //static int b=12; //error
  void f1(){
   System.out.println(i+j);
   System.out.println(m+n);
   System.out.println(a+i);
   System.out.println(a+m);
   //System.out.println(a+b);
  }
  void f2(){
   sayhello3();  //直接调用
  }
}
void sayhello2(){
  Inner inner=new Inner();
   inner.f1();
}
void sayhello3(){
  System.out.println("test hello");
}
public static void main(String args[]){
  Outer out=new Outer();
  out.sayhello();
  out.sayhello2();
  out.sayhello3();
  
}
}

上面体现的是 内部类调用外部成员  外部调用内部属性和方法的过程以及解决方法
下面看一个例子 怎么让一个外部类调用内部类的信息
eg:
package self.study.test;
import self.study.test.Outer.Inner;;
public class OuterTest {
public static void main(String args[]){
Outer out=new Outer();
out.sayhello();
out.sayhello2();
out.sayhello3();
Inner inner=out.new Inner();
inner.f1();
inner.f2();
}
}

在另外的类调用内部类的成员的过程

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/40487/showart_327136.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP