Java 接口访问不同 类 通过调用相同的接口
Java interface access different classes by calling same interface
我想使用 java 接口,这样我就可以在我的其他 class 中调用定义接口,例如 'private SoapURL soapURL;',这样我就可以访问任何 class的方法例如:我想使用登录:-
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 class
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";
}
}
SignUpSoap class
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 class
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);
The main use of Interface is polymorphism, or the ability to perform the same
operation on a number of different objects,
which is exactly what you wanted in your scenario
你的方法非常好,只是需要修改一下
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 中的任何一个 child 类
soapURL=new LoginSoap();//soapURL.someMethod will call method of LoginSoapClass
soapURL=new SignUpSoap();// will call method of SignUpSoap class
soapURL=new ResetPasswordSoap();
我想使用 java 接口,这样我就可以在我的其他 class 中调用定义接口,例如 'private SoapURL soapURL;',这样我就可以访问任何 class的方法例如:我想使用登录:-
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 class
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";
}
}
SignUpSoap class
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 class
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);
The main use of Interface is polymorphism, or the ability to perform the same operation on a number of different objects, which is exactly what you wanted in your scenario
你的方法非常好,只是需要修改一下
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 中的任何一个 child 类
soapURL=new LoginSoap();//soapURL.someMethod will call method of LoginSoapClass
soapURL=new SignUpSoap();// will call method of SignUpSoap class
soapURL=new ResetPasswordSoap();