简体   繁体   English

在 Java 中的公共类中创建私有类的实例

[英]Making an instance of a private class in a public class in Java

I am looking to make an instance of Car in a constructor called VehiclePanel, but I can't seem to figure out how to do it, so here I am.我想在一个名为 VehiclePanel 的构造函数中创建一个 Car 实例,但我似乎无法弄清楚如何做到这一点,所以我在这里。 Is anyone able to help with this?有没有人能帮忙解决这个问题?

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
}
}
public VehiclePanel() {
  Car car = new Car();
  add(car);
}

Note that since Car is declared inside VehiclePanel, the fact that it is private doesn't matter.请注意,由于 Car 是在 VehiclePanel 中声明的,因此它是私有的这一事实并不重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM