简体   繁体   English

如何仅允许特定成员访问类 java 中的方法

[英]How to allow accessing to a method in a class java only to particular members

I have to create employee object.我必须创建员工对象。 There is an attribute called occupation in that employee class.在该雇员类中有一个称为职业的属性。 Only accountant's can access the method call calculatnetsalary() method.只有会计师可以访问方法调用calculatnetsalary()方法。 How to code this in java.如何在 java 中编写此代码。

There are a couple of ways to achieve this.有几种方法可以实现这一点。

Maybe the best approach is to put in the same package your accountant and employee classes and make the calculatenetsalary method package protected.也许最好的方法是将会计类和员工类放在同一个包中,并保护 calculatenetsalary 方法包。 Of course if you don't want any other class to access the method, the other classes will go to different packages.当然如果你不希望任何其他类访问该方法,其他类将转到不同的包。

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

相关问题 Java(FX)-仅允许1个类从单例类中调用方法 - Java(FX) - Only allow 1 class to call a method from a singleton class 如何检查java类中是否有特定的方法? - How to check if a java class has a particular method in it? Java:访问内部类的继承的受保护成员 - Java: Accessing an inner class' inherited protected members 父类访问子类Java的受保护成员 - Parent class accessing protected members of subclasses Java 如何修改对 class 的访问,以便一个特定属性只能由另一个 class 的成员访问? - How to modify access to a class so that one particular attribute is only accessible by members of another class? 如何在Java中的同一个类中为多个枚举成员使用toString()方法 - how to use the toString() method for multiple enum members in same class in Java 通过继承在Java中使用父类实例访问子类成员 - Accessing child class members with parent class instance in java through inheritance 主类中只有Java的静态成员 - Java only static members in main class 仅包含私有成员的Java类 - Java class containing only private members 如何允许泛型类方法仅由某些类型的参数执行? - How to allow a generic class method to be executed only by certain types of parameters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM