简体   繁体   English

如何解决从子 class 为其父调用方法?

[英]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.

相关问题 为什么对父 class 构造函数的调用不调用在子 class 中覆盖的父方法? - Why does the call to a parent class constructor not call the parent's methods that are overriden in the child class? 如何使用子 class 名称调用不同父接口中的方法? - How to call methods in different parent interfaces using child class name? 如何在JAVA中从父类的内部类中调用子类中的重写方法? - How can I call the overridden method in a child class from a parent class's inner class in JAVA? Parent可以调用Child Class方法吗? - Can a Parent call Child Class methods? 从Java中的父列表调用子方法 - Call child methods from parent list in Java Java:从父级访问Child类方法 - Java: accessing Child class methods from parent 有没有办法从java中的子类对象调用父类方法而不修改方法 - is there any way to call parent class method from child class object in java without modifying methods 从子类调用父类中的方法 - Call method in parent class from child class 从子类外部访问父类的受保护方法(使用反射或任何可行的方法) - Accessing a parent class's protected method from outside a child class (with reflection or anything that would work) 如何从单独的类中调用活动的方法 - How to call an activity's methods from a separate class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM