简体   繁体   English

Java 在接口中创建方法并更改参数类型

[英]Java create method in Interface and change the parameter type

How do I create an interface that when Overridden can have different types?如何创建一个接口,当 Overridden 时可以有不同的类型? Sometimes I would like the method to accept an item of type 'HtmlPage' and other times I would like it to take the type 'Document'.有时我希望该方法接受类型为“HtmlPage”的项目,而其他时候我希望它接受类型为“Document”的项目。 Later these could change to even more types.稍后这些可能会更改为更多类型。

Example:例子:

public interface MyInterface{
   void checkThis() throws Exception;

   Output Show(Input input, String url) throws Exception;
}

public class HeadLevel extends MyInterface<HtmlPage,String>{
   Output scrape(HtmlPage page, String url) throws Exception;
}

public class MyClass implements HeadLevel<Output>{
   @Override 
   public void checkThis(Document page, String url){

   }
}

I am thinking something like this should be achievable.我认为这样的事情应该是可以实现的。 I have tried looking around using keywords 'Overload' and 'Override' in my searches but cannot find something that can work like this.我尝试在搜索中使用关键字“Overload”和“Override”四处寻找,但找不到可以像这样工作的东西。 As you can see 'MyClass' has Overridden the method and has defined the parameters being used.如您所见,“MyClass”已覆盖该方法并定义了正在使用的参数。

It feels that you are trying to force the code to solve a problem that you don't understand clearly.感觉你是在强行用代码去解决一个你没有理解清楚的问题。 I like to step back and think about the connections between components and improve my design in such situations.我喜欢退后一步,思考组件之间的联系,并在这种情况下改进我的设计。

Maybe think along the lines that an HtmlPage is a Document.也许按照 HtmlPage 是文档的思路来思考。 So, if you extend an HtmlPage with a Document ( public class HtmlPage extends Document ).因此,如果您使用 Document 扩展 HtmlPage ( public class HtmlPage extends Document )。 When an Interface method accepts Document, it will take HtmlPage.当接口方法接受 Document 时,它将接受 HtmlPage。 Assuming that you have control over the relationship between Document and HtmlPage, in other words, you are not using a third-party library.假设你已经控制了Document和HtmlPage之间的关系,换句话说,你没有使用第三方库。 Once that is done, a method won't need to operate on two unrelated concepts.一旦完成,一个方法就不需要对两个不相关的概念进行操作。

I' am not clear about the definition of your problem, better naming might have helped, either way, a potential solution might look like this:我不清楚您的问题的定义,更好的命名可能会有所帮助,无论哪种方式,潜在的解决方案可能如下所示:

interface MyInterface{
    <K extends Document> void checkThis(K htmlPage) throws Exception;

    Output Show(Input input, String url) throws Exception;
}

class HeadLevel implements MyInterface{
    public <K extends Document> void checkThis(K htmlPage) throws Exception
    {
        // Do something
    }

    public Output Show(Input input, String url) throws Exception{
        return new Output();
    }

    public <K extends Document> Output scrape(K htmlPage, String url) throws Exception
    {
        return new Output();
    }
}

class MyClass extends HeadLevel{

    public MyClass() throws Exception
    {
        checkThis(new HtmlPage());
        checkThis(new Document());
    }

    public <K extends Document> void checkThis(K htmlPage) throws Exception
    {
        super.checkThis(htmlPage);
    }
}

class Document{

}

class HtmlPage extends Document
{

}

https://docs.oracle.com/javase/tutorial/java/generics/types.html https://docs.oracle.com/javase/tutorial/java/generics/types.html

In this case you don't need any interface, it's not possible to do what you want, you need to understand the concept of interfaces before use it. 在这种情况下,您不需要任何接口,无法做您想做的事,需要在使用接口之前先了解接口的概念。

https://docs.oracle.com/javase/tutorial/java/concepts/interface.html https://docs.oracle.com/javase/tutorial/java/concepts/interface.html

The implementation I was looking for turned out to not exist however there is a better way to be able to do what I was looking for using Input and Output. 我一直在寻找的实现不存在,但是有一种更好的方法可以使用输入和输出来完成我正在寻找的事情。

public interface MyInterface<Input, Output> {
   void checkThis(Input input, Output output) throws Exception;

   Output Show(Input input, String url) throws Exception;
}

public class HeadLevel extends MyInterface<HtmlPage,String>{
   Output scrape(HtmlPage page, String url) throws Exception;
}

public class MyClass implements HeadLevel<Output>{
   @Override 
   public void checkThis(Document page, String url){

   }
}

It seems the answer was actually under my nose. 答案似乎在我的耳边。 I misunderstood what Input/Output types are and how you can use them. 我误解了什么是输入/输出类型以及如何使用它们。 Input and Output can be of any time that you expect to come in or go out of an application sort of speak. 输入和输出可以是您希望进入或退出某个应用程序的任何时间。

Input is any information that is needed by your program to complete its execution.There are many forms that program input may take 输入是程序完成其执行所需的任何信息。程序输入可以采用多种形式

Input can take the form of many different types such as HtmlPage, Document, and String. 输入可以采取许多不同类型的形式,例如HtmlPage,Document和String。 Using input you can Override your method and have your desired types. 使用输入,您可以覆盖您的方法并拥有所需的类型。

You can read more about Input and Output here 您可以在此处阅读有关输入和输出的更多信息

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

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