草庐IT

overriding

全部标签

Java - "@Override"有什么用?

这个问题在这里已经有了答案:关闭12年前。PossibleDuplicate:What's“@Override”thereforinjava?到目前为止,我从未在方法前放置“@Override”。我看到了一些代码示例,但我不明白它的实用性。我想要一些解释。非常感谢,JDelage

java - 从接口(interface)覆盖通用返回类型

我有几个接口(interface):publicinterfaceEndpoint{publicClassgetFetchableType();}publicinterfaceFetchable{...fetcheddatafields}publicinterfaceFetcher{publicTfetch(Endpointendpoint);}对于实现Fetcher的类,为什么编译器使用这个方法声明:publicFetchableImplfetch(Endpointendpoint){returnnull;}虽然这些都是不正确的声明:publicFetchableImplfetch(

java - JAXB 2.x : How to override an XmlElement annotation from parent class - Mission Impossible?

为什么这不可能?看起来很简单,但它的行为并不像预期的那样。总结:A类使用聚合的DataAbean,而B类(A类的子类)使用聚合的DataBbean(而DataB扩展了DataA)。我编写了这些测试类来可视化和解释我的问题:A类:packagetest;importjavax.xml.bind.annotation.XmlAccessType;importjavax.xml.bind.annotation.XmlAccessorType;importjavax.xml.bind.annotation.XmlElement;importjavax.xml.bind.annotation.X

java - 名称冲突 : The method add(Object) of type test2 has the same erasure as add(E) of type HashSet<E> but does not override it

导入java.util.*;classAextendsHashSet{publicbooleanadd(Objectobj){//compilererrorreturntrue;}}orclassAbc{publicvoidadd(Tt){}//compilererrorpublicvoidadd(Objecti){}//compilererror(can'toverload?)}错误:名称冲突:test2类型的方法add(Object)与HashSet类型的add(E)具有相同的删除,但没有覆盖它我不知道上述错误背后的概念是什么,有人可以建议我在哪里可以研究这个概念吗?

Java 7 但不是 Java 6 : "is not abstract and does not override abstract method"

谁能解释为什么这在JDK1.6中编译,但在JDK1.7中却没有,我从中得到了错误消息:java:ExampleisnotabstractanddoesnotoverrideabstractmethodcompareTo(java.lang.Object)injava.lang.Comparable?importjava.util.concurrent.*;publicclassExampleimplementsScheduledFuture{@OverridepubliclonggetDelay(TimeUnitunit){return0;}@Overridepublicintcomp

java - 覆盖具有更高可见性的方法是好的做法吗?

回答这个问题:HowtoGUI-Usingpaintcomponent()toinitializeaGUIandthentoaddGUIbasedonmouse我已经声明了:Youdon'toverridepaintComponent()properly.Thisisaprotectedmethod,notpublic.Ifyouadd@Overrideannotationonthismethodthenthecompilerwillcomplain.但是@peeskillet明智地指出了这一点:Thecompilerwillnotcomplainaboutpublicorprotec

Java:在覆盖方法时替换参数的子类/子类型?

所以我之前问过这个问题,但大多数人发现我的代码有错误,而不是问题本身。无论如何,我正在尝试重写类中的接口(interface)方法。但是,我希望覆盖方法中的参数类型是覆盖方法中定义的参数类型的子类。界面是:publicinterfaceObserver{publicvoidupdate(ComponentUpdateEventupdateEvent)throwsException;}虽然重写这个方法的类是:publicclassConsoleDrawerextendsDrawer{//...@Overridepublicvoidupdate(ConsoleUpdateEventupda

java - 为什么如果静态方法不涉及多态性(后期绑定(bind))我会看到无法覆盖静态方法的错误

请考虑以下代码:classA{publicstaticvoidm(Numbern){System.out.println("NumberA");};}classBextendsA{publicstaticintm(Numbern){System.out.println("NumberB");return1;};}输出:java:m(java.lang.Number)ininheritanceTest.Bcannotoverridem(java.lang.Number)ininheritanceTest.Areturntypeintisnotcompatiblewithvoid我知道静态

java - 我们可以在一个接口(interface)中有一个 main() 并在实现这个接口(interface)的类中有不同的 main() 实现吗?

我知道main()可以在一个类中重载,编译器总是将带有String[]args的参数作为执行开始的主要方法。但是是否可以声明相同的main(Stringargs[])inaninterfaceandimplementitindifferentclassesdifferently?例如,packagetest;interfacetest{publicvoidmain(Stringargs[]);publicvoiddisplay();}packagetest;classTestclass1implementstest{publicvoiddisplay(){System.out.prin

java - 异常重写方法

下面是书中的引述:TheoverridingmethodmustNOTthrowcheckedexceptionsthatareneworbroaderthanthosedeclaredbytheoverriddenmethod.Forexample,amethodthatdeclaresaFileNotFoundExceptioncannotbeoverriddenbyamethodthatdeclaresaSQLException,Exception,oranyothernon-runtimeexceptionunlessit'sasubclassofFileNotFoundExc