保护超类方法免受子类方法的影响

发布于 2024-10-20 11:10:52 字数 424 浏览 2 评论 0原文

一个类是否有可能有一个只能由其对象调用并对于子类对象隐藏的方法?例如:

class ClassOne {
    ... // attributes

    public void doSomething() { ... }
}

class ClassTwo extends ClassOne {
    ... // attributes and methods
}

ClassOne c1 = new ClassOne();
c1.doSomething(); // ok to call

ClassTwo c2 = new ClassTwo();
c2.doSomething(); // forbidden

我知道这在继承方面似乎很奇怪,但有可能吗?

PS:本题的目的只是为了了解更多有关 OO 编程的继承的知识。

Is it possible to a class has a method that can only be called by it's objects and hidden for a subclass object? For instance:

class ClassOne {
    ... // attributes

    public void doSomething() { ... }
}

class ClassTwo extends ClassOne {
    ... // attributes and methods
}

ClassOne c1 = new ClassOne();
c1.doSomething(); // ok to call

ClassTwo c2 = new ClassTwo();
c2.doSomething(); // forbidden

I know this seems weird thinking in therms of inheritance, but is it possible?

PS: the objective of this question is just to learn more about inheritance of OO programming.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

忆伤 2024-10-27 11:10:52

这是不可能的,也是你的权利,这会破坏继承权。想想

ClassOne c2 = new ClassTwo();
c2.doSomething(); // what to do?

这必须起作用,因为 ClassTwo 是一个 ClassOne。编辑:至少必须编译,如果该方法被重写并执行其他操作取决于您的设计。但你不能让编译器就此产生错误。

It's not possible and your right that this would break inheritance. Think about

ClassOne c2 = new ClassTwo();
c2.doSomething(); // what to do?

This has to work because ClassTwo is a ClassOne. Edit: at least is has to compile, if the method is overridden and does something else is up to your design. But you cannot make the compiler produce an error on this.

冷了相思 2024-10-27 11:10:52

由于 doSomething 是在 classOne 中声明的,因此它将在任何子类中可用。您无法隐藏它或降低它的可见性(将其设置为私有或受保护)。由于 ClassTwo 是 ClassOne 的一个,因此它可以完成 ClassOne 可以做的所有事情。

你能得到的最接近的是类似 :

class ClassTwo extends ClassOne {
   public void doSomething() { throw new UnsupportedOperationException(); }
}

的东西,它并不能完全满足你的要求。

如果您处于需要这样做的情况,最好可能是重新考虑类层次结构,将其划分为具有 doSomething 方法的类类型和不具有 doSomething 方法的类型

public class Parent { ... }
public class ClassOne extends Parent {
   public void doSomething(){ ... }
}
public class ClassTwo extends Parent { 
}

,或者将 doSomething 放入接口中。

Since doSomething is declared in classOne it will be available in any subclasses. You can't hide it or reduce it's visibility ( make it private or protected ). Since ClassTwo is a ClassOne it can do everything ClassOne can do.

The closest you can come is something like :

class ClassTwo extends ClassOne {
   public void doSomething() { throw new UnsupportedOperationException(); }
}

which doesn't quite do what you want.

If you're in a situation where you need to do this best is probably to rethink the class hierarchy into types of your class that have the doSomething method and types that don't

public class Parent { ... }
public class ClassOne extends Parent {
   public void doSomething(){ ... }
}
public class ClassTwo extends Parent { 
}

or put doSomething in an interface.

南巷近海 2024-10-27 11:10:52

可能我误解了你,但你为什么不把你的方法设为私有或包可见呢?

May be I misunderstood you, but why don't you just make your method private or package visible?

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