[英]Java: Creating an empty interface to access different classes from one attribute?
[英]Java interface access different classes by calling same interface
我想以某種方式使用Java接口,以便在其他類(例如“ private SoapURL soapURL;”)中進行調用定義接口 然后我可以訪問任何類的方法,例如:我想使用login:-
private SoapURL soapURL;
SoapUrl = LoginSoap ();
String nameSpace = soapURL.getMethodName();
String url = soapURL.getUrl();
有什么辦法可以做這樣的事情。 對不起,我對面向對象的原則不太滿意,但是如果我的問題有解決方案,我想知道。 提前致謝。
public interface SoapURL {
public String getNameSpace();
public String getUrl();
public String getSoapAction();
public String getMethodName();
public String getTag();
}
LoginSoap類
public class LoginSoap implements SoapURL {
@Override
public String getNameSpace() {
return "https://host.com/MobileWFC/";
}
@Override
public String getUrl() {
return "https://host.com/MobileWFC/MobileWS.asmx";
}
@Override
public String getSoapAction() {
return "https://host.com/MobileWFC/UserControl";
}
@Override
public String getMethodName() {
return "UserControl";
}
@Override
public String getTag() {
return "Login Activity";
}
}
SignSoap類
public class SignUpSoap implements SoapURL {
@Override
public String getNameSpace() {
return "https://host.com/MobileWFC/";
}
@Override
public String getUrl() {
return "https://host.com/MobileWFC/MobileWS.asmx";
}
@Override
public String getSoapAction() {
return "https://host.com/MobileWFC/UserRegister";
}
@Override
public String getMethodName() {
return "UserRegister";
}
@Override
public String getTag() {
return "SignUp Activity";
}
}
ResetPasswordSoap類
public class ResetPasswordSoap implements SoapURL {
@Override
public String getNameSpace() {
return "https://host.com/MobileWFC/";
}
@Override
public String getUrl() {
return "https://host.com/MobileWFC/MobileWS.asmx";
}
@Override
public String getSoapAction() {
return "https://host.com/MobileWFC/UserPasswordReset";
}
@Override
public String getMethodName() {
return "UserPasswordReset";
}
@Override
public String getTag() {
return "Forget Password Activity";
}
}
只是這樣做,例如:
SoapURL example = new LoginSoap();
String a = example.getTag();
a
應該等於"Login Activity"
您的實現看起來正確。 要使用它,可以在main中執行以下操作:
SoapURL reset = new ResetPasswordSoap();
System.out.println(reset.getUrl());
這是在大型系統中使耦合最小化的方法。 通過為共同工作的對象組使用通用接口來減少對象之間的依賴性。 您可能是面向對象原理的新手,但您已經領先了游戲一步
要將其傳遞給函數,請執行以下操作:
public JPanel resetPass(SoapURL reset) {
...
}
// In main:
JPanel resetPassPanel = resetPass(reset);
Interface的主要用途是多態性 ,或者對多個不同對象執行相同操作的能力,這正是您所需要的方案
您的方法絕對不錯,只需進行修改
private SoapURL soapURL;
//SoapUrl = LoginSoap (); // This line should be replaced with the Below line
soapURL=new LoginSoap();
String nameSpace = soapURL.getMethodName();
String url = soapURL.getUrl();
由於LoginSoap
, SignUpSoap
, ResetPasswordSoap
類是SoapURL Interface
實現類,因此SoapURL的引用變量可以存儲任何這些子類的Object
soapURL=new LoginSoap();//soapURL.someMethod will call method of LoginSoapClass
soapURL=new SignUpSoap();// will call method of SignUpSoap class
soapURL=new ResetPasswordSoap();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.