控制对类内部数据/函数成员访问的类

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;
  }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值