简体   繁体   English

尽管子类中也有相同的方法,如何阻止子类对象从父类调用方法

[英]How to stop child class object invoking method from parent class despite same method also being in child class

I have an object of type ElectronicProduct which extends Product, I try to invoke the loadFromFile() method from within the child class, giving the exact name and argument types, yet instead the child class object invokes the loadFromFile() from the parent class.我有一个扩展 Product 的 ElectronicProduct 类型的对象,我尝试从子类中调用 loadFromFile() 方法,给出确切的名称和参数类型,但子类对象从父类调用 loadFromFile() 。 How do I stop this?我该如何阻止?

The code is as follows:代码如下:

ElectronicProduct eProduct = new ElectronicProduct();
eProduct.loadFromFile(eProduct,bin); 

however it invokes the method in parent class below:但是它调用下面父类中的方法:

public Product loadFromFile(Product aProduct, BufferedReader bin) throws IOException, ParseException{ 

   //some code 

}

instead of the child class shown below:而不是下面显示的子类:

public ElectronicProduct loadFromFile(Product aProduct, BufferedReader bin) throws IOException, ParseException{

// Some code

}

I did try using @Override in the child class method, but because one returns a product and the other returns an electronicProduct the override does not seem to work.我确实尝试在子类方法中使用 @Override,但是因为一个返回一个产品而另一个返回一个电子产品,所以覆盖似乎不起作用。 Any help would be greatly appreciated.任何帮助将不胜感激。

EDIT编辑

This is very strange as now it has started working as I wanted it to, without changing anything.这很奇怪,因为现在它已经开始按照我想要的方式工作,没有改变任何东西。 Different scenario but a similar thing happened before where it gives an error warning, i delete the code and write the exact same thing, and then it works.不同的场景但类似的事情发生在它给出错误警告之前,我删除了代码并编写了完全相同的东西,然后它就可以工作了。 I do not want this to happen randomly as it is a graded assignment.我不希望这种情况随机发生,因为这是一项分级作业。 Do not want to go off topic but it must be related.不想跑题,但一定是相关的。 Please advise if anyone knows what has just happened?请告知是否有人知道刚刚发生了什么?

EDIT*2编辑*2

In response to comment from Tiffado, the reason a Product object is passed in and not an ElectronicProduct object, is because within the child class method, the Product object is passed to the parent class.作为对来自 Tiffado 的评论的回应,传递 Product 对象而不是 ElectronicProduct 对象的原因是因为在子类方法中,Product 对象被传递给父类。 If I instead pass in a child class object into the child method which then passes the object to a parent method it may not work, as I have learned that parent classes do not 'see' child classes.如果我改为将子类对象传递到子方法中,然后子方法将对象传递给父方法,则它可能不起作用,因为我了解到父类不会“看到”子类。 Please correct me if this is incorrect.如果这是不正确的,请纠正我。

public ElectronicProduct loadFromFile(Product aProduct, BufferedReader bin) throws IOException, ParseException{


ElectronicProduct eProduct = new ElectronicProduct();

eProduct = (ElectronicProduct)super.loadFromFile(aProduct,bin);

eProduct.setGuarunteeDuration(Integer.valueOf(bin.readLine()));

return eProduct;

}

There is no way for the compiler to know the difference between your two methods: You need to specify different parameter ordering at the method signature, number of parameters, but no subclass parameter types or subclass return types will make a difference:编译器无法知道您的两种方法之间的区别:您需要在方法签名、参数数量处指定不同的参数顺序,但没有子类参数类型或子类返回类型会产生影响:

The method:方法:

public Product loadFromFile(Product aProduct, BufferedReader bin) throws IOException, ParseException{ 

Is just the same as是一样的

public ElectronicProduct loadFromFile(Product aProduct, BufferedReader bin) throws IOException, ParseException{

since the return type (ElectronicProduct) is a subclass of Product, so it is not part of the method signature to use polymorphism.由于返回类型 (ElectronicProduct) 是 Product 的子类,因此它不是使用多态的方法签名的一部分。

See also: "Java method signatures only take method name and number and type of parameters", available at: https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html另请参阅:“Java 方法签名仅采用方法名称以及参数的数量和类型”,位于: https : //docs.oracle.com/javase/tutorial/java/javaOO/methods.html

Please see the next example, which, by overriding rules, will call the child method, but since there's a hardcoded call to the parent , it will also call the parent:请参阅下一个示例,通过覆盖规则,它将调用 child 方法,但由于对 parent 进行了硬编码调用,因此它也将调用 parent:

public class Product {

  static int productIdentifier = 0;
  static int electronicProductIdentifier = 0;

    public Product loadFromFile( Product aProduct ) {

    System.out.println("Call loadFromFile from Product");
        Product product = new Product();
        return product;

    }

  public Product ()  {
    System.out.println("Creating a Product with ID " + 
            ( ++ productIdentifier ) );
  }

}


public class ElectronicProduct extends Product {

    public ElectronicProduct loadFromFile( Product aProduct ) {

    System.out.println("Call loadFromFile from ElectronicProduct");
        ElectronicProduct eProduct = new ElectronicProduct();
    super.loadFromFile( eProduct ); // Hardcoded call to parent!
        return eProduct;

    }

  public ElectronicProduct ()  {
    System.out.println("Creating an ElectronicProduct with ID " + 
            ( ++ electronicProductIdentifier ) );
  }

}

Product p = new Product();
ElectronicProduct ep = new ElectronicProduct();
ep.loadFromFile( ep );

Output:输出:

Creating a Product with ID 1
Creating a Product with ID 2
Creating an ElectronicProduct with ID 1
Call loadFromFile from ElectronicProduct
Creating a Product with ID 3
Creating an ElectronicProduct with ID 2
Call loadFromFile from Product
Creating a Product with ID 4

(There's a call of loadFromFile() from product, since it is being explicitly called from ElectronicProduct.loadFromFile() ) (有一个来自产品的 loadFromFile() 调用,因为它是从 ElectronicProduct.loadFromFile() 显式调用的)

If you comment out the line calling to super.loadFromFile() , you'll see no call to Product.loadFromFile() :如果您注释掉调用super.loadFromFile() ,您将看不到对Product.loadFromFile()调用:

Creating a Product with ID 1
Creating a Product with ID 2
Creating an ElectronicProduct with ID 1
Call loadFromFile from ElectronicProduct
Creating a Product with ID 3
Creating an ElectronicProduct with ID 2

Maybe you should use a method like this one :也许你应该使用这样的方法:

public ElectronicProduct loadFromFile(ElectronicProduct aProduct, BufferedReader bin) throws IOException, ParseException{

Since the main difference is the object you put in the parameters, you should specify it.由于主要区别在于您放入参数的对象,因此您应该指定它。

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

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