2016-09-25 139 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,事實上,它是私有的無所謂。

相關問題