@Override 我自己的方法?

发布于 2024-10-19 20:54:19 字数 794 浏览 5 评论 0原文

我的设置与您在下面看到的类似。我刚刚注意到“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空‖城人不在 2024-10-26 20:54:19

@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 you extend from) or one of the interfaces it implements.

Edit: sorry I may have misinterpreted what you meant. The reason why you can't override setupViews() is that it's private in MyClass 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 as YourClass), make it protected. Otherwise, make it private.

-残月青衣踏尘吟 2024-10-26 20:54:19

我想你想用
this.setupViews();

I think you want to use
this.setupViews();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文