解决需要覆盖 Java 中的抽象方法的问题吗? [英] Work around the need to override abstract methods in Java?

查看:29
本文介绍了解决需要覆盖 Java 中的抽象方法的问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在做一个作业,它涉及一个代表通用动物的抽象类,还有猫、狗、爬行动物等的子类.超类具有抽象方法每个子类实际上都没有使用这些方法. 例如,有狗和猫使用的品种和性别的访问器和变异器,而不是爬行动物类.我个人认为这是一个奇怪的设置,但这是作业所要求的.

I've been working on an assignment that involves an abstract class that represents a generic animal, with subclasses for cat, dog, reptile, etc. The superclass has abstract methods that aren't actually used by each subclass. For example, there are accessors and mutators for breed and gender that are used by dog and cat but not the reptile class. I personally think this is an odd setup, but it's what is required by the assignment.

一开始我尝试忽略特定子类中未使用的抽象方法,但我很快发现这会导致错误,因为抽象方法没有被覆盖.我所要做的就是将其放入适当的方法中进行修复...

At first I tried leaving out the abstract methods not used in a particular subclass, but I quickly found out that leads to an error, due to abstract methods not being overridden. All I had to do to fix that was put in the appropriate methods...

public class Reptile extends Animal
...
public void setBreed(String newBreed){}  
public String getBreed(){return null;}  


...虽然看起来有点浪费,因为它们没有被使用.这不是一个大问题,但它让我很好奇是否有替代方法来覆盖给定子类中未使用的抽象方法.除此之外,当您需要覆盖抽象方法但实际上根本不会使用该方法时,是否有任何约定?


...though it seems to be a bit of a waste seeing as they aren't used. It's not a huge issue, but it made me curious as to whether or not there is an alternative to having to override unused abstract methods in a given subclass. Barring that, is there any convention for what to do when you need to override an abstract method but won't actually be using the method at all?

推荐答案

这里有两个解决方案.

您可以有多个级别的抽象类.换句话说(这些是 shell 类和函数......即没有代码):

You can have multiple levels of abstract classes. In other words (these are shell classes and functions...i.e. no code):

public abstract class Animal{
  public abstract void fooMethod();
}

public abstract class Pet extends Animal{
  public abstract void breed();
  public abstract void gender();
}

public class Reptile extends Animal{
  public void fooMethod(){}
}
public class Cat extends Pet{ //do the same for Dog
  public void fooMethod(){}
  public void breed(){}
  public void gender(){}
}

这样,Reptile 仍然继承了 Animal,但您知道 Reptile 没有品种或性别功能,因此您不必为它做任何事情.同样,Cat 和 Dog 也扩展了 Animal,但现在它们具有必须覆盖的品种和性别.

This way, Reptile still extends Animal, but you know Reptile doesn't have a breed or gender function, so you don't have to do anything for it. Similarly, Cat and Dog also extend Animal, but now they have breed and gender they must override as well.

你的另一个解决方案是做这样的事情(两个解决方案中最糟糕的一个):

Your other solution is to do something like this (the worse of the two solutions):

public class Reptile extends Animal{
  public void breed(){
    throw new UnsupportedOperationException("Don't call Reptile#breed!!");
  }
}

这个解决方案特别糟糕,因为如果你没有适当的错误处理,这个异常会一直向上传播到你的主方法,如果没有异常处理一直到你的主方法方法,那么你的程序就会崩溃.

This solution is particularly bad, because if you don't have proper error handling, this exception will propagate all the way up the stack to your main method, and if there is not exception handling for this all the way to your main method, then your program crashes.

我个人推荐选择 A.

这篇关于解决需要覆盖 Java 中的抽象方法的问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆