@Override 我自己的方法?
我的设置与您在下面看到的类似。我刚刚注意到“YourClass”实际上是在“MyClass”中实现我的一些逻辑。啊。我尝试在“YourClass”中的 setupViews() 上方抛出 @Override,但它不会编译,并指出“DataManagerActivity 类型的方法 setupViews() 必须覆盖超类方法”
代码已更改。这是一个例子。我刚刚输入了错误的内容。同样的问题。如何防止扩展 MyClass 的 YourClass 从 MyClass 实现 setupViews() ?
public class MyClass extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setupViews();
...
}
private void setupViews() {
....
}
}
public class YourClass extends MyClass {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setupViews();
...
}
private void setupViews() {
....
}
}
I have a setup similar to what you see below. I have just noticed that "YourClass" is actually implementing some of my logic from "MyClass." Ugh. I tried to throw an @Override above setupViews() in "YourClass" but it won't compile stating, "The method setupViews() of type DataManagerActivity must override a superclass method"
Code changed. It was an example. I just typed the wrong thing. Same question. How can I keep YourClass that extends MyClass from implementing setupViews() from MyClass?
public class MyClass extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setupViews();
...
}
private void setupViews() {
....
}
}
public class YourClass extends MyClass {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setupViews();
...
}
private void setupViews() {
....
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@Override
指示编译器失败,除非其下面的方法重写超类(您扩展
的超类)中的方法或接口
之一>它实现了。编辑:抱歉我可能误解了你的意思。您无法覆盖
setupViews()
的原因是它在MyClass
中是private
,因此您无法访问或从任何子类覆盖它。如果这就是您想要的,那么您希望您的方法受到
保护
- 例如,可以在定义它的类的子类中访问和重写,但不能从外部访问。编辑 2':所以底线:
如果您希望在子类(例如
YourClass
)中可覆盖setupViews()
,请使其受保护
。否则,将其设为私有
。@Override
instructs the compiler to fail unless the method underneath it overrides a method in the superclass (the one youextend
from) or one of theinterface
s it implements.Edit: sorry I may have misinterpreted what you meant. The reason why you can't override
setupViews()
is that it'sprivate
inMyClass
so that you cannot access or override it from any subclasses.If that's what you want, then you want your method to be
protected
- as in, accesible and overrideable in subclasses of the class it's defined in, but not accessible from outside.Edit 2': so bottom line:
If you want
setupViews()
to be overridable in subclasses (such asYourClass
), make itprotected
. Otherwise, make itprivate
.我想你想用
this.setupViews();
I think you want to use
this.setupViews();