相关疑难解决方法(0)

如何防止从特定类调用公共方法

我有一个现有的类,我想添加一个方法.但我希望只从特定类的特定方法调用该方法.有什么方法可以阻止来自其他类/方法的调用吗?

例如,我有一个现有的A类

public final class A
{
    //other stuff available for all classes/methods

    //I want to add a method that does its job only if called from a specific method of a class, for example:

    public void method()
    {
        //proceed if called from Class B.anotherMethod() else throw Exception
    }
}
Run Code Online (Sandbox Code Playgroud)

这样做的一种方法是获取StackTrace内部method()然后确认父方法?

我正在寻找的解决方案是更清洁和可取的解决方案,如模式或其他东西.

java class public

15
推荐指数
4
解决办法
7796
查看次数

标签 统计

class ×1

java ×1

public ×1