[英]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.