控制对类内部数据或函数成员访问的类
2008-01-05 10:54:08 来源:WEB开发网核心提示:public class PRotectClassTest{public static void main(String[] args) throws ProtectClass.NoaccessException{ProtectClass p1=new ProtectClass("chenqi",&
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;
}
}
}
赞助商链接