是否需要在Java中覆盖抽象方法? [英] Work around the need to override abstract methods in Java?

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

问题描述

我一直致力于一项涉及代表一般动物的抽象类的赋值,其中包含cat,dog,reptile等的子类。超类具有抽象方法,每个子类实际上并未使用它们。例如,有狗和猫使用的品种和性别的存取者和变异者,但不是爬行动物类。我个人认为这是一个奇怪的设置,但这是作业所需要的。

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!!");
  }
}

此解决方案特别糟糕,因为如果你不喜欢如果有正确的错误处理,这个异常将一直向上传播到你的main方法,如果没有异常处理,一直到你的main方法,那么你的程序崩溃了。

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天全站免登陆