Java中的通用字段多态性

Java中的通用字段多态性

本文介绍了Java中的通用字段多态性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public< M extends AgeFactor>< p> void setAgeFActor(M m){
System.out.println(referene receiveed+ m.factor); //它应该引用什么? AgeFactor或类型运行时给出的年轻
this.factor = m.factor;

$ / code>

在上面的代码中,参考 M m 引用 AgeFactor 或我在运行时给出的类型。
假设我通过以下方式调用它:

  ath1.setAgeFActor(new young()); // ath1指的是setAgeFActor方法的类

,层次结构为:

  class AgeFactor {
String factor;
}

年轻人延伸AgeFactor {
弦乐系数=年轻;

public String toString(){
return+ factor +;

}
}

经验丰富的课程延伸AgeFactor {
String factor =经验丰富;

public String toString(){
return+ factor +;



$ b class pro扩展AgeFactor {
String factor =Pro;

public String toString(){
return+ factor +;


$ b code
$ b

我想知道的是为什么 m.factor 引用 AgeFactor 的字段因子? ? m.factor 应引用 young pro 类型

如果m实际上是指AgeFactor,那么我明白m.factor总是会引用AgeFactor的因子,因为多态性不适用于字段。如果是这样,那么泛型的需求是什么?
Java的泛型本身是通用的吗?
请求帮助我获得泛型。

解决方案

字段不以方法的方式重写。如果你想要这种多态性,你必须使用方法来实现,而不是使用字段。



这里,经历 pro 有两个不同的字段,分别是 factor ,一个来自 AgeFactor 经历 pro 中的一个。它具有相同名称的事实完全是巧合:如果你有一个对象,并且所有已知的对象都是 AgeFactor ,那么 AgeFactor 版本的字段将被使用。



这个设计的一个更明智的版本可能是只有一个字段名为 factor ,并将其设置在每个类的构造函数中。


I have really got confused with Generics please help me with my situation:

public <M extends AgeFactor> void setAgeFActor(M m){
    System.out.println("referene receiveed"+m.factor); //WHAT SHOULD IT REFER TO?? AgeFactor or type given at Runtime as young
    this.factor=m.factor;
}

In the above code the reference M m refers to AgeFactor or the type that i give at Runtime.Suppose i call it via:

ath1.setAgeFActor(new young()); //ath1 is referring to setAgeFActor method's class

and hierarchy being:

class AgeFactor {
    String factor;
}

class young extends AgeFactor {
    String factor = "Young";

    public String toString() {
        return " " + factor + " ";

    }
}

class experienced extends AgeFactor {
    String factor = "Experienced";

    public String toString() {
        return " " + factor + " ";

    }
}

class pro extends AgeFactor {
    String factor = "Pro";

    public String toString() {
        return " " + factor + " ";

    }
}

What i want to know is why m.factor refer to AgeFactor's field factor ?? m.factor should referring to young or pro the type i give at Runtime.

If m is actually referring to AgeFactor then i understand that m.factor will refer to AgeFactor's factor always because polymorphism is not applied to fields. If so, then what is the need of generics?Are Java's generics generic itself?Pleas help me getting over Generics.

解决方案

Fields aren't overridden in the way that methods are. If you want this kind of polymorphism, you have to do it with methods, not with fields.

Here, experienced and pro have two different fields named factor, one from AgeFactor, and one from experienced and pro. The fact that it has the same name is completely coincidental: if you have an object, and all that's known about it is that it's an AgeFactor, then the AgeFactor version of that field will be used.

A more sensible version of this design might be to have only one field named factor, and to set it in the constructors of each class.

这篇关于Java中的通用字段多态性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 02:49