可能是愚蠢的,但我想清楚我对这段代码的技术理解:
import netscape.*;//ldap jar
public class A {
public void method() {
...
try {
//code is written here.
LDAPSearchResults lsr = ldi.search(LDAPConnectionInfo.MY_SEARCHBASE,LDAPConnectionInfo.MY_SCOPE,LDAPConnectionInfo.MY_FILTER,null,false);
while(lsr.hasMoreElements()){
LDAPEntry findEntry = (LDAPEntry)lsr.nextElement();
} catch(...) {
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我打电话给另一个班
public class B {
A a = new A();
//here I want to use attributeName
}
Run Code Online (Sandbox Code Playgroud)
我需要进行任何修改......
调用Object类型的方法.
public class C{
private String attributeName;
public String getAttributeName() {
return attributeName;
}
public Object method(){
attributeName=lAttribute.getName();
}
}
Run Code Online (Sandbox Code Playgroud)
你需要一个班级成员A和一个吸气者:
public class A {
private String attributeName;
public String getAttributeName() {
return attributeName;
}
public void method(){
...
try {
//code is written here.
attributeName = lAttribute.getName();
}
catch() {
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后:
public class B {
A a = new A();
// somewhere
String str = a.getAttributeName();
}
Run Code Online (Sandbox Code Playgroud)
无法像在原始示例中那样访问方法的私有变量,因为它们仅在方法调用期间存在于堆栈中.
编辑:我注意到另一个问题:
我怎么能在另一个类中处理所有这些异常.
我假设您想在其他地方调用您的方法并在那里捕获异常.在这种情况下,您可以使用throws关键字来表示您的方法将异常传递给调用者:
public class A {
public void method() throws IOException {
//code is written here.
String attributeName = lAttribute.getName();
}
public void anotherMethod() {
try {
method();
} catch(IOException ex) {
...
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,如果其他一些代码调用method它将被强制处理或进一步传播异常.
| 归档时间: |
|
| 查看次数: |
2028 次 |
| 最近记录: |