无法在java中创建内部类对象

时间:2017-08-27 10:36:40

标签: java

我无法在java中创建内部类对象:

package OOO;

class Car{
    class Engine{

        void display() {
            System.out.println("this is inner diaplay() method");
        }

    }
}

public class Sample8InnerClassCar {
    Car c = new Car();
    Car.Engine e = c.new Car.Engine();
}

它给我一个错误:无法分配成员类型Car.Engine。有人可以帮我理解更多吗?

1 个答案:

答案 0 :(得分:7)

正确的语法是:

Car.Engine e = c.new Engine();

完整的代码段:

package OOO;

class Car {
    class Engine {

        void display() {
            System.out.println("this is inner diaplay() method");
        }
    }
}

public class Sample8InnerClassCar {

    Car c = new Car();
    Car.Engine e = c.new Engine();
}