2016-09-25 144 views
-2

我正在寻找一个名为VehiclePanel的构造函数中的Car实例,但我似乎无法弄清楚如何去做,所以在这里。有人能够帮助吗?在Java的公共类中创建一个私有类的实例

public class VehiclePanel extends JPanel { 
//variables here 

public VehiclePanel() { 
// somehow need to create a new instance of Car class and add it to the VehiclePanel 


} 


private class Car extends JPanel { 
// Car code here, not important 
} 
} 
+4

为什么不简单地'汽车=新车();' - 然后使用你的汽车变量?你的问题让我感到困惑,因为我不知道你遇到了什么问题。 –

+1

'this.add(new Car());' – 4castle

+2

如果'Car'不需要访问'VehiclePanel'的状态,可以考虑将其设置为['static'嵌套类](https://docs.oracle。 COM/JavaSE的/教程/ JAVA/javaOO/nested.html)。 – 4castle

回答

0
public VehiclePanel() { 
    Car car = new Car(); 
    add(car); 
} 

注意,由于汽车被宣布里面VehiclePanel,事实上,它是私有的无所谓。

相关问题