Java 在接口中创建方法并更改参数类型
Java create method in Interface and change the parameter type
如何创建一个接口,当 Overridden 时可以有不同的类型?有时我希望该方法接受类型 'HtmlPage' 的项目,而其他时候我希望它接受类型 'Document'。稍后这些可能会更改为更多类型。
示例:
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){
}
}
我认为这样的事情应该是可以实现的。我曾尝试在搜索中使用关键字 'Overload' 和 'Override' 环顾四周,但找不到可以像这样工作的东西。如您所见,'MyClass' 已覆盖该方法并定义了正在使用的参数。
在这种情况下你不需要任何接口,不可能做你想做的,你需要在使用之前了解接口的概念。
https://docs.oracle.com/javase/tutorial/java/concepts/interface.html
事实证明我正在寻找的实现不存在,但是有一种更好的方法可以使用输入和输出来完成我正在寻找的事情。
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){
}
}
看来答案其实就在我眼皮子底下。我误解了 Input/Output 类型是什么以及如何使用它们。输入和输出可以是您希望进入或离开应用程序的任何时间。
Input is any information that is needed by your program to complete
its execution.There are many forms that program input may take
输入可以采用许多不同类型的形式,例如 HtmlPage、Document 和 String。使用输入,您可以覆盖您的方法并获得所需的类型。
您可以阅读有关输入和输出的更多信息here
感觉你是在强行用代码来解决一个你没看明白的问题。我喜欢退一步思考组件之间的联系,并在这种情况下改进我的设计。
也许按照 HtmlPage 是文档的思路来思考。所以,如果你
使用文档扩展 HtmlPage (public class HtmlPage extends Document
)。当接口方法接受 Document 时,它将接受 HtmlPage。假设你已经控制了Document和HtmlPage之间的关系,换句话说,你没有使用第三方库。一旦完成,一个方法就不需要对两个不相关的概念进行操作。
我不清楚您的问题的定义,更好的命名可能会有所帮助,无论哪种方式,潜在的解决方案可能如下所示:
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
如何创建一个接口,当 Overridden 时可以有不同的类型?有时我希望该方法接受类型 'HtmlPage' 的项目,而其他时候我希望它接受类型 'Document'。稍后这些可能会更改为更多类型。
示例:
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){
}
}
我认为这样的事情应该是可以实现的。我曾尝试在搜索中使用关键字 'Overload' 和 'Override' 环顾四周,但找不到可以像这样工作的东西。如您所见,'MyClass' 已覆盖该方法并定义了正在使用的参数。
在这种情况下你不需要任何接口,不可能做你想做的,你需要在使用之前了解接口的概念。
https://docs.oracle.com/javase/tutorial/java/concepts/interface.html
事实证明我正在寻找的实现不存在,但是有一种更好的方法可以使用输入和输出来完成我正在寻找的事情。
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){
}
}
看来答案其实就在我眼皮子底下。我误解了 Input/Output 类型是什么以及如何使用它们。输入和输出可以是您希望进入或离开应用程序的任何时间。
Input is any information that is needed by your program to complete its execution.There are many forms that program input may take
输入可以采用许多不同类型的形式,例如 HtmlPage、Document 和 String。使用输入,您可以覆盖您的方法并获得所需的类型。
您可以阅读有关输入和输出的更多信息here
感觉你是在强行用代码来解决一个你没看明白的问题。我喜欢退一步思考组件之间的联系,并在这种情况下改进我的设计。
也许按照 HtmlPage 是文档的思路来思考。所以,如果你
使用文档扩展 HtmlPage (public class HtmlPage extends Document
)。当接口方法接受 Document 时,它将接受 HtmlPage。假设你已经控制了Document和HtmlPage之间的关系,换句话说,你没有使用第三方库。一旦完成,一个方法就不需要对两个不相关的概念进行操作。
我不清楚您的问题的定义,更好的命名可能会有所帮助,无论哪种方式,潜在的解决方案可能如下所示:
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