gpt4 book ai didi

java - Foo 的实现类可以称为 Foo 的扩展类吗?

转载 作者:行者123 更新时间:2023-12-02 01:05:36 26 4
gpt4 key购买 nike

这个问题一直让我困惑;下面是一个例子。

接口(interface)类:

public interface UserInterface{

public String make_nickname();

}

实现类:

public class UserInfo implements UserInterface{

private String name;
private String nickname;

public String make_nickName(){
this.nickname = this.name + "ish";
return this.nickname;
}


}




鉴于此,我正在调用任何实现 UserInterface 的实例像这样:

另一个远程类中的一些任意函数:

public void showNickName(<T extends UserInterface>T user){
system.out.println(user.make_nickname());
}

T用于表示 UserInterface 的(通用)实现类.
但这里我将其称为<T exnteds UserInterface>据我所知,这意味着 T 是 UserInterface子类(不是实现类) .
这是调用 UserInfo 实例的合法语法吗? ?
不知何故,上面的代码似乎对我有用。

欢迎任何反馈。

最佳答案

Java tutorial on bounded type parameters 中所述:

In generics context, extends is used in a general sense to mean either "extends" (as in classes) or "implements" (as in interfaces).

关于java - Foo 的实现类可以称为 Foo 的扩展类吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60088972/

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