简体   繁体   English

Java接口通过调用相同的接口来访问不同的类

[英]Java interface access different classes by calling same interface

I want to use java interface in a way that i will make a call defining interface in my other class like 'private SoapURL soapURL;' 我想以某种方式使用Java接口,以便在其他类(例如“ private SoapURL soapURL;”)中进行调用定义接口 and than i can access any class's method for example : i want to use login:- 然后我可以访问任何类的方法,例如:我想使用login:-

private SoapURL soapURL;
SoapUrl = LoginSoap ();

String nameSpace = soapURL.getMethodName();
String url = soapURL.getUrl();

Is there any way to do something like this. 有什么办法可以做这样的事情。 I am sorry i am not very good with Object Oriented principles but if there is a solution for my problem i would like to know it. 对不起,我对面向对象的原则不太满意,但是如果我的问题有解决方案,我想知道。 Thanks in advance. 提前致谢。

public interface SoapURL {
    public String getNameSpace();
    public String getUrl();
    public String getSoapAction();
    public String getMethodName();
    public String getTag();
}

LoginSoap class 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";
    }
}

SignUpSoap class 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 class 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";
    }
}

Just do, for example: 只是这样做,例如:

SoapURL example = new LoginSoap();
String a = example.getTag();

a should be equal to "Login Activity" a应该等于"Login Activity"

Your implementation looks correct. 您的实现看起来正确。 To make use of it, you can do this in main: 要使用它,可以在main中执行以下操作:

SoapURL reset = new ResetPasswordSoap();
System.out.println(reset.getUrl());

This is a method of minimizing coupling in large systems. 这是在大型系统中使耦合最小化的方法。 And reduces dependency between objects by making use of a common interface for groups of objects that work together. 通过为共同工作的对象组使用通用接口来减少对象之间的依赖性。 You might be new at Object oriented principles, but you are one step ahead of the game already 您可能是面向对象原理的新手,但您已经领先了游戏一步

To pass it to a function, you do: 要将其传递给函数,请执行以下操作:

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 Interface的主要用途是多态性 ,或者对多个不同对象执行相同操作的能力,这正是您所需要的方案

Your approach is absolutely fine , just a modification needed 您的方法绝对不错,只需进行修改

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();

Since LoginSoap , SignUpSoap , ResetPasswordSoap classes are implemented classes of SoapURL Interface , thus reference variable of SoapURL can store Object of any of these child classes 由于LoginSoapSignUpSoapResetPasswordSoap类是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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM