public class ProtectClassTest{
public static void main(String[] args) throws ProtectClass
ProtectClass p
System
System
try{
p
}
catch(ProtectClass
System
}
try{
p
}
catch(ProtectClass
System
}
System
System
}
}
class ProtectClass{
//user define exception
public static final class NoAccessException extends Exception{
public String toString(){
return
}
}
//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_ARRAY
}
//get the property available state
public final boolean getPropertyAvailable(int pos){
return (pos>=
}
//private property defination section
private String bankAccount;
private String bankPassword;
private void setBankAccount(String bankAccount) throws NoAccessException{
if (getPropertyAvailable(BANKACCOUNT_POS))
this
else
throw new NoAccessException();
}
private void setbankPassword(String bankPassword) throws NoAccessException{
if(getPropertyAvailable(BANKAPASSWORD_POS))
this
else
throw new NoAccessException();
}
ProtectClass(String bankAccount
/*
如果使用這兩句被屏蔽代碼
this
this
*/
this
this
}
ProtectClass() throws NoAccessException{
this(
}
public final void setMethod(int methodID
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