简体   繁体   English

防止方法在另一个包中被调用/覆盖

[英]Preventing a method from being called/overridden in another bundle

I have this abstract class myapi.MyAbstract which is extended by client bundles. 我有这个抽象类myapi.MyAbstract ,它由客户端捆绑包扩展。

package myclientbundle;

import myapi.MyAbstract;

public class MyClass extends MyAbstract {
    // Implementation
}

I need to add a non-API method at myapi.MyAbstract , but I can't find a way of doing that. 我需要在myapi.MyAbstract上添加一个非API方法,但是我找不到这样做的方法。 That method is intended to be called only from inside my API bundle. 该方法只能在我的API包内部调用。

The closest I've got to the solution was put the method as default inside an internal interface and change myapi.MyAbstract to implement MyInternalInterface . 我最接近解决方案的方法是将方法作为default方法放入内部接口,然后更改myapi.MyAbstract以实现MyInternalInterface So that, MyClass would inherit that method and it wouldn't be able to being called from outside of the API bundle. 因此,MyClass将继承该方法,并且无法从API包外部调用它。 But it doesn't prevent of being overridden at client bundle. 但这并不能防止在客户端捆绑包中被覆盖。

Is there another way of doing that? 还有另一种方法吗?

EDIT 编辑

I understand MyAbstract and MyClass are in different packages. 我了解MyAbstract和MyClass位于不同的程序包中。 (If they would be in the same package check here for package-private access level). (如果它们在同一个程序包中,请在此处检查程序包专用访问级别)。

The interface you define the method on will need to have at least protected visibility, otherwise your class MyClass , which is in another package cannot read it. 在其上定义方法的接口至少需要具有受保护的可见性,否则位于另一个程序包中的类MyClass不能读取它。

A method on an interface is by definition public and cannot be final. 接口上的方法根据定义是公共的,并且不能是最终的。 Therefore a default method on an interface is visible to all classes and can be overriden. 因此,接口上的默认方法对所有类都是可见的,并且可以被覆盖。

You could do like this: 您可以这样:

public abstract class MyAbstract {
    protected final void foo() {}
}

public class MyClass extends MyAbstract {
}

The foo method will be visible by all classes extending from MyAbstract , but the method cannot be overriden anymore, because it has been marked final MyAbstract扩展的所有类都可以看到foo方法,但是该方法已被标记为final ,因此无法再覆盖该方法。

If your interface has different scoped method, you should ask yourself if you do not violate the Single Responsibility principle. 如果您的界面具有不同的作用域方法,则应问自己是否没有违反“单一职责”原则。 For example, you could create 2 API Interface (different packages or bundles) if they need both to be accessed from elsewhere. 例如,如果需要从其他位置访问两个API接口(不同的程序包或捆绑包),则可以创建两个接口。

However, if it's only for internal usage, you could create a second interface, more specific, in the implementation bundle/not exported package. 但是,如果仅用于内部使用,则可以在实现包/不导出包中创建第二个接口(更具体)。

For example : MyAbstract (API) <--- MySpecificAbstract (Interface, but in IMPL) <------- MyClass (IMPL). 例如:MyAbstract(API)<--- MySpecificAbstract(接口,但在IMPL中)<------- MyClass(IMPL)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM