是否可以隐藏或降低对Java中的Inherited Methods的访问权限? [英] Is it possible to hide or lower access to Inherited Methods in Java?

查看:185
本文介绍了是否可以隐藏或降低对Java中的Inherited Methods的访问权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类结构,我希望基类中的某些方法可以从直接从基类派生的类中访问,但不能从派生类派生的类中访问。根据Java语言规范,可以覆盖继承方法的访问规范,使其更公开,但不是更私密。例如,这是我需要做的事情的要点,但这是非法的:

I have a class structure where I would like some methods in a base class to be accessible from classes derived directly from the base class, but not classes derived from derived classes. According to the Java Language specification it is possible to override access specifications on inherited methods to make them more public, but not more private. For example, this is the gist of what I need to do, but is illegal:

// Defines myMethod
public class Base {
    protected void myMethod() {}
}

// Uses myMethod and then hides it.
public class DerivedOne extends Base {
    @Override
    private void myMethod();
}

// can't access myMethod.
public class DerivedTwo extends DerivedOne {

}

有没有有什么方法可以实现这个目标吗?

Is there any way to accomplish this?

编辑解释我为什么要这样做:

Edited to explain why I would like to do this:

在这种情况下类结构是一种数据处理和导入结构。它读入并解析充满表格数据的文本文件,然后将它们存储在数据库中。

In this case the class structure is a data handling and import structure. It reads in and parses text files full of tabular data and then stores them in a database.

基类是管理数据库处理部分的基表类。其中包含了相当多的功能,这些功能对于所有表类型都是通用的 - 因为一旦它们在数据库中,它们就变得统一。

The base class is the base table class managing the database handling part of it. There is a fair amount of functionality contained in it that is common to all table types - as once they are in the database they become uniform.

中间类特定于要解析的文件中的表类型,并具有表解析和导入逻辑。它需要访问一些基类的数据库访问函数。

The middle class is specific to the kind of table in the file being parsed, and has the table parsing and import logic. It needs access to some of the base class's database access functions.

顶级类是特定于表的,只是以父类可以理解的方式初始化表的布局。此外,基类的用户不需要查看或访问中产阶级所执行的数据库特定功能。本质上,我想将这些函数仅显示在基类之上的一个级别而不是其他人。

The top level class is specific to the table and does nothing more than initialize the table's layout in a way the parent classes can understand. Also users of the base class do not need to see or access the database specific functions which the middle class do. In essence, I want to reveal these functions only to one level above the base class and no one else.

我问,因为,虽然我发布的代码是一个例子,非法,可能还有其他一些手段来完成同样的目的。我问是否存在。

I ask because, although the code I posted as an example is illegal, there may be some other means to accomplish the same end. I'm asking if there is.

也许隐藏是错误的方式来表达这一点 - 我真正需要做的是暴露一些应该是私有的功能类到层次结构中的类一级。隐藏会实现这一点 - 但我可以看到隐藏是一个问题。有没有其他方法可以做到这一点?

Perhaps hiding is the wrong way to phrase this - what I really need to do is expose some functionality that should be private to the base class to the class one level up in the hierarchy. Hiding would accomplish this - but I can see how hiding would be a problem. Is there another way to do this?

推荐答案

我认为你提出的问题的本质暴露了概念问题与您的对象模型。您试图将各种不同的职责描述为是一种关系,而实际上您应该做的是描述有一个或使用关系。您希望从子类隐藏基类功能这一事实告诉我,这个问题实际上并没有映射到三层继承树。

I think the very nature of the problem as you've posed it exposes conceptual problems with your object model. You are trying to describe various separate responsibilities as "is a" relationships when actually what you should be doing is describing "has a" or "uses a" relationships. The very fact that you want to hide base class functionality from a child class tells me this problem doesn't actually map onto a three-tiered inheritance tree.

听起来你正在描述一个典型的ORM问题。让我们再看看这个,看看我们是否可以将它重新映射到除了严格的是继承之外的其他概念,因为我认为你的问题不是技术性的,它是概念性的:

It sounds like you're describing a classic ORM problem. Let's look at this again and see if we can re-map it onto other concepts than strict "is a" inheritance, because I really think your problem isn't technical, it's conceptual:

你说:


基类是基础表类
管理$ b $的数据库处理部分它。其中包含了相当数量的
功能,它们是所有表类型通用的
- 因为一旦
,它们就会在数据库中变成
uniform。

The base class is the base table class managing the database handling part of it. There is a fair amount of functionality contained in it that is common to all table types - as once they are in the database they become uniform.

这可能更清楚,但听起来我们有一个类需要管理数据库连接和常见的数据库操作。在单一责任之后,我想我们已经完成了。您不需要扩展此类,您需要移动到需要使用其功能的类。

This could be more clear, but it sounds like we have one class that needs to manage the DB connection and common db operations. Following Single Responsibility, I think we're done here. You don't need to extend this class, you need to hand it to a class that needs to use its functionality.


中间类特定于
解析文件中的
类表,并具有表解析和
导入逻辑。它需要访问基类的数据库访问
函数的一些

The middle class is specific to the kind of table in the file being parsed, and has the table parsing and import logic. It needs access to some of the base class's database access functions.

这里的中产阶级听起来有点像 Data Mapper 。这个类不需要扩展前一个类,它需要拥有对它的引用,可能在构造函数或setter上作为接口注入。

The "middle class" here sounds a bit like a Data Mapper. This class doesn't need to extend the previous class, it needs to own a reference to it, perhaps injected on the constructor or a setter as an interface.


顶级类特定于
表,并且只执行
初始化表的布局,方式是父类可以理解的

基类的用户也不需要
需要查看或访问中间
类所具有的数据库
特定函数。本质上,我想将
这些函数仅显示在基类之上的一个级别
而不是其他人。

The top level class is specific to the table and does nothing more than initialize the table's layout in a way the parent classes can understand. Also users of the base class do not need to see or access the database specific functions which the middle class do. In essence, I want to reveal these functions only to one level above the base class and no one else.

我不清楚为什么一个高级类似乎对db模式有所了解(至少那是初始化表格的布局这句话给我的意思),但是再次,如果前两个之间的关系课程是封装(有一个/使用)而不是继承(是一个),我认为这不是问题。

I'm not clear why a high-level class seems to have knowledge of the db schema (at least that's what the phrase "initialize the table's layout" suggests to me), but again, if the relationship between the first two classes were encapsulation ("has a"/"uses a") instead of inheritance ("is a"), I don't think this would be a problem.

这篇关于是否可以隐藏或降低对Java中的Inherited Methods的访问权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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