问题描述
我正在练习仿制药,我做了以下事情.
I am practicing generics and i did the following.
创建了一个名为man的类,如下所示.
Created a class called man as below.
public class Man{
int hands=2;
int legs=2;
boolean alive;
public Man(boolean b) {
// TODO Auto-generated constructor stub
alive=b;
}
}
创建了一个名为人"的课程
created a class called person
public class Person<Man> {
Man p;
String name;
public Person(Man p, String name) {
this.p = p;
this.name = name;
}
public Person() {
// TODO Auto-generated constructor stub
}
}
试图实例化如下所示的string或int类型的人,并且它抛出错误.
tried to instantiate a person of type string or int as below and it dosent throw an error.
Person<String> p1 = new Person<String>(new String("test"), "test1");
Person<Integer> p2=new Person<>(1,"test");
当我仔细检查时,收到以下警告.类型参数Man正在隐藏类型Man"
when i checked carefully, i got the following warning. "The type parameter Man is hiding the type Man"
所以我知道在类Java例子中给Man作为类Person的类型参数与给'E'一样好.它不是Man.class.为什么会这样呢?如何将我的Man.class分配为通用类型参数?
So i understand that giving Man as a type parameter to the class Person is as good as giving 'E' in java examples. Its not Man.class. Why did this happen?How do i assign my Man.class as the generic type parameter?
您能给我扔一些好的问题/练习来学习泛型吗?
and can you throw me some good questions/practice exercises to learn generics.
推荐答案
因为 Man
是同一包中的类的名称.只需将通用名称从 Person< Man>
更改为 Person< T>
或其他与 Man
都不相似的名称即可其他班级.
Because Man
is the name of a class in the same package. Just change the name of your generic from Person<Man>
to Person<T>
or something else that doesn't resemble nor Man
nor any other class.
有关泛型的更多信息:
看起来您希望 Person
使用泛型仅具有 Man
属性.您将需要具有一个定义通用类的超类,并且 Person
将从其继承,以定义它使用 Man
作为所使用的类.简短示例:
Looks like you want that Person
has only Man
attribute using generics. You will need for that to have a super class that defines the generic and Person
will inherit from it defining that it uses Man
as the class used. Short example:
public abstract class AliveBeing<T> {
protected T livingBeing;
public AliveBeing(T livingBeing) {
this.livingBeing = livingBeing;
}
}
public class Person extends AliveBeing<Man> {
//now your livingBeing field is from Man type...
public Person(Man man) {
super(man);
}
}
这可以做到,但IMO的这种设计很奇怪.最好多解释一下您当前的问题,以获得更好,更准确的答案.
This will make it but IMO this design is odd. It would be better to explain more about your current problem to get a better and accurate answer.
请注意区别:
//case 1
public class Foo<T> {
//...
}
//case 2
public class Bar<E> extends Foo<E> {
//...
}
//case 3
public class Baz extends Foo<String> {
//...
}
在第一种情况下, Foo
使用泛型类,您可以通过 T
引用此类.在第二种情况下, Bar
是 Foo
的扩展,并使用其自己的泛型类进行声明,您可以通过 E
引用它,但是此E
将与 Foo
中使用的泛型相同(而不是 T
,它将是 E
).在第三种情况下, Baz
类从 Foo
扩展而来,但是它没有定义任何通用行为,而是声明了 Foo
中使用的通用类.是 String
.
In first case, Foo
uses a generic class and you can refer to this class by T
. In second case, Bar
extends from Foo
and declares using its own generic class and you can refer to it by E
, but this E
will be the same generic used from Foo
(instead of T
it will be E
). In third case, Baz
class extends from Foo
but it won't define any generic behavior, instead it declares that the generic class used from Foo
is String
.
这篇关于类型参数E隐藏类型E.泛型模板到一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!