用泛型定义抽象方法 [英] Define abstract methods with generics

查看:552
本文介绍了用泛型定义抽象方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读泛型,但在Java中实现一个简单的层次结构时仍然存在一些基本问题。目标是定义一个抽象方法来比较同一类的两个对象,如果对象来自不同的类,它应该返回false。例子:

我们定义了抽象类Sound

  abstract class Sound {
public boolean check(Sound d){return false;};
}

一些扩展Sound的类和另一个扩展它的抽象类,如Alarm

 抽象类Alarm扩展声音{
@Override
public boolean check(Alarm d){//做一些事情并返回布尔值};
}

有更多的类可以扩展它。



简单的解决方案是将方法定义为它的显示方式,它会起作用,但是我觉得有更好的方法来强制执行层次结构,所以Sound类定义了该方法只能用于参数这是同一类。



我用泛型试了一下:

 抽象类声音{
public< T扩展声音>布尔检查(T d){返回false;};
}

抽象类Alarm扩展声音{
@Override
public< T extends Alarm>布尔检查(T d){//做某事并返回布尔值};
}

 抽象类Alarm扩展声音{
@Override
public boolean check(Alarm d){//检查并返回boolean};



$ b <$>

Java抱怨是因为对编译器我不重写Alarm中的检查方法。
任何线索都可能缺少什么?

解决方案

您的想法本质上是错误的。通用方法的要点是 caller 可以指定符合约束的任何类型。

换句话说,调用者可以写入

 声音。< NonAlarm>检查(...); 

其中 NonAlarm 是任何继承声音



警报继承声音,其 check()方法必须具有相同的约束。



相反,您可以使用 CRTP

 公共抽象类声音< T扩展声音< T>> {
public abstract boolean check(T d);
}

公共类警报扩展声音<警报> {
@Override
public boolean check(Alarm d){...}
}


I've been reading a lot on generics but I still have some basic problems implementing a simple hierarchy in Java. The goal is to define an abstract method that will compare two objects of the same class, if the objects are from different classes it should return false. Example:

We define the abstract class Sound

abstract class Sound{
    public boolean check(Sound d){return false;};
}

Some classes that extend Sound and another abstract class that extends from it, like Alarm

abstract class Alarm extends Sound{
    @Override
    public boolean check(Alarm d){//Do something and return boolean};
}

With some more classes that extend from it.

Simple solution would be to define the method as its shown and it will work, BUT I feel there's a better way to enforce the hierarchy so the Sound class defines that the method should be used only with a parameter that is of the same class.

What I've tried with Generics:

abstract class Sound{
    public <T extends Sound> boolean check(T d){return false;};
}

abstract class Alarm extends Sound{
    @Override
    public <T extends Alarm> boolean check(T d){//Do something and return boolean};
}

or

abstract class Alarm extends Sound{
    @Override
    public boolean check(Alarm d){//check and return boolean};
}

Java complains because to the compiler I'm not overriding the check method in Alarm. Any clue on what could be missing?

解决方案

Your idea is inherently wrong.
The point of a generic method is that the caller can specify any type that meets the constraints.
In other words, the caller can write

sound.<NonAlarm> check(...);

Where NonAlarm is any class that inherits Sound.

Since Alarm inherits Sound, its check() method must have the same constraint.

Instead, you can use the CRTP:

public abstract class Sound<T extends Sound<T>> {
    public abstract boolean check(T d);
}

public class Alarm extends Sound<Alarm> {
    @Override
    public boolean check(Alarm d) { ... }
}

这篇关于用泛型定义抽象方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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