LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 980|回复: 3

有关内部类的一个小问题像请大家帮忙一下?

[复制链接]
发表于 2006-1-16 17:56:51 | 显示全部楼层 |阅读模式
我有个程序问题要请教一下。可否请各位帮忙~

  1. package test;
  2. /**
  3. * 关于多重继承的问题,实例*/
  4. public class test12 {

  5.         /**
  6.          * @param args
  7.          */
  8.         public static void main(String[] args) {
  9.                 // TODO Auto-generated method stub
  10.                 henduo hd=new henduo();
  11.                 chouxiang cx=hd.getCLASS();
  12.                 cx.ok();
  13.                 cx.no();
  14.                
  15.         }

  16. }

  17. interface A{
  18.         void print();
  19. }
  20. interface B{
  21.         void setStr(String x);
  22. }
  23. abstract class chouxiang{
  24.         public void ok(){};
  25.         public void no(){};
  26. }
  27. class henduo {
  28.         private class yiban extends chouxiang implements A,B{
  29.                 public void ok(){
  30.                         System.out.println("ok");
  31.                 }
  32.                 public void no(){
  33.                         System.out.println("no");       
  34.                 }
  35.                 public void print(){
  36.                         ok();
  37.                         no();
  38.                 }
  39.                 public void setStr(String s){}
  40.         }
  41.         public chouxiang  getCLASS(){
  42.                 return new yiban();
  43.         }
  44. }
复制代码

问题是,我想在main()中,用对chouxiang的引用来访问yiban()里的方法。但是我这样做只有ok()和no()可以被正常访问 print()和setStr()就不行了。
不知道是为什么?
发表于 2006-1-16 19:21:35 | 显示全部楼层
cx是被声明chouxiang类的,所以不能调用yiban()里的内容. 需要使用时,需要类型转换.
You may try:
A a = (A)cx; //to use the interface A's operator.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-1-16 19:28:11 | 显示全部楼层
我试过,A fff=(A)cx的没有成功~
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-1-16 19:32:57 | 显示全部楼层
我错了~, 刚刚试的时候,可以了,谢谢啊~
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表