gpt4 book ai didi

java - 更改返回类型为接口(interface)中 Object 的重写方法的返回类型 - java

转载 作者:行者123 更新时间:2023-11-29 03:39:26 30 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Overriding return type in extended interface - Bad idea?

让我们考虑以下接口(interface),

public interface Cat{
public Object getCat();//returns any type of object
}

public interface DomesticCatInterface{
public String somethinOnlyRelatedToDomesticCat();
}

public interface WildCatInterface{
public String somethinOnlyRelatedToWildCat();
}

public class DomesticCat implements Cat, DomesticCatInterface{
@Override
public DomesticCat getCat(){
return this;
}

@Override
public String somethinOnlyRelatedToDomesticCat(){
return "something";
}

}

public class WildCat implements Cat, WildCatInterface{
@Override
public WildCat getCat(){
return this;
}

@Override
public String somethinOnlyRelatedToWildCat(){
return "something";
}
}

当我需要从类中获取引用时,这是一个很好的做法吗?

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com