简体   繁体   English

扩展抽象类的类

[英]Class which extends abstract class

I have a problem, i have abstract class which implements interface, and one more class which extends this first abstract class, but i got an error it says that i my second class must inherit methods from interface, and i dont want that, i also can change it to abstract class too, but then i can't call constructor from that class in some third class, how can i fix this?我有一个问题,我有一个实现接口的抽象类,还有一个扩展了第一个抽象类的类,但是我得到一个错误,它说我的第二个类必须从接口继承方法,我不想要那个,我也也可以将其更改为抽象类,但是我无法在某个第三类中从该类调用构造函数,我该如何解决这个问题? I searched for answer on internet but couldnt find it, and i dont understand abstract classes rly good so i can't solve it.Please help (ps sorry for my english, it is really bad)我在互联网上搜索了答案但找不到它,我不明白抽象类很好所以我无法解决它。请帮忙(ps 对不起我的英语,这真的很糟糕)

Edit: I have an assignment where it is requiered that my second class needs to extend abstract class, and i am implementing interface because they also gave me main class in which they are making object from constructor in my second class and then they are using it with type of interface, i dont know if this makes any sense, code is pretty long, but i will give some lines: Here is my main class (NationalTeam bulgaria = new NationalTeam("Bulgaria", Formation.F352, "Bulgaria"); nationalManager.setManagingTeam(bulgaria);) (bulgaria must be type of interface), also here is my constructor from second class which extends abstract class:(public NationalTeam(String name,Formation formation,String country){ super(name,formation); this.country=country; })编辑:我有一个任务,要求我的第二个类需要扩展抽象类,我正在实现接口,因为他们还给了我主类,他们在我的第二个类中从构造函数创建对象,然后他们正在使用它对于接口类型,我不知道这是否有意义,代码很长,但我会给出一些行:这是我的主类 (NationalTeam bulgaria = new NationalTeam("Bulgaria", Formation.F352, "Bulgaria") ; nationalManager.setManagingTeam(bulgaria);) (bulgaria 必须是接口类型),这里也是我的第二类构造函数,它扩展了抽象类:(public NationalTeam(String name,Formation Formation,String country){ super(name,formation) ); this.country=country; })

Abstract class:Abstract classes may have abstract methods which means methods are methods without implementations.抽象类:抽象类可能有抽象方法,这意味着方法是没有实现的方法。

when you are extending a abstract class A, methods of class A must be implemented by your subclass lets say B unless you make your child class B an anstract class.当您扩展抽象类 A 时,类 A 的方法必须由您的子类实现,例如 B,除非您将子类 B 设为 anstract 类。

You need to think how want to design the system not how compiler forces you.您需要考虑如何设计系统而不是编译器如何强迫您。

If design says second class should be abstract then do it and then ultimately implement your abstract methods in your implementation.如果设计说第二个类应该是抽象的,那么就去做,然后最终在你的实现中实现你的抽象方法。

On the other way, if second class is your implementation not abstract, the you have to provide implementation to make object of it另一方面,如果第二个类是您的实现不是抽象的,则您必须提供实现以使其成为对象

my second class must inherit methods from interface, and i dont want that我的第二个类必须从接口继承方法,我不想要那个

Really?真的吗? Then why did you declare the interface in the first place?那你当初为什么要声明接口呢? Why the abstract class needs to implement the interface?为什么抽象类需要实现接口?

Interfaces are supposed to be a higher level of abstraction.接口应该是更高级别的抽象。 It is a contract between the client code and the library.它是客户端代码和库之间的契约。 It creates a interface between the client code and the library.它在客户端代码和库之间创建了一个接口。 It's like saying "I have the abilities to do the things the interface requires me to do".这就像说“我有能力做界面要求我做的事情”。 And you say you don't want to?你说你不想?

So there are two good ways and one bad way to fix this:因此,有两种好方法和一种坏方法可以解决此问题:

Good好的

  1. Don't extend the abstract class.不要扩展抽象类。 Because if you do, it probably does not make sense.因为如果你这样做,它可能没有意义。

  2. The abstract class don't extend the interface.抽象类不扩展接口。

Bad ( This is really really bad )糟糕(这真的很糟糕

Add the methods in the interface and leave the body blank or return a random value.在接口中添加方法并将主体留空或返回一个随机值。 Again, don't do this if you don't have to .再说一次,如果你没有必要,不要这样做。

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

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