Java父级';子构造函数中的私有属性
标题说明了一切,我得到了一个类,其中构造函数的变量必须是私有的Java父级';子构造函数中的私有属性,java,parent-child,downcast,Java,Parent Child,Downcast,标题说明了一切,我得到了一个类,其中构造函数的变量必须是私有的 public class AdMedia { private String name; private int price; public AdMedia(){} public AdMedia(String name, int price) { this.name = name; this.price = price; } 这当然是变量的公共gettersetter 在我尝试制作一个名为Magazine的儿童
public class AdMedia {
private String name;
private int price;
public AdMedia(){}
public AdMedia(String name, int price) {
this.name = name;
this.price = price;
}
这当然是变量的公共getter
setter
在我尝试制作一个名为Magazine的儿童类之后,问题就出现了。类应该继承名称和价格,但是价格对于每个对象初始化都是常量。因此它们不会作为名称出现在构造函数上
public class Magazine extends AdMedia {
private int area;
private String position;
private String topics;
public Magazine() {}
public Magazine(String name, int size, String position, String topic){
super();
this.size = size;
this.position = position;
this.topic = topic;
}
它也有自己的getter
setter
我试图将价格放入构造函数中,但构造函数要求传递一个参数。使用super(name)
也会通知父构造函数都没有这样的形状
当我试图使用父类方法getName()
获取name
时,这会使我变得复杂,我想这可能需要一些向下转换
我曾尝试搜索解决方案,但大多数都要求我将变量的可访问性更改为受保护
。在private
中是否没有其他方法可以执行此操作
编辑:
我忘了提到我上面写的操作的结果是无法访问杂志名称,因此当我尝试向下转换获取名称时,返回的是空值。您可以将您的子构造函数编写为
public Magazine(String name, int size, String position, String topic){
super();
setName(name);
setPrice(100); // 100 is your constant price
this.size = size;
this.position = position;
this.topic = topic;
}
或作为
但是,这两种方式都可能在以后改变价格:
Magazine m = new Magazine("name", 50, "position", "topic");
m.setPrice(10);
如果需要防止这种情况,还应覆盖setPrice()
setter:
public class Magazine extends AdMedia {
...
@Override
public void setPrice(int price) {
// what to do here?
// * you could either silently ignore
// (which might surprise some users expecting to be able to change the price)
// * throw an UnsupportedOperationException
// (which might surprise other users which are not prepared to handle such an exception)
}
}
您可以将子构造函数编写为
public Magazine(String name, int size, String position, String topic){
super();
setName(name);
setPrice(100); // 100 is your constant price
this.size = size;
this.position = position;
this.topic = topic;
}
或作为
但是,这两种方式都可能在以后改变价格:
Magazine m = new Magazine("name", 50, "position", "topic");
m.setPrice(10);
如果需要防止这种情况,还应覆盖setPrice()
setter:
public class Magazine extends AdMedia {
...
@Override
public void setPrice(int price) {
// what to do here?
// * you could either silently ignore
// (which might surprise some users expecting to be able to change the price)
// * throw an UnsupportedOperationException
// (which might surprise other users which are not prepared to handle such an exception)
}
}
这回答了你的问题吗?什么是价格的不变值?是否由实例化new Magazine()的用户设置@DeanDebrio@QuickSilver它应该按打印量计算,主要是一个变量,乘以100英镑或100英镑something@WoAiNii我理解private意味着唯一可以访问变量的只有类、父类本身,并且不能被继承,除非使用父类的getter setter。实际上,我对我需要将privates用于不接受它的参数(price变量)的构造函数的部分感到困惑。这是否回答了你的问题?什么是价格的不变值?是否由实例化new Magazine()的用户设置@DeanDebrio@QuickSilver它应该按打印量计算,主要是一个变量,乘以100英镑或100英镑something@WoAiNii我理解private意味着唯一可以访问变量的只有类、父类本身,并且不能被继承,除非使用父类的getter setter。实际上,我对需要将privates用于不接受参数的构造函数(“price”变量)的部分感到困惑。不过,整个代码都是固定的。我以前从来没有用过这样的超级。谢谢没关系,整个代码都修复了。我以前从来没有用过这样的超级。谢谢