在java中定义抽象方法时_关于java:当我们在类中定义接口的抽象方法时,是“覆盖”或“实现”,还是干脆说“定义”这些方法?...

B类中的show()方法是"重写","实现"还是仅是"定义"?

interface A

{

void show();

}

class B implements A

{

public void show()

{

System.out.println("What is the proper term to explain?");

}

}

实施更好

我会说"实现"是最正确的,因为"重写"表明它已经有一个被重写的实现,而"定义"仅在类" B"的情况下才是正确的

可能是stackoverflow.com/questions/44856645/的重复项(无论如何,JLS答案在那里。在此示例中,show被覆盖并实现。)

无论使用哪种术语,都应仍使用@Override批注。

一般来说,我说"覆盖"是指重新定义继承的行为,而我说"实现"是指定义一个行为(尚未实现)。

所以我想说B实现了show()。

捷豹路虎似乎就是这样:

If a non-abstract method mC overrides an abstract method mA from a

class C, then mC is said to implement mA from C.

oracle示例文档似乎说override也是为了重新定义行为:

The ability of a subclass to override a method allows a class to

inherit from a superclass whose behavior is"close enough" and then to

modify behavior as needed. The overriding method has the same name,

number and type of parameters, and return type as the method that it

overrides. An overriding method can also return a subtype of the type

returned by the overridden method. This subtype is called a covariant

return type.

请注意,java.lang.Override批注并未相反(重点是我的):

Indicates that a method declaration is intended to override a method

declaration in a supertype. If a method is annotated with this

annotation type compilers are required to generate an error message

unless at least one of the following conditions hold:

The method does override or implement a method declared in a

supertype.

The method has a signature that is override-equivalent to that of any

public method declared in Object.

该API可以在@Override注释之外另外提供@Implement。

但是两者都显得很笨拙。因此,他们可能只保留了一个@Override来传达这两件事。

在我的理解中,注释状态为"确实覆盖或实现了在超类型中声明的方法" =>,覆盖旨在用于抽象类,并用于接口。

@Joel但是您可以覆盖在继承的类中定义的具体方法。不是吗

一个简短的答案,我们override那些方法,为什么?

因为这代表了多态语句的概念,所以请记住

List aa = new ArrayList<>();

// List => interface

// ArrayList => class

aa.add("polymorphic statment");

多态性声明和覆盖是相辅相成的。

尽管任何类还可以通过声明自己abstract来实现接口,而无需使用override任何方法。

注意:在任何IDE中应用相同的概念,将引导您添加@Override批注。

但是最重??要的是建议替换现有的实现,接口不是这种情况(如果我们不使用默认值)

错误:方法未从超类型@Override覆盖或实现方法^在错误的上下文中使用@Override注释时,出现上述错误消息。我认为Java使用相同的注释从接口实现方法并覆盖子类中的方法。

@PratipChakraborty您必须做错了一些事情,访问修饰符,参数等。请查看此演示以验证您的代码

@Pavneet_Singh我想要一些极客建议以解决争议。由于没有单独的注释表示从接口实现方法,因此使用了@Override。尽管这两个概念有点相似,但是Stack Overflow的专家希望对此主题有更多的了解。

任何其他视图@Pavneet_Singh,因为我认为IDE导致@Override注释,原因是缺少任何其他可以定义相同注释的适当注释。无论如何,感谢您的宝贵时间和意见。

@PratipChakraborty看,实现(甚至在编程中)的含义是提供上下文或做特定的事情,因此我们可以重写(与实现相同以实现特定的事情),或者只是继承方法(如果类是抽象的)

@Pavneet_Singh请看一下---- zh.wikipedia.org/wiki/Interface_(Java)

您应该说实现,因为override意味着您要覆盖某些现有方法,这对于接口来说是不正确的。接口不能包含方法定义,它们仅指定合同。

(除非有默认实现-自Java 8起)

我不知道。感谢您指出了这一点。但是,我无法想到这种情况有用的情况(为什么不使用抽象类?)

它确实与抽象类相似,但是有时您确实希望在接口中使用它,例如,当实现类实现多个接口或已经扩展了一些抽象类时。但是显然不能过度使用它,否则会导致糟糕的设计。

我同意,过度使用会导致糟糕的设计

类B中的方法show()实现在接口A中声明的方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值