java 创建类的新实例时的Java覆盖方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7347612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:36:35  来源:igfitidea点击:

Java overriding methods when creating new instance of a class

javaandroidoop

提问by blessenm

This might be simple for seasoned java developers but I just cant seem to figure it out. I read a post from here. The code was

对于经验丰富的 Java 开发人员来说,这可能很简单,但我似乎无法弄清楚。我从这里读了一篇文章。代码是

View v = new View(this) {
    @Override
    protected void onDraw(Canvas canvas) {
        System.out.println("large view on draw called");
        super.onDraw(canvas);
    }
};

It was an Android question. Here the user creates an instance of a view and overrides a method in a single line. Is there a name for this kind of coding?

这是一个Android问题。在这里,用户创建了一个视图实例并在一行中覆盖了一个方法。这种编码有名称吗?

My second doubt is, he overrides a protected method from another package. Isn't protected mean package private. I know this will work as I tried it out but I just couldn't figure out why it worked. So why is this code working?

我的第二个疑问是,他覆盖了另一个包中的受保护方法。不受保护意味着包是私有的。我知道这会在我尝试时起作用,但我无法弄清楚它为什么起作用。那么为什么这段代码有效呢?

I did try to google this and search in SO before asking but couldn't figure out an answer.

在问之前,我确实尝试过谷歌搜索并在 SO 中搜索,但无法找到答案。

采纳答案by aioobe

protectedmeans (roughly) "available to sub-classes". (See this table.) Since the new View(this) { ... }creates a subclass, it is possible to override the method within it.

protected意味着(大致)“可用于子类”。(请参阅此表。)由于new View(this) { ... }创建了子类,因此可以覆盖其中的方法。

In this case it doesn't matter that you're in a different package. (See the protectedline and second column in this table.) The fact that the method is in a subclass is sufficient to "get access" to a protected method.

在这种情况下,您在不同的包中并不重要。(请参阅此表中protected行和第二列。)该方法位于子类中的事实足以“访问”受保护的方法。



Potential follow-up question: What sense does it make, if Ican't call the method anyway?

潜在的后续问题:如果无论如何都不能调用该方法,它有什么意义?

All methods in Java are virtual. This means that whenever the Viewclass performs a seemingly internal call to the onDrawmethod, this call will be dispatched to the overridden method.

Java 中的所有方法都是虚拟的。这意味着每当View类对onDraw方法执行看似内部的调用时,该调用将被分派到被覆盖的方法。

回答by Guido

That is not exactly a kind of coding. That is a Java anonymous class. It is very common in Android and in general with event listeners and that kind of stuff.

这不完全是一种编码。那是一个 Java 匿名类。这在 Android 中很常见,通常与事件侦听器之类的东西一起使用。

For more details you can read this link(probably not the best one):

有关更多详细信息,您可以阅读此链接(可能不是最好的链接):

The anonymous inner classes is very useful in some situation. For example consider a situation where you need to create the instance of an object without creating subclass of a class and also performing additional tasks such as method overloading.

匿名内部类在某些情况下非常有用。例如,考虑这样一种情况,您需要在不创建类的子类的情况下创建对象的实例,并且还需要执行其他任务,例如方法重载。

About your second question, the keyword protectedmeans that the method is only available to subclasses, so it is possible to override the method.

关于您的第二个问题,关键字protected表示该方法仅对子类可用,因此可以覆盖该方法。

回答by James DW

This is an anonymous class. You are correct that you are overriding a protected method and this is perfectly normal. A protected method is visible, and can therefore be overridden, by a sub class, which is what you have created here.

这是一个匿名类。您正在覆盖受保护的方法是正确的,这是完全正常的。受保护的方法是可见的,因此可以被您在此处创建的子类覆盖。

Package protected is the default scope when you do not provide a scope for your variable or method. That is different to protected.

当您没有为变量或方法提供范围时,包受保护是默认范围。这与受保护不同。

回答by Nikolay Ivanov

Just like others here are already answered this is called anonymous class, and overriding protected methods is legal since protected methods are visible to child classes and classes in same package.

就像这里的其他人已经回答一样,这称为匿名类,并且覆盖受保护的方法是合法的,因为受保护的方法对子类和同一包中的类是可见的。

回答by Suman

So many answeres were there for "protected", so i am going to other one :)
@override is informing the compiler to override the base class method, and if there is no base class method of this signature then throws compilation error.

有这么多“受保护”的
答案,所以我要去另一个:) @override 通知编译器覆盖基类方法,如果没有此签名的基类方法,则会引发编译错误。

These are called annotations. You can search for annotations topic in java. You can create custom annotations as well.

这些被称为注释。您可以在java中搜索注释主题。您也可以创建自定义注释。

Regards,
SSuman185

问候,
SSuman185