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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 冯国强 中级黑马   /  2013-12-18 12:28  /  1204 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

public class Test5 {
       
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                //定义类B对象,并调用类B中的方法func
                A.B b=new A。B();
                b.func();
        }
   
}
//定义类A
class A{
        int x=1;
        //定义内部类B
        class B{
                int x=2;
                void func()
                {
                        int x=3;
                        System.out.println(x);//输出B类方法func中的变量x
                        System.out.println(this.x);//输出B类中的公有变量x
                        System.out.println(A.this.x);//输出A类中的公有变量x
                }
        }
}
在主函数中实例化对象时报错,为什么?

评分

参与人数 1黑马币 +5 收起 理由
乔兵 + 5

查看全部评分

7 个回复

倒序浏览
我想你的错是格式上的问题,如下:
  1. A.B b = new A().new B();
复制代码


对比一下你的创建对象的格式,你就懂了
回复 使用道具 举报
public class Test5 {
        
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                //定义类B对象,并调用类B中的方法func
                A.B b=new A().new B();
                b.func();
        }
   
}
//定义类A
class A{
        int x=1;
        //定义内部类B
        class B{
                int x=2;
                void func()
                {
                        int x=3;
                        System.out.println(x);//输出B类方法func中的变量x
                        System.out.println(this.x);//输出B类中的公有变量x
                        System.out.println(A.this.x);//输出A类中的公有变量x
                }
        }
}
回复 使用道具 举报
  1. public class Test5 {
  2.         public static void main(String[] args) {
  3.         // TODO Auto-generated method stub
  4.         //定义类B对象,并调用类B中的方法func
  5.         A.B b=new A(). new B();//错误分析:不管是内部类   还是普通类   只要不是static修饰,要调用它里面的方法的时候,都需要每一个实例化出一个它的对象
  6.                                                         //A.B b=new A.B();这种语法不存在在java语言中,我想其他语言中估计也没有吧
  7.         b.func();
  8. }
  9. }
  10. class A{
  11.     int x=1;
  12.     //定义内部类B
  13.     class B{
  14.             int x=2;
  15.             void func()
  16.             {
  17.                     int x=3;
  18.                     System.out.println(x);//输出B类方法func中的变量x
  19.                     System.out.println(this.x);//输出B类中的公有变量x
  20.                     System.out.println(A.this.x);//输出A类中的公有变量x
  21.             }
  22.     }
  23. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
乔兵 + 1

查看全部评分

回复 使用道具 举报
楼上正解
回复 使用道具 举报
new A().B()一看后面的B()以为是A类中的一个方法
回复 使用道具 举报
Kyle 中级黑马 2013-12-18 13:48:33
7#
板凳基本说的都对,但有一点我不赞同

//A.B b=new A.B();这种语法不存在在java语言中,我想其他语言中估计也没有吧

这种语法在Java当中是存在的。
当B被静态关键字修饰的时候,就可以用这种方式创建内部类对象。
代码验证如下:
  1. public class Test {
  2.         public static void main(String[] args) {
  3.                 // 定义类B对象,并调用类B中的方法func
  4.                 A.B b = new A.B(); //被静态修饰过的内部类,用这种方式来创建对象
  5.                 b.func();
  6.         }
  7. }

  8. class A {
  9.         int x = 1;

  10.         // 定义内部类B,并用static修饰
  11.         static class B {
  12.                 int x = 2;

  13.                 void func() {
  14.                         int x = 3;
  15.                         System.out.println(x);// 输出B类方法func中的变量x
  16.                 }
  17.         }
  18. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
乔兵 + 1

查看全部评分

回复 使用道具 举报
如果内部类未声明为static,在实例化时首先需要new一个外部类的对象。并通过new A().new B()的方式new 内部类,表明这个内部类指向该外部类。内部类的class类型为:A.B。
静态内部类与普通内部类的区别在于,静态内部类的对象是不指向与某个具体的外部类对象,所以在创建对象时不需要创建外部类对象。并且在new的时候是通过 new A.B()方式。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马