熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

控制對類內部數據或函數成員訪問的類

2022-06-13   來源: Java核心技術 

  public class ProtectClassTest{
  public static void main(String[] args) throws ProtectClassNoAccessException{
  ProtectClass p=new ProtectClass(chenqi);
  Systemoutprintln(pbankAccount:+pgetField(pBANKACCOUNT_POS));
  Systemoutprintln(pbankPassword:+pgetField(pBANKAPASSWORD_POS));
  try{
  psetMethod(pBANKACCOUNT_POSrisingsoft);
  }
  catch(ProtectClassNoAccessException error){
  Systemoutprintln(set pbankAccount:+error);
  }
  try{
  psetMethod(pBANKAPASSWORD_POS);
  }
  catch(ProtectClassNoAccessException error){
  Systemoutprintln(set pbankPassword:+error);
  }
  Systemoutprintln(pbankAccount:+pgetField(pBANKACCOUNT_POS));
  Systemoutprintln(pbankPassword:+pgetField(pBANKAPASSWORD_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=;
  public static final int BANKAPASSWORD_POS=;
  //Inner property state array
  //只需要修改以下的對象元素訪問全縣數組即可動態控制用戶可讀/寫的數據成員范圍( / / / )
  private static final int[] PROPERTY_ARRAY={};
  //get the property state array count
  private final int getPropertyCount(){
  return (PROPERTY_ARRAY!=null)?PROPERTY_ARRAYlength:;
  }
  //get the property available state
  public final boolean getPropertyAvailable(int pos){
  return (pos>= && pos<getPropertyCount())?(PROPERTY_ARRAY[pos]==):false;
  }
  //private property defination section
  private String bankAccount;
  private String bankPassword;
  private void setBankAccount(String bankAccount) throws NoAccessException{
  if (getPropertyAvailable(BANKACCOUNT_POS))
  thisbankAccount = bankAccount;
  else
  throw new NoAccessException();
  }
  private void setbankPassword(String bankPassword) throws NoAccessException{
  if(getPropertyAvailable(BANKAPASSWORD_POS))
  thisbankPassword=bankPassword;
  else
  throw new NoAccessException();
  }
  
  ProtectClass(String bankAccountString bankPassword) throws NoAccessException{
  /*
  如果使用這兩句被屏蔽代碼則對象無法構造
  thissetBankAccount(bankAccount);
  thissetbankPassword(bankPassword);
  */
  thisbankAccount=bankAccount;
  thisbankPassword=bankPassword;
  }
  ProtectClass() throws NoAccessException{
  this();
  }
  
  public final void setMethod(int methodIDString 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;
  }
  }
  }

From:http://tw.wingwit.com/Article/program/Java/hx/201311/25666.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.