如何声明一个抽象方法,以便参数类型(类)始终是 Children 类?

发布于 2024-11-17 14:32:59 字数 1853 浏览 1 评论 0原文

编辑:见底部

首先,我在问这个问题之前搜索了答案,但正如您从标题中看到的那样,我不知道这是如何称呼的,我会尽可能编辑问题。 请原谅我这一点。

我有以下抽象类:

public abstract class ValidableDTO implements Serializable {
    public abstract boolean isValid();
    public abstract boolean equals(ValidableDTO compared);
    // EDIT : new solution
    public abstract <T extends ValidableDTO> boolean equals(T compared);
}

我想获得类似的实现:

public class MyDTO extends ValidableDTO {

    private String myValue; //With a getter and setter of course

    public MyDTO() {
        // ...
    }

    @Override
    public boolean isValid() {
        return true; // Validation
    }

// __________ LOOK AT THE FOLLOWING PARAMETER TYPE __________­­
    @Override
    public boolean equals(MyDTO compared) {
        return true; // Comparison
    }
}

我能得到的最接近的是

@Override
public boolean equals(ValidableDTO compared) {
    boolean isEqual = false;

    if (compared instanceof MyDTO) {
        isEqual = getValue().equals(compared.getValue());
    }

    return isEqual;
}

我尝试使用 public abstract boolean equals(compare); 但这并不不工作。

可能吗(在我看来,应该是这样)?谢谢您的宝贵时间...我仍然不知道如何用 1 句话来描述这一点...(笑)

真诚的。 - 我

更近了一步,感谢用户:aps!

以下工作在抽象类定义(ValidableDTO)

public abstract <T extends ValidableDTO> boolean equals(T compared);

__BUT__

中实现MyDTO 仍然不太好,最后,它与使用我的“if instanceof”解决方案完全相同,因为 ValidableDTO 是一个抽象类,必须继承。

现在使用 Aps 的解决方案看起来是什么样子:

public <T extends ValidableDTO> boolean equals(T compared) { ... }

我仍然需要检查它是否是 MyDTO 实例...

另一方面,谷歌似乎不知道“Validable”或“Validatable”是否是真实的单词..一个是正​​确的吗?

EDIT : see bottom

First off I searched for an answer before asking this one, but as you can see with the title I have no idea how this is called and I will edit the question whenever I can.
Please forgive me on this.

I have the following abstract class :

public abstract class ValidableDTO implements Serializable {
    public abstract boolean isValid();
    public abstract boolean equals(ValidableDTO compared);
    // EDIT : new solution
    public abstract <T extends ValidableDTO> boolean equals(T compared);
}

I'd like to get a similar implementation :

public class MyDTO extends ValidableDTO {

    private String myValue; //With a getter and setter of course

    public MyDTO() {
        // ...
    }

    @Override
    public boolean isValid() {
        return true; // Validation
    }

// __________ LOOK AT THE FOLLOWING PARAMETER TYPE __________­­
    @Override
    public boolean equals(MyDTO compared) {
        return true; // Comparison
    }
}

The closest I could get is

@Override
public boolean equals(ValidableDTO compared) {
    boolean isEqual = false;

    if (compared instanceof MyDTO) {
        isEqual = getValue().equals(compared.getValue());
    }

    return isEqual;
}

I tried using public abstract boolean equals(<? extends ValidableDTO> compared); but this doesn't work.

Is that even possible (it should be IMO) ? Thank you for your time and ... I still don't know how to describe this in 1 sentence... (lol)

Sincerely.
- me

One step closer, thanks to user : aps !

the following works in the Abstract class definition (ValidableDTO)

public abstract <T extends ValidableDTO> boolean equals(T compared);

__BUT__

the implementation in MyDTO still isn't fine and in the end, it's exactly the same as using my 'if instanceof' solution because ValidableDTO is an abstract class and HAS to be inherited.

What it looks like for now using Aps' solution :

public <T extends ValidableDTO> boolean equals(T compared) { ... }

I still have to check if it's a MyDTO instance...

ON A SIDE NOTE, google doesn't seem to know if "Validable" or "Validatable" are real words.. which one is correct?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的绿洲 2024-11-24 14:32:59

您可以在抽象类的方法签名声明上使用泛型来实现这一点:

public abstract class ValidableDTO<T extends ValidableDTO> implements Serializable {
    public abstract boolean isValid();
    public abstract boolean equals(T compared);
}

public class MyDTO extends ValidableDTO<MyDTO> {

    @Override
    public boolean isValid() {
        ...
    }


    @Override
    public boolean equals(MyDTO compared) {
        return true; // Comparison
    }
}

[编辑]

下面的评论讨论摘要:

关于 Java 编码风格和原则,它重写默认的 public boolean equals(Object other) 方法而不是使用泛型构造来尝试约束参数的类型会更合理。 equals(...) 的标准做法是使用 instaceof 运算符检查参数兼容性、执行强制转换并在特定对象结构级别使用更细粒度的比较。这与原始帖子中提议的替代实现类似。

 @Override
public boolean equals(Object other) {
    if (this == other) return true;
    if (other instanceof MyDTO) { 
        MyDTO otherDTO = (MyDTO) other; 
        return this.getValue().equals(otherDTO.getValue()); // mind null values
    } 
    return false; 
}

此方法具有使类准备好在集合中使用的附加价值,这是 DTO 类的常见情况(例如 List

You can achieve that using generics on the declaration of your method signature at the abstract class:

public abstract class ValidableDTO<T extends ValidableDTO> implements Serializable {
    public abstract boolean isValid();
    public abstract boolean equals(T compared);
}

public class MyDTO extends ValidableDTO<MyDTO> {

    @Override
    public boolean isValid() {
        ...
    }


    @Override
    public boolean equals(MyDTO compared) {
        return true; // Comparison
    }
}

[EDIT]

Summary of the comments discussion below:

Regarding Java coding style and principles, it would be more reasonable to override the default public boolean equals(Object other) method instead of using a generics construction to try to constrain the type of the parameter. The standard practice for equals(...) is to use the instaceof operator to check for parameter compatibility, perform a cast and use finer-grained comparisons at the level of the specific object structure. That's similar to the proposed alternative implementation in the original post.

 @Override
public boolean equals(Object other) {
    if (this == other) return true;
    if (other instanceof MyDTO) { 
        MyDTO otherDTO = (MyDTO) other; 
        return this.getValue().equals(otherDTO.getValue()); // mind null values
    } 
    return false; 
}

This approach has the additional value of making the class ready for use in a collection, which is a very common case for DTO classes (e.g. List<UserDTO>)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文