Java返回类型为Void和Object(更像是可选的)

Java返回类型为Void和Object(更像是可选的),java,optional,Java,Optional,我想知道是否有一种方法可以用void或Object实现接口方法 例如 接口 及 实现类1 实施类2 您可以使用MyInterface和用于void案例的: public interface MyInterface<T> { T myMethod(); } public class MyClassOne implements MyInterface<Void> { @Override public Void myMethod() {

我想知道是否有一种方法可以用void或Object实现接口方法

例如

接口

实现类1

实施类2


您可以使用
MyInterface
和用于
void
案例的:

public interface MyInterface<T> {
    T myMethod();
}

public class MyClassOne implements MyInterface<Void> {
    @Override
    public Void myMethod() {
        System.out.println("MyClassOne");
        return null;
    }
}

public class MyClassTwo implements MyInterface<String> {
     @Override
     public String myMethod() {
         System.out.println("MyClassTwo");
         return "MyClassTwo";
     }
}
公共接口MyInterface{
T myMethod();
}
公共类MyClassOne实现MyInterface{
@凌驾
公共方法(){
System.out.println(“MyClassOne”);
返回null;
}
}
公共类MyClass2实现MyInterface{
@凌驾
公共字符串myMethod(){
System.out.println(“MyClass2”);
返回“MyClass2”;
}
}

您可以使用
MyInterface
和用于
void
案例的:

public interface MyInterface<T> {
    T myMethod();
}

public class MyClassOne implements MyInterface<Void> {
    @Override
    public Void myMethod() {
        System.out.println("MyClassOne");
        return null;
    }
}

public class MyClassTwo implements MyInterface<String> {
     @Override
     public String myMethod() {
         System.out.println("MyClassTwo");
         return "MyClassTwo";
     }
}
公共接口MyInterface{
T myMethod();
}
公共类MyClassOne实现MyInterface{
@凌驾
公共方法(){
System.out.println(“MyClassOne”);
返回null;
}
}
公共类MyClass2实现MyInterface{
@凌驾
公共字符串myMethod(){
System.out.println(“MyClass2”);
返回“MyClass2”;
}
}

Void类是一个不可实例化的占位符类,用于保存对表示Java关键字Void的类对象的引用。

Void类是一个不可实例化的占位符类,用于保存对表示Java关键字Void的类对象的引用。

一般来说,我认为您尝试做的不是一个好主意,因为有了它,您就无法将编程作为接口,因为根据您期望的实现,结果是或不是。 使用带有Void的参数化类型可以说明问题

事实上,您不能有一个基于或多或少精确的类型的类型化返回。 在返回中处理对象类型的实例

例如:

MyInterface myInterface = new MyClassOne();
Object myMethod = myInterface.myMethod();
所以,您必须执行instanceof或getClass来猜测要执行的操作。。。 你不再做OOP了。 接口失去了电源

如果需要一个带返回的方法和一个不带返回的方法,请创建两个方法。如果要使用单个方法,可以保留最广泛的方法(哪一个方法具有返回类型),并且当没有要返回的信息时,可以返回null或特殊值


一个方法必须有明确的责任。

一般来说,我认为您尝试做的不是一个好主意,因为有了它,您就不能作为接口编程,因为根据您期望的结果或否实现。 使用带有Void的参数化类型可以说明问题

事实上,您不能有一个基于或多或少精确的类型的类型化返回。 在返回中处理对象类型的实例

例如:

MyInterface myInterface = new MyClassOne();
Object myMethod = myInterface.myMethod();
所以,您必须执行instanceof或getClass来猜测要执行的操作。。。 你不再做OOP了。 接口失去了电源

如果需要一个带返回的方法和一个不带返回的方法,请创建两个方法。如果要使用单个方法,可以保留最广泛的方法(哪一个方法具有返回类型),并且当没有要返回的信息时,可以返回null或特殊值


方法必须有明确的责任。

非常好,谢谢。为什么我们需要返回空值?我不能使用void。我的主要问题是我已经实现了Class1,我不能更改方法签名。我的新类实现Class2必须返回。但是,两者必须使用相同的接口。不能使用void(小v),因为它不是一个类型,所以必须使用void(大v),或者可以使用
对象
并返回null。如果你不能更改你的void实现,那么你就不能(AFAIK)做你想做的事情。非常好,谢谢。为什么我们需要返回空值?我不能使用void。我的主要问题是我已经实现了Class1,我不能更改方法签名。我的新类实现Class2必须返回。但是,两者必须使用相同的接口。不能使用void(小v),因为它不是一个类型,所以必须使用void(大v),或者可以使用
对象
并返回null。如果无法更改void实现,则无法(AFAIK)执行所需操作。在这两种情况下都可以返回字符串,在第一种情况下只返回null(无论如何都会被忽略),在这两种情况下都可以返回字符串,在第一种情况下只返回null(无论如何都会被忽略)
MyInterface myInterface = new MyClassOne();
Object myMethod = myInterface.myMethod();