简体   繁体   English

如何从Java的子类访问基类中的方法

[英]How to access method in base class from child class in java

How to access parent class's methods from child class? 如何从子类访问父类的方法? Following is my class implementation: 以下是我的课程实现:

public interface BaseUrl {
    public String getNameSpace();
    public String getUrl();
}

BaseSoapUrl Class: BaseSoapUrl类:

public class BaseSoapUrl implements BaseUrl {

    @Override
    public String getNameSpace() {
        return "https://host.com/AndroidWFC/";
    }

    @Override
    public String getUrl() {
        return "https://host.com/AndroidWFC/MobileWS.asmx";
    }
}

SoapURL Interface: SoapURL接口:

public interface SoapURL {
    public String getSoapAction();
    public String getMethodName();
}

LoginSoap Class: LoginSoap类:

public class LoginSoap extends BaseSoapUrl implements SoapURL {

   @Override
    public String getSoapAction() {
        return "https://host.com/AndroidWFC/UserControl";
    }

    @Override
    public String getMethodName() {
        return "UserControl";
    }
}

For the sake of simplifying the code i want to implement them like this. 为了简化代码,我想像这样实现它们。 earlier it was something like below : 以前是这样的:

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

LoginSoap Class: LoginSoap类:

public class LoginSoap implements SoapURL {

   @Override
    public String getNameSpace() {
        return "https://host.com/AndroidWFC/";
    }

    @Override
    public String getUrl() {
        return "https://host.com/AndroidWFC/MobileWS.asmx";
    }
   @Override
    public String getSoapAction() {
        return "https://host.com/AndroidWFC/UserControl";
    }

    @Override
    public String getMethodName() {
        return "UserControl";
    }
}

and i could access these methods like below : 我可以访问以下这些方法:

private SoapURL soapURL = new LoginSoap();
String static final url = soapURL.getUrl();

Now the return values form public String getNameSpace(); 现在返回值的形式为public String getNameSpace(); and public String getUrl(); public String getUrl(); are going to be same in all child classes ; 所有儿童班级都一样; so why to write code again and again. 那么为什么要一次又一次地编写代码。 There are going to be many classes which is going to implements SoapURL interface because of web service is being used. 由于正在使用Web服务,将有许多类将实现SoapURL接口。

so my question is how to access methods which is in BaseSoapUrl through soapURL ? 所以我的问题是如何通过soapURL访问BaseSoapUrl方法?

You can re-factor the SoapURL interface to be an abstract class , where you can provide some common implementation of the getNameSpace() and getUrl() methods and leave the others as abstract ones, so that the sub-classes will be forced to provide implementation for them. 您可以将SoapURL接口SoapURL为一个abstract class ,可以在其中提供getNameSpace()getUrl()方法的一些通用实现, 将其他方法保留为abstract类,这样子类将被迫提供为他们实施。

public abstract class SoapURL {
    public String getNameSpace() {
        return "https://host.com/AndroidWFC/";
    }
    public String getUrl() {
       return "https://host.com/AndroidWFC/MobileWS.asmx";
    }

    public abstract String getSoapAction();
    public abstract String getMethodName();
}

I think your refactoring was not correct. 我认为您的重构不正确。 If you consider that a SoapUrl must have a getNameSpace(), then define the getNameSpace() method in the interface. 如果您认为SoapUrl必须具有getNameSpace(),则在接口中定义getNameSpace()方法。 If the purpose of your refactoring was to always return the same values for some of the SoapUrl's methods, I would suggest to follow this organization: 如果重构的目的是始终为某些SoapUrl方法返回相同的值,那么我建议您遵循以下组织:

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

public abstract class BaseSoapUrl implements SoapUrl {
    @Override
    public String getNameSpace() {
        return "https://host.com/AndroidWFC/";
    }

    @Override
    public String getUrl() {
        return "https://host.com/AndroidWFC/MobileWS.asmx";
    }
}

public class LoginSoap extends BaseSoapUrl {

    @Override
    public String getSoapAction() {
        return "https://host.com/AndroidWFC/UserControl";
    }

    @Override
    public String getMethodName() {
        return "UserControl";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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