使用条件更改访问修饰符 [英] Change the access modifier using condition
本文介绍了使用条件更改访问修饰符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在编译时指定访问修饰符?
例如:
Hi,
Is this possible to specify access modifier at compile time?
Ex:
if(accessModifier.public)
change method void A() into Public void A()
else if(accessModifier.protected)
change method void A() into Protected void A()
我需要根据从用户那里获得的条件来启用或禁用某些方法对外部世界的访问权限.
我该怎么办?
在此先感谢
最好的问候,
SRJ
I need to enable or disable the certain methods access permission to the outer world based on the condition get it from users.
how can i do this?
Thanks in advance
Best Regards,
SRJ
推荐答案
思考您要问的问题:最初必须拥有所有方法public
才能完全编译代码.尽管有可能(可能我还没有尝试过)在运行时使用反射更改访问修饰符,但这并不是明智的选择,因为编译器已经为方法调用进行了编码.
而是在执行例程时检查用户的访问级别-并在此时抛出异常或类似情况.
Think about what you are asking: You would have to have all the methods initiallypublic
in order to compile your code at all. While it is (probably, I haven''t tried) possible to change the access modifier at runtime using reflection, it is not a sensible thing to do, as the compiler will already have coded for the method call already.
Instead, check the access level of the user when you execute the routine - and throw an exception or similar at that point.
尽管我不明白为什么有人想要更改访问修饰符在编译时,我认为这是可行的.
查看#if DEBUG - #endif
子句.现在让我们考虑一个
Though I do not understand why somebody would like to change the access modifiers at compile time, I think it is feasable.
Look at the#if DEBUG - #endif
clause. Now let''s consider a
#if PUBLIC
public void A()
#else
protected void A()
#endif
{
//here comes the body of A()
}
这篇关于使用条件更改访问修饰符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文