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

编程入门 行业动态 更新时间:2024-10-18 23:31:07
是否需要在Java中覆盖抽象方法?(Work around the need to override abstract methods in Java?)

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

起初我试图省略未在特定子类中使用的抽象方法,但我很快发现由于抽象方法没有被覆盖而导致错误。 我需要做的就是用适当的方法来解决这个问题......

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

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

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类和函数......即没有代码):

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没有品种或性别功能,所以你不必为它做任何事情。 同样,猫和狗也扩展动物,但现在他们必须覆盖品种和性别。

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

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

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

我个人推荐选择A.

You have two solutions here.

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(){} }

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.

I recommend choice A, personally.

更多推荐

本文发布于:2023-07-26 15:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1277489.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:抽象   方法   Java   Work   methods

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!