如何在java中使用桥接技术实现协变方法重写 [英] How covariant method overriding is implemented using bridging Technique in java

查看:132
本文介绍了如何在java中使用桥接技术实现协变方法重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读Covariant Overriding时,我发现很奇怪的事实,


协变方法覆盖是使用桥接技术实现的。
它也表示这个特性在java5及以上版本中实现(我认为这是因为java5中引入的泛型)

它是如何发生的。请帮助我举例说明。

解决方案

考虑一个例子:

  public interface Shape< T extends Shape< T>> {
T getType();
void setType(T type);
}

public class Circle实现形状< Circle> {
Circle getType(){}
void setType(Circle circle){}
}

现在看起来不错。但是,在类型擦除之后,接口松散它的泛型类型,并且类型 T 被替换为上限。所以接口和类看起来像:

  public interface Shape {
Shape getType();
void setType(Shape type);
}

public class Circle实现Shape {
Circle getType(){}
void setType(Circle circle){}
}

现在,问题就出在这里。擦除后的 Circle 中的方法并不是真正的 Shape 的重写版本。请注意,现在看起来的这些方法会对所需的参数以及返回的值应用更大的限制。这是因为擦除会改变界面中方法的签名。 为了解决这个问题,编译器为那些将调用委托给类中的实际方法的人添加了桥接方法。


$ b $因此,该类实际上转换为:

  public class Circle implements Shape {
Circle getType ){}
void setType(Circle circle){}

//编译器添加的桥接方法。
Shape getType(){return getType(); } //代表实际的方法
void setType(Shape shape){setType((Circle)shape); } //代表实际方法
}

所以,桥接方法现在被覆盖版本的接口中的方法,他们将调用委托给执行该任务的实际方法。



请注意,桥接方法中使用的类型是擦除接口的类型参数,在这种情况下 Shape






< 参考文献:


While reading on Covariant Overriding, i find out very strange fact,

covariant method overriding is implemented using a bridging technique. it also said that this feature is implemented in java5 and above.(i think it is because generics introduced from java5)

How it happens.Please help me with example.

解决方案

Consider an example:

public interface Shape<T extends Shape<T>> {
    T getType();
    void setType(T type);
}

public class Circle implements Shape<Circle> {
    Circle getType() { }
    void setType(Circle circle) {  }
}

It looks good as of now. But, after type erasure, the interface loose it's generic type, and the type T is replaced with the upper bound. So the interface and class looks like:

public interface Shape {
    Shape getType();
    void setType(Shape type);
}

public class Circle implements Shape {
    Circle getType() { }
    void setType(Circle circle) {  }
}

Now, here's the problem. The method in Circle after erasure is not really an overridden version of Shape. Note that, now the methods as it looks, applies greater restriction on the parameter it takes, and the value it returns. This is because the erasure changes the signature of method in interface.

To solve this issue, the compiler adds bridge method for those, which delegates the call to those actual methods in class.

So, the class is really converted to:

public class Circle implements Shape {
    Circle getType() { }
    void setType(Circle circle) {  }

    // Bridge method added by compiler.
    Shape getType() { return getType(); }  // delegate to actual method
    void setType(Shape shape) { setType((Circle)shape); }  // delegate to actual method
}

So, the bridge method is now the overridden version of the methods in the interface, and they delegate the call to the actual method that does the task.

Note that the type used in the bridge method is the erasure of the type parameter of the interface, in this case Shape.


References:

这篇关于如何在java中使用桥接技术实现协变方法重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆