-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
}
}
爲什麼不簡單地'汽車=新車();' - 然後使用你的汽車變量?你的問題讓我感到困惑,因爲我不知道你遇到了什麼問題。 –
'this.add(new Car());' – 4castle
如果'Car'不需要訪問'VehiclePanel'的狀態,可以考慮將其設置爲['static'嵌套類](https://docs.oracle。 COM/JavaSE的/教程/ JAVA/javaOO/nested.html)。 – 4castle