如何创建一个不返回任何特定类型的通用抽象函数?

huangapple go评论59阅读模式
英文:

How do you make a generic abstract function that does not return any particular type?

问题

目前,我有一个名为Entity的超类。

abstract class Entity {
    private String name;
    private Area area;

    public Entity(String name, Area area) {
        this.name = name;
        this.area = area;
    }

    private static void entityWait() {
        Time.sleep(Random.nextInt(50, 600));
    }

    private String getName() {
        return name;
    }

    private Area getArea() {
        return area;
    }

    abstract <T> T getNearest();

    abstract boolean validate();
}

就我所理解的而言,这表明T是一个泛型,返回类型可以设置为任何类型。

abstract <T> T getNearest();

然后,我有一个子类,我从Entity类进行扩展:

public class Mob extends Entity {

    private String name;
    private Area area;

    public Mob(String name, Area area) {
        super(name, area);
    }

    @Override
    Npc getNearest() {
        Npc npc = Npcs.getNearest(name);
        return npc;
    }
}

最终,我将会有一个SceneObject类和一个Item类,它们都指代游戏世界中的物品,这些物品将需要相同的功能。因此,最终在SceneObject类中我会这样做:

@Override
SceneObject getNearest() {
    SceneObject object = SceneObjects.getNearest(name);
    return object;
}

然而,我得到了一个未经检查的覆盖警告 - 返回类型需要未经检查的转换。这是什么意思,我该如何以正确的方式解决这个问题?

英文:

At the moment, I have a superclass called Entity.

abstract class Entity {
    private String name;
    private Area area;

    public Entity(String name, Area area) {
        this.name = name;
        this.area = area;
    }

    private static void entityWait() {
        Time.sleep(Random.nextInt(50, 600));
    }

    private String getName() {
        return name;
    }

    private Area getArea() {
        return area;
    }

    abstract &lt;T&gt; T getNearest();

    abstract boolean validate();


}

As far as I understand, this indicates that T is a generic and the return type can be set to anything.

abstract &lt;T&gt; T getNearest();

I then have my subclass in which I extend from Entity:

public class Mob extends Entity {

private String name;
private Area area;

public Mob(String name, Area area) {
    super(name, area);
}

@Override
Npc getNearest() {
    Npc npc = Npcs.getNearest(name);
    return npc;
}

Eventually, I will have a SceneObject and Item class which refers to items in the game world which will require the same functions. So, eventually I'll have to do in the SceneObject class:

@Override
SceneObject getNearest() {
    SceneObject object = SceneObjects.getNearest(name);
    return object;
}

However, I'm getting an unchecked override warning - return type requires unchecked conversion. What does this mean and how do I do this the correct way?

答案1

得分: 3

你想将Entity本身声明为泛型:

abstract class Entity<T> {
    private String name;

    ...

    abstract T getNearest();

    ...
}

然后你的扩展类使用这个泛型参数:

public class Mob extends Entity<Npc> {

    ...

    @Override
    Npc getNearest() {
       return Npcs.getNearest(name);
    }

    ...

}
英文:

You want to declare Entity itself as generic:

abstract class Entity&lt;T&gt; {
    private String name;

    ...

    abstract T getNearest();

    ...
}

Then your extending class uses the generic parameter:

public class Mob extends Entity&lt;Npc&gt; {

    ...

    @Override
    Npc getNearest() {
       return Npcs.getNearest(name);
    }

    ...

}

答案2

得分: 1

定义带有泛型参数的 Entity 类:

abstract class Entity<T> {

从你的方法中删除泛型类型声明:

abstract T getNearest();

让你的子类扩展带有泛型类型的 Entity

public class Mob extends Entity<Npc> {
英文:

define your Entity class with generic parameters:

abstract class Entity&lt;T&gt; {

And remove the generic type declaration from your method:

abstract T getNearest();

And have your subclass extend Entity with a generic type:

public class Mob extends Entity&lt;Npc&gt; {

答案3

得分: 1

你尝试的方法违反了面向对象编程的至少两条规则。你混合了类和接口。你期望任何Entity的子类都能够满足一个未知的合约以获得最近的内容。如果我使用ArrayList&lt;IllegalStateException&gt;类型参数调用这个方法,子类必须返回这种类型。这怎么可能在编译时知道呢?

第二个问题是通过调用一个静态方法Npcs.getNearest(name)来突破Mob实例上下文的限制,这个方法可以执行任何操作,比如在世界另一边的职员桌上打印一张纸。那个人会扫描结果,并通过信鸽返回结果。然后,光学字符识别工具会杀死信鸽,提取字符并返回一个Npc,或者返回null、Runtime ExceptionOutOfMemoryError

我猜你可能考虑过通过接口表达的合约,方法getNearest返回由方法的实现者确定的内容。因此,你需要使用接口而不是类。同时也不清楚同一个类中是否可以有两个不同的getNearest方法。

也许让SceneObjectMob都有各自的getNearest方法已经足够了,不需要通过Entity来强制实现。否则,getNearest应该返回一个与源对象的距离有关的内容,甚至可以是任何源对象,这样SceneObjectgetNearest就可以与MobgetNearest进行比较,这是你的合约所暗示的。如果从未出现这种情况,那就不要使用继承。

英文:

What you are trying breaks at least two rules of object oriented programming. You mix class and interface. You expect any subclass of Entity to fulfill an unknown contract to get nearest. If I invoke this method with a type argument of ArrayList&lt;IllegalStateException&gt; the subclass must return this type. How can this be known at compile time?

The second is breaking out of the instance' context of Mob by invoking a static method Npcs.getNearest(name) that does anything…like printing out a sheet of paper on a clerks desk on the other side of the world to get nearest Npc. That person scans in the result and returns it with a carrier pigeon. An optical character recognition tool then kills the pigeon to extract the characters and returns an Npc, or null or a Runtime Exception or OutOfMemoryError.

I assume you thought about a contract like expressed through an interface and the method getNearest returns something, that is determined by the realizer of the method. Thus you need interfaces instead of classes. And it is also unclear, whether there can be a superposition of two different getNearest in the same class.

Maybe it is absolutely sufficient to let SceneObject have a getNearest of its own as well as Mob and not force this through the Entity. Otherwise getNearest ought to return something that bears a distance to its source or even any source, so getNearest of SceneObject can be compared to getNearest of Mob – what your contract suggests. If this is never the case, drop all evil inheritance.

huangapple
  • 本文由 发表于 2020年10月1日 01:11:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64142549.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定