一旦初始化就覆盖一个类
下面的代码覆盖对象 GenObject 中的 navigationClick。除了扩展 GenObject 并在子类中实现方法重写之外,还有其他方法可以重写 navigationClick 吗?
是否有如下所示的构造的名称,在类初始化时会发生覆盖?
GenObject go= new GenObject(){
public boolean navigationClick(int status, int time)
{
Below code is overriding navigationClick in object GenObject. Is there any other way of overriding navigationClick other than extending GenObject and implementing a method override in sub - class ?
Is there a name for such a construct as below, where an override occurs when the class is initialised ?
GenObject go= new GenObject(){
public boolean navigationClick(int status, int time)
{
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,为了重写方法,您始终必须创建一个子类。
示例代码中所做的称为 匿名类,但它实际上只是用于创建子类的快捷语法。在字节码级别,它是一个与其他类一样的类,并且它有自己的 .class 文件,名称类似于
ContainingClass$0.class
No, in order to override a method you always have to create a subclass.
What is done in your example code is called an anonymous class, but it's really just a shortcut syntax for creating a subclass. At the bytecode level, it's a class like any other, and it will have its own .class file named something like
ContainingClass$0.class
您只能通过子类化来覆盖。您正在做的是创建一个
匿名
< /a> 类。You can only override by subclassing. What you're doing is creating an
anonymous
class.