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

2008-02-23 09:27:38来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

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的一些负面看法