具有默认(程序包)可见性替代的Android方法(不应该起作用,但是-为什么?) [英] Android method with default (package) visibility overriding (shouldn't work, but does - why?)

查看:55
本文介绍了具有默认(程序包)可见性替代的Android方法(不应该起作用,但是-为什么?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Android中的SVG支持,并提出了库声称它支持SVG,就像它们本身一样.

I've been playing around with SVG support in Android and came up with this library which claims that it supports SVG just as it they were native.

由于我非常努力地发现这是不可能的,所以我去看看这个家伙实际上是如何做到的.所以我遇到了他的

Since I took great pains in discovering that this is not really possible, I went to see how this dude actually managed it. So I came upon his Resources derivative in which he declares a method (loadDrawable) that has default visibility in base Resources class.

有趣的是,通常皮棉会报告您无法编写此方法,因为它会隐藏基本方法,但是在这种情况下(请注意 @Override 指令),就好像该方法是在基类中编写的一样被调用.所有调用此方法的方法都将调用重写,而不是原始方法.对于来自经典的编译器(例如 C ++ Pascal )的我来说,这完全是无法理解的.

The funny thing is, normally lint would just report that you can't write this method since it would hide base method, but in this particular case (note the absense of @Override directive) this method gets to be called as if it were written in the base class. All the methods invoking this method will invoke the override instead of original method. For me coming from classic compilers such as C++ or Pascal, this is totally beyond comprehension.

基于此,我一次使用反射就使SVG支持完全起作用,对此我感到非常高兴,但是:

Based on this, I managed to get my SVG support working completely with one single use of reflection and am super-happy about this, but:

为什么这样做?

推荐答案

Dalvik解释器中似乎存在一个错误,该错误允许重写包专用方法.显然,Google意识到了这个问题(在Jelly Bean中?),因为Dalvik报告了警告,指出在这种情况下它不正确地覆盖了package-private方法,而ART报告为错误并且无法编译它.正确的行为当然是允许它,但不允许从其他程序包覆盖程序包私有方法,但是Google似乎希望避免破坏依赖于此行为的现有应用程序.

It looks like there is a bug in the Dalvik interpreter which allows package-private methods to be overridden. Apparently, Google recognized this issue (in Jelly Bean?), as Dalvik reports a warning that it is incorrectly overriding a package-private method in this case, and ART reports it as an error and fails to compile it. The correct behaviour would of course be to allow it but not allow overriding package-private methods from other packages, but it looks like Google want to avoid breaking existing applications that depend on this behaviour.

更新:现在正式是报告了默认方法-默认包装可见性-覆盖-shouldnt-work-but/23094051#comment35295472_23005656>的报告

UPDATE: This is now officially confirmed in the ART documentation update on June 16, although it states that ART issues a warning instead of a critical error as velis reported in the comments on the question:

Dalvik错误地允许子类覆盖程序包专用方法.在这种情况下,ART会发出警告:

Dalvik incorrectly allowed subclasses to override package-private methods. ART issues a warning in such cases:

在Android 4.1之前,方法void com.foo.Bar.quux()
会错误地覆盖
中的package-private方法com.quux.Quux

Before Android 4.1, method void com.foo.Bar.quux()
would have incorrectly overridden the package-private method in
com.quux.Quux

如果您打算在另一个包中重写类的方法,请将该方法声明为 public protected .

If you intend to override a class's method in a different package, declare the method as public or protected.

这篇关于具有默认(程序包)可见性替代的Android方法(不应该起作用,但是-为什么?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆