如何在子类中设置实例变量,即使它是私有的并在超类中声明?
How to set an instance variable in subclass even though it's private and declared in superclass?
import javax.swing.ImageIcon;
public class City {
private String name;
private int population, x, y;
private ImageIcon marker;
public City(String name, int population, int x, int y) {
marker = new ImageIcon("marker_city.png");
this.name = name;
this.population = population;
this.x = x;
this.y = y;
}
public ImageIcon getMarker() {
return marker;
}
public void setMarker(ImageIcon marker) {
this.marker = marker;
}
public String toString() {
return this.name;
}
}
我需要用不同的 ImageIcon 文件创建一个子类 ("marker_prov.png")。我应该如何设置 marker
变量?这个必须在超类中保持私有。
import javax.swing.ImageIcon;
public class ProvCapital extends City {
private String province;
public ProvCapital(String name, int population, int x, int y, String province) {
super(name, population, x, y, marker);
marker = new ImageIcon("marker_prov.png");
this.province = province;
}
我想我可以使用 getter 或 setter,因为它们是 public,但我不知道如何使用。
marker = new ImageIcon("marker_prov.png");
setMarker(marker);
应该这样做吗?
我建议更改 class ProvCapital
的构造函数。删除 marker
参数,因为无论如何您都会忽略它。试试下面的构造函数。
public ProvCapital(String name, int population, int x, int y, String province) {
super(name, population, x, y, new ImageIcon("marker_prov.png"));
this.province = province;
}
编辑
实际上,我看到您在 class City
的构造函数中也忽略了 marker
参数,所以最好也放弃它。只保留 setMarker()
方法。为 class City
尝试以下构造函数
public City(String name, int population, int x, int y) {
setMarker(new ImageIcon("marker_city.png"));
this.name = name;
this.population = population;
this.x = x;
this.y = y;
}
然后如下更改 class ProvCapital
的构造函数。
public ProvCapital(String name, int population, int x, int y, String province) {
super(name, population, x, y);
setMarker(new ImageIcon("marker_prov.png"));
this.province = province;
}
在 City
中添加一个新的(受保护的)构造函数,它将 marker
作为参数:
protected City(String name, int population, int x, int y, ImageIcon marker) {
this.name = name;
this.population = population;
this.x = x;
this.y = y;
this.marker = marker;
}
理想情况下,在 public 构造函数中重用它:
public City(String name, int population, int x, int y) {
this(name, popoulation, x, y, new ImageIcon("marker_city.png"));
}
然后在子类的 super()
调用中传递选定的标记:
public ProvCapital(String name, int population, int x, int y, String province) {
super(name, population, x, y, new ImageIcon("marker_prov.png"));
this.province = province;
}
如果需要,这种方法甚至允许您删除 setter 并使 marker
最终化。
import javax.swing.ImageIcon;
public class City {
private String name;
private int population, x, y;
private ImageIcon marker;
public City(String name, int population, int x, int y) {
marker = new ImageIcon("marker_city.png");
this.name = name;
this.population = population;
this.x = x;
this.y = y;
}
public ImageIcon getMarker() {
return marker;
}
public void setMarker(ImageIcon marker) {
this.marker = marker;
}
public String toString() {
return this.name;
}
}
我需要用不同的 ImageIcon 文件创建一个子类 ("marker_prov.png")。我应该如何设置 marker
变量?这个必须在超类中保持私有。
import javax.swing.ImageIcon;
public class ProvCapital extends City {
private String province;
public ProvCapital(String name, int population, int x, int y, String province) {
super(name, population, x, y, marker);
marker = new ImageIcon("marker_prov.png");
this.province = province;
}
我想我可以使用 getter 或 setter,因为它们是 public,但我不知道如何使用。
marker = new ImageIcon("marker_prov.png");
setMarker(marker);
应该这样做吗?
我建议更改 class ProvCapital
的构造函数。删除 marker
参数,因为无论如何您都会忽略它。试试下面的构造函数。
public ProvCapital(String name, int population, int x, int y, String province) {
super(name, population, x, y, new ImageIcon("marker_prov.png"));
this.province = province;
}
编辑
实际上,我看到您在 class City
的构造函数中也忽略了 marker
参数,所以最好也放弃它。只保留 setMarker()
方法。为 class City
public City(String name, int population, int x, int y) {
setMarker(new ImageIcon("marker_city.png"));
this.name = name;
this.population = population;
this.x = x;
this.y = y;
}
然后如下更改 class ProvCapital
的构造函数。
public ProvCapital(String name, int population, int x, int y, String province) {
super(name, population, x, y);
setMarker(new ImageIcon("marker_prov.png"));
this.province = province;
}
在 City
中添加一个新的(受保护的)构造函数,它将 marker
作为参数:
protected City(String name, int population, int x, int y, ImageIcon marker) {
this.name = name;
this.population = population;
this.x = x;
this.y = y;
this.marker = marker;
}
理想情况下,在 public 构造函数中重用它:
public City(String name, int population, int x, int y) {
this(name, popoulation, x, y, new ImageIcon("marker_city.png"));
}
然后在子类的 super()
调用中传递选定的标记:
public ProvCapital(String name, int population, int x, int y, String province) {
super(name, population, x, y, new ImageIcon("marker_prov.png"));
this.province = province;
}
如果需要,这种方法甚至允许您删除 setter 并使 marker
最终化。