java中不同的子类对父类的方法是否有可能有不同的可见性
不同的子类对父类的方法是否有不同的可见性?假设有一个类A,它定义了10个方法。它有两个不同的子ClassB和ClassC。 ClassB 和 ClassC 是否有可能访问 ClassA 的不同方法。就像 ClassB 只能访问 ClassA 中定义的 10 个方法中的 6 个,而 ClassC 只能访问 ClassA 的其他 4 个方法? ClassB 和 ClassC 在同一个封装中。
谢谢, 阿西特
Is it possible that different child classes have different visibilities to the methods of the parent. Suppose there is a class A which has 10 methods defined. It has two different child ClassB and ClassC. Is it possible that that ClassB and ClassC has access to different methods of ClassA. Like ClassB has access to only 6 of 10 methods defined in ClassA and ClassC has acess only to the other 4 methods of ClassA? ClassB and ClassC are in same package.
Thanks,
Asit
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我认为上课是不可能的。要隔离功能,您应该使用接口而不是扩展类。
如果您需要划分诸如那。
然后尝试使用组合而不是继承从简单的类中组合出复杂的类。另请查看策略模式。
将您的功能划分为这样的接口 -
然后像这样实现您的类 -
注意:该示例是用 C# 编写的。你需要计算出 java 的等价物。
I don't think it is possible with classes. To segregate functionality you should use
interfaces
instead of extending classes.It is quite likely that your class A is violating the Single Responsibility Principal if you need to divide methods like that.
Then look to use composition instead of inheritance to compose complex classes from the simpler ones. Also take a look at the strategy pattern.
Divide your functionality in interfaces like this -
Then implement your classes like this -
Note: The example is in C#. You need to work out the java equivalent.
您可以使用接口来完成这样的事情,但不能使用具体的类。
You can do such a thing with interfaces, but not concrete classes.
适配器 或 装饰器模式会帮助你。
Adapter or Decorator pattern will help you.
从你写的来看,我想你所拥有的是
在这种情况下,B和C只会看到A类中的方法methodA和methodC。methodB
是私有的,因此无法访问的
methodD是受包保护的,也就是说仅限于同一包中的类(a我现在意识到,以一种奇怪的方式拥有 C++
friend
关键字的等价物),因此在 packgaefirst
之外不可见。From you wrote, i suppose what you have is
In this case, B and C will only see methods methodA and methodC from class A.
methodB is private, so unreachable
methodD is package protected, that's to say restricted to classes in same packgae (a weird way to have an equivalent of the C++
friend
keyword, I realize now), and as a consequence not visible outside of packgaefirst
.不太明白你的意思......
通常父类的方法可能有不同的访问修饰符作为可能的固有类。但有一个条件(引用自 lang 规范):
重写或隐藏方法的访问修饰符必须提供至少与重写或隐藏方法一样多的访问权限,否则会发生编译时错误。
您不能限制类的可用性,即,如果类 A 中的方法受到“保护”,那么您可以将类 B 中的重写方法声明为“公共”。但反之则不行
not really understood what do you mean...
it is generally possible that methods of a parent class have different access modifiers as possible inherent classes. but with one condition (citation from lang spec):
The access modifier of an overriding or hiding method must provide at least as much access as the overridden or hidden method, or a compile-time error occurs.
you cannot coarct usability of a class, i.e. if a method from your class A was "protected" then you can declare an overriding method in your class B as "public". but it doesn't work vice-versa
我不知道你问题的背景,所以我无法评论你的设计是否合理,以及你的动机是否合理。由于其他人已经采取了您的 ClassA 需要重构的立场,因此我将采取相反的做法,并假设它是一个具有单一明确定义目的的合理类,并且不需要细分。
在这种情况下,为什么不使用对象适配器模式来实现您想要的目标呢?您可以通过实现将调用转发到 ClassA 适配器的包装器方法,在 ClassB 和 ClassC 适配器中公开所需的 ClassA 方法。当然,您可以选择扩展这些方法。
I don't know the context of your question so I can't comment as to whether your design is sound and by extension, whether your motivation for this is justified. Since others have already taken the stance that your ClassA requires refactoring, I'm going to do the opposite and assume that it's a sensible class with a single well-defined purpose and it doesn't require subdivision.
In which case, why not use the object adapter pattern to achieve what you're after? You can expose the ClassA methods you want to in your ClassB and ClassC adapters by implementing wrapper methods which forward invocations to your ClassA adaptee. And of course, you can optionally extend those methods.
这个答案基于您对术语子类
的使用
简短回答否
更长的答案
在这种情况下:
类 Base 有以下方法:method1、method2、method3
使用 Java 无法设置以下情况:
class Derived1(该类扩展了 Base 类)可以访问 method1 和 method2,但不能访问 method3。
class Derived2(此类扩展了 Base 类)可以访问 method1、method2 和 method3。
这两个类都在同一个包中。
在 Java 中,当一个类扩展另一个类时,这些事情总是适用:
派生类可以调用基类的每个包访问方法。
This answer is predicated on your use of the term child class
Short answer no
Longer answer
In this situation:
class Base has the following methods: method1, method2, method3
There is no way, using Java, to setup the following situation:
class Derived1 (this class extends class Base) can access method1 and method2, but cannot access method3.
class Derived2 (this class extends class Base) can access method1, method2, and method3.
Both classes are in the same package.
In Java, when one class extends another class these things always apply:
the derived class may call every package access method of the base class.