简体   繁体   中英

Lock Methods from Object (Java)

i have the following problem.

i hava a java class

 public class A {
    login()
    getRights()
    methodA()
    methodB()
    methodC()
   }

now i have the following scenario. I create an instance of class A. and now i want to check with the login function wheter i'm logged in. If not, the object don't have the rights to call method a, b and c. After that i want to check with the getRights() function wheter the object have the rights to call the method b and c. Is there a smart implementation? otherwise i have to inherit from class a and overwrite the blocked functions ?

You can call login() before calling methodA(), or you can write a call to login() inside methodA() so that the code inside methodA() can't be run without running login() first. Then if the object calls methodA() but doesn't return the right value from login() you can throw an exception before going any farther. Then do the same thing with getRights() inside B and C.

如果调用了方法a,b和c但未登录,则可能引发异常。您只需要使用变量来设置是否已登录,并设置是否引发异常,而getRights仅使用该变量。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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