控制对类内部数据/函数成员访问的类
2008-02-23 09:27:38来源:互联网 阅读 ()
public class ProtectClassTest{
public static void main(String[] args) throws ProtectClass.NoAccessException{
ProtectClass p1=new ProtectClass("chenqi","100001");
System.out.println("p1.bankAccount:" p1.getField(p1.BANKACCOUNT_POS));
System.out.println("p1.bankPassword:" p1.getField(p1.BANKAPASSWORD_POS));
try{
p1.setMethod(p1.BANKACCOUNT_POS,"risingsoft");
}
catch(ProtectClass.NoAccessException error){
System.out.println("set p1.bankAccount:" error);
}
try{
p1.setMethod(p1.BANKAPASSWORD_POS,"100002");
}
catch(ProtectClass.NoAccessException error){
System.out.println("set p1.bankPassword:" error);
}
System.out.println("p1.bankAccount:" p1.getField(p1.BANKACCOUNT_POS));
System.out.println("p1.bankPassword:" p1.getField(p1.BANKAPASSWORD_POS));
}
}
class ProtectClass{
//user define exception
public static final class NoAccessException extends Exception{
public String toString(){
return "No privilege to access this property(field/method)";
}
}
//static final defination section
public static final int BANKACCOUNT_POS=0;
public static final int BANKAPASSWORD_POS=1;
//Inner property state array
//只需要修改以下的对象元素访问全县数组即可动态控制用户可读/写的数据成员范围。(0,0 / 0,1 / 1,0 / 1,1)
private static final int[] PROPERTY_ARRAY={0,1};
//get the property state array count
private final int getPropertyCount(){
return (PROPERTY_ARRAY!=null)?PROPERTY_ARRAY.length:0;
}
//get the property available state
public final boolean getPropertyAvailable(int pos){
return (pos>=0 && pos<getPropertyCount())?(PROPERTY_ARRAY[pos]==1):false;
}
//private property defination section
private String bankAccount;
private String bankPassword;
private void setBankAccount(String bankAccount) throws NoAccessException{
if (getPropertyAvailable(BANKACCOUNT_POS))
this.bankAccount = bankAccount;
else
throw new NoAccessException();
}
private void setbankPassword(String bankPassword) throws NoAccessException{
if(getPropertyAvailable(BANKAPASSWORD_POS))
this.bankPassword=bankPassword;
else
throw new NoAccessException();
}
ProtectClass(String bankAccount,String bankPassword) throws NoAccessException{
/*
如果使用这两句被屏蔽代码,则对象无法构造
this.setBankAccount(bankAccount);
this.setbankPassword(bankPassword);
*/
this.bankAccount=bankAccount;
this.bankPassword=bankPassword;
}
ProtectClass() throws NoAccessException{
this("","");
}
public final void setMethod(int methodID,String param) throws NoAccessException{
switch(methodID){
case BANKACCOUNT_POS:
try{
setBankAccount(param);
}
catch(NoAccessException error){
throw error;
}
break;
case BANKAPASSWORD_POS:
try{
setbankPassword(param);
}
catch(NoAccessException error){
throw error;
}
break;
}
}
private String getBankAccount(){
if (getPropertyAvailable(BANKACCOUNT_POS))
return bankAccount;
else
return null;
}
private String getbankPassword(){
if(getPropertyAvailable(BANKAPASSWORD_POS))
return bankPassword;
else
return null;
}
public final String getField(int methodID){
switch(methodID){
case BANKACCOUNT_POS:
return getBankAccount();
case BANKAPASSWORD_POS:
return getbankPassword();
default:
return null;
}
}
}
上一篇: 使用基本数组类型创建可自扩展的数组类
下一篇: 自定义SMTPAppender的源码
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:对Spring的一些负面看法
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash