Java 链接、返回基本对象以及与扩展类的类型不匹配

Java 链接、返回基本对象以及与扩展类的类型不匹配,java,types,polymorphism,Java,Types,Polymorphism,我遇到过这样一个班。它有一个“with”方法,可以让人们将事物链接在一起 public class Bear { protected List<String> names = new ArrayList<String>(); protected List<String> foods = new ArrayList<String>(); public Bear withName(String name) {

我遇到过这样一个班。它有一个“with”方法,可以让人们将事物链接在一起

public class Bear {
    protected List<String> names = new ArrayList<String>();
    protected List<String> foods = new ArrayList<String>();

    public Bear withName(String name) {
        names.add(name);
        return this;
    }

    public Bear withFood(String food) {
        foods.add(food);
        return this;
    }
}

// ...somewhere else
Bear b = new Bear().withName("jake").withName("fish");
给出的错误是:

类型不匹配:无法从动物转换为熊

显然,当您返回基类this时,它将返回一个Bear类型,并且不会进行任何类型的自动转换

解决/绕过此问题的方法有哪些?

您正在寻找:

公共抽象类动物{
受保护的列表名称=新的ArrayList();
带名称的公共T(字符串名称){
名称。添加(名称);
返回(T)这个;
}
}
这将给出一个不可避免的未检查强制类型转换警告,因为类型系统无法阻止您编写
class Cat extensed Animal{}class Dog extensed Animal

如果基类中有多个生成器方法,则可以通过编写
private T returnThis(){return(T)this;}
来隔离警告。您正在查找:

公共抽象类动物{
受保护的列表名称=新的ArrayList();
带名称的公共T(字符串名称){
名称。添加(名称);
返回(T)这个;
}
}
这将给出一个不可避免的未检查强制类型转换警告,因为类型系统无法阻止您编写
class Cat extensed Animal{}class Dog extensed Animal

如果基类中有多个生成器方法,则可以通过编写
private T returnThis(){return(T)this;}
来隔离警告。您正在查找:

公共抽象类动物{
受保护的列表名称=新的ArrayList();
带名称的公共T(字符串名称){
名称。添加(名称);
返回(T)这个;
}
}
这将给出一个不可避免的未检查强制类型转换警告,因为类型系统无法阻止您编写
class Cat extensed Animal{}class Dog extensed Animal

如果基类中有多个生成器方法,则可以通过编写
private T returnThis(){return(T)this;}
来隔离警告。您正在查找:

公共抽象类动物{
受保护的列表名称=新的ArrayList();
带名称的公共T(字符串名称){
名称。添加(名称);
返回(T)这个;
}
}
这将给出一个不可避免的未检查强制类型转换警告,因为类型系统无法阻止您编写
class Cat extensed Animal{}class Dog extensed Animal


如果基类中有多个生成器方法,则可以通过编写
private T returnThis(){return(T)this;}

来隔离警告,您的
Bear
类扩展了
Animal
,因此继承了声明为

public Animal withName(String name) ...
方法调用在编译时验证

Bear b = new Bear().withName("jake");  // Breaks
编译器只知道
Animal#withName(String)
返回一个
Animal
。它无法知道在运行时您实际返回的是
熊。因此,它不能让您将该值分配给

@Override
public Bear withName(String name) {
    names.add(name); // or invoke super method
    return this;
}
您可以在
Bear
类中执行操作或重写该方法,并将其返回类型更改为
Bear

@Override
public Bear withName(String name) {
    names.add(name); // or invoke super method
    return this;
}

如果对类型为
Bear
的引用调用该方法,该方法的返回类型将为
Bear

您的
Bear
类扩展了
Animal
,因此继承了声明为

public Animal withName(String name) ...
方法调用在编译时验证

Bear b = new Bear().withName("jake");  // Breaks
编译器只知道
Animal#withName(String)
返回一个
Animal
。它无法知道在运行时您实际返回的是
熊。因此,它不能让您将该值分配给

@Override
public Bear withName(String name) {
    names.add(name); // or invoke super method
    return this;
}
您可以在
Bear
类中执行操作或重写该方法,并将其返回类型更改为
Bear

@Override
public Bear withName(String name) {
    names.add(name); // or invoke super method
    return this;
}

如果对类型为
Bear
的引用调用该方法,该方法的返回类型将为
Bear

您的
Bear
类扩展了
Animal
,因此继承了声明为

public Animal withName(String name) ...
方法调用在编译时验证

Bear b = new Bear().withName("jake");  // Breaks
编译器只知道
Animal#withName(String)
返回一个
Animal
。它无法知道在运行时您实际返回的是
熊。因此,它不能让您将该值分配给

@Override
public Bear withName(String name) {
    names.add(name); // or invoke super method
    return this;
}
您可以在
Bear
类中执行操作或重写该方法,并将其返回类型更改为
Bear

@Override
public Bear withName(String name) {
    names.add(name); // or invoke super method
    return this;
}

如果对类型为
Bear
的引用调用该方法,该方法的返回类型将为
Bear

您的
Bear
类扩展了
Animal
,因此继承了声明为

public Animal withName(String name) ...
方法调用在编译时验证

Bear b = new Bear().withName("jake");  // Breaks
编译器只知道
Animal#withName(String)
返回一个
Animal
。它无法知道在运行时您实际返回的是
熊。因此,它不能让您将该值分配给

@Override
public Bear withName(String name) {
    names.add(name); // or invoke super method
    return this;
}
您可以在
Bear
类中执行操作或重写该方法,并将其返回类型更改为
Bear

@Override
public Bear withName(String name) {
    names.add(name); // or invoke super method
    return this;
}

如果对类型为
Bear
的引用调用该方法,该方法的返回类型将为
Bear

你可以,我不想搞清楚哪种方法有效,哪种方法无效。你可以,我不想搞清楚哪种方法有效,哪种方法无效。你可以,我不想搞清楚哪种方法有效,哪种方法无效。你可以,我不想搞清楚哪种方法有效,哪种方法没有。当我在自己的问题上尝试这种类型的解决方案时,我在
@Override
上得到一个错误:
方法没有从其超类重写方法