数据库

位置:IT落伍者 >> 数据库 >> 浏览文章

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


发布日期:2020年01月23日
 
控制对类内部数据或函数成员访问的类

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;

}

}

}

               

上一篇:c3po数据库连接池中取出连接

下一篇:检测你数据库连接的物理状态