如何声明一个抽象方法,以便参数类型(类)始终是Children类? [英] How do you declare an abstract method so that the parameter type (class) is always the Children class?

查看:179
本文介绍了如何声明一个抽象方法,以便参数类型(类)始终是Children类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:见底部



首先我在问这个之前搜索了一个答案,但正如你所看到的标题我我不知道如何调用它,我会随时编辑问题。
请原谅我。



我有以下抽象类:

 公共抽象类ValidableDTO实现Serializable {
public abstract boolean isValid();
public abstract boolean equals(ValidableDTO比较);
//编辑:新解决方案
public abstract< T extends ValidableDTO> boolean equals(T比较);
}

我想得到类似的实现:

 公共类MyDTO扩展ValidableDTO {

private String myValue; //使用getter和setter当然是

public MyDTO(){
// ...
}

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

// __________查看以下参数类型__________
@Override
公共布尔值等于(MyDTO比较){
返回true ; //比较
}
}

我能得到的最接近的是

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

if(比较myDTO的实例){
isEqual = getValue()。equals(comparative.getValue());
}

return isEqual;
}

我尝试使用 public abstract boolean equals(< ?扩展ValidableDTO>比较); 但这不起作用。



甚至可能(它应该是IMO)?感谢您的时间和...我仍然不知道如何用一句话来形容...(笑)



此致。
- 我



靠近一点,多亏了用户:aps!



以下在Abstract类定义中工作(ValidableDTO)

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

__ __



MyDTO中的实现仍然没有问题,最后,它与使用我的'if instanceof'解决方案完全相同,因为ValidableDTO是一个抽象类,而且要继承HAS。 / p>

现在使用Aps解决方案看起来如何:

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

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



在一边注意,google似乎不知道Validable或Validatable是否是真正的单词..哪一个是正确的?

解决方案

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

 公共抽象类ValidableDTO< T extends ValidableDTO>实现Serializable {
public abstract boolean isValid();
public abstract boolean equals(T compare);
}

公共类MyDTO扩展了ValidableDTO< MyDTO> {

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


@Override
public boolean equals(MyDTO比较){
return true; //比较
}
}



以下评论摘要摘要:



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

  @Override 
public boolean equals(Object other){
if(this == other)return true;
if(MyDTO的其他实例){
MyDTO otherDTO =(MyDTO)other;
返回this.getValue()。equals(otherDTO.getValue()); // mind null values
}
返回false;
}

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


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?

解决方案

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>)

这篇关于如何声明一个抽象方法,以便参数类型(类)始终是Children类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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