A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 程玉习 中级黑马   /  2014-1-3 20:20  /  726 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 程玉习 于 2014-1-3 20:21 编辑

这个在主函数中怎么创建对象访问内部类?

  1. class Outer
  2. {
  3.         float f1 = 14.5f;
  4.         class Inner
  5.         {
  6.                 float f2 = 25.6f;
  7.                 void getNum()
  8.                 {
  9.                         System.out.println(f1);
  10.                 }
  11.         }
复制代码


评分

参与人数 1技术分 +1 收起 理由
田磊阳 + 1

查看全部评分

5 个回复

倒序浏览
同学你好,你可以这么创建:
回复 使用道具 举报
  //先创建一个能访问内部类的对象
  Outer.Inner oi = new Outer().new Inner();

  //再调用方法即可
  oi.getNum();
回复 使用道具 举报
快乐的黑马 发表于 2014-1-3 20:24
//先创建一个能访问内部类的对象
  Outer.Inner oi = new Outer().new Inner();

谢谢 望多交流指点
回复 使用道具 举报
看代码
  1. public class InnerClass
  2. {

  3.         public static void main(String[] args)
  4.         {
  5.                 new Outer().new Inner().getNum();

  6.         }

  7. }

  8. class Outer
  9. {
  10.         float f1 = 14.5f;
  11.         class Inner
  12.         {
  13.                 float f2 = 25.6f;
  14.                 void getNum()
  15.                 {
  16.                         System.out.println(f1);
  17.                 }
  18.         }
  19. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
田磊阳 + 1

查看全部评分

回复 使用道具 举报

Thank you!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马