[英]How to work around to call methods from a child class for it's parent?
If I add a Detective as a Book, how do I call the setPrice
method (because you just can't call a child method for a parent class)?如果我将 Detective 添加为 Book,如何调用setPrice
方法(因为您不能为父类调用子方法)?
This is the code:这是代码:
public class Book {
String title;
//Contructors, get/setters, Override output methods
}
public class Detective extends Book {
int price;
//Contructors, get/setters, Override output methods
}
public class BookManager {
Book[] list;
int count = 0;
final int MAX = 100;
//Contructors, get/setters, Override output methods
public void add(Book x) {
if(count >= MAX) {
System.out.println("Failed!");
}
list[count] = x;
count++;
System.out.println("Added!");
}
public void updatePrice(String title, int newPrice) {
for(int i = 0; i < count; i++) {
if(list[i].equals(title) && list[i] instanceof Detective) {
//list[i].setPrice(newPrice) is wrong//
}
}
}
}
public static void main(String[] args) {
BookManager list = new BookManager();
Detective de = new Detective("abc", 123);
list.add(de);
//list.updatePrice("abc", 456); is wrong//
}
Is there another way to update the price?还有其他方法可以更新价格吗?
Some options, depends how the data should be modeled.一些选项,取决于数据应该如何建模。
1 - just use a cast to Detective
to use its methods: 1 - 只需对Detective
使用强制转换即可使用其方法:
if (list[i].equals(title) && list[i] instanceof Detective) {
Detective dectective = (Detective) list[i];
detective.setPrice(newPrice);
2 - shouldn't every Book have a price? 2 - 每本书不应该有价格吗?
public class Book {
String title;
//Contructors, get/setters, Override output methods
public void setPrice(int price) {
...
}
}
now it's trivial to call it:现在调用它很简单:
// instanceof not need here for this to work
if (list[i].equals(title) && list[i] instanceof Detective) {
list[i].setPrice(newPrice);
eventually the method is empty in Book
but overridden in Detective
最终该方法在Book
中为空,但在Detective
中被覆盖
public class Book {
...
public void setPrice(int price) {
// intentionally empty, overridden in aubclasses
}
}
public class Detective extends Book {
...
@Override
public void setPrice(int p) {
...
}
}
3 - one step further, assuming there is no just-a-Book, that is, only subclasses of Book
: make the class and the method abstract
: 3 - 更进一步,假设没有 just-a-Book,即只有Book
的子类:使 class 和方法abstract
:
public abstract class Book { // maybe make this an interface
...
public abstract void setPrince(int p);
}
and each subclass must implement that method并且每个子类都必须实现该方法
public class Detective extends Book {
...
@Override
public void setPrice(int p) [
...
}
}
and calling as in并调用
if (list[i].equals(title) && list[i] instanceof Detective) {
list[i].setPrice(newPrice);
This does not allow creation of book as in new Book(...)
;这不允许像在new Book(...)
中那样创建书; to create a book, only subclasses are allowed, eg Book book = new Detective(...)
要创建一本书,只允许使用子类,例如Book book = new Detective(...)
What i usually do is define an interface that the parent implements and the child can call.我通常做的是定义一个父实现和子可以调用的接口。
Interface:界面:
public interface IBookListener {
void updatePrice (String title, int newPrice);
}
In parent:在父母中:
public class BookManager implements IBookListener {
public void add(Book x) {
x.setListener(this);
...
}
...
public void updatePrice (String title, int newPrice) {
...
}
}
In child:在儿童中:
public class Book {
...
private IBookListener listener;
public setLister(IBookListener listener) {
this.listener = listener;
}
public someMethod () {
listener.updatePrice("title", 1);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.