我无法在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。有人可以帮我理解更多吗?
答案 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();
}