Java 如何将方法与比较类型的附加功能进行比较?

Java 如何将方法与比较类型的附加功能进行比较?,java,comparator,Java,Comparator,如果对象属于参数中提供的特定类型,我想使用类型为的附加参数创建一个比较器,以便为属性添加更多优先级。比如说, new Comparator<Person>(){ @override public int compare(Person p1, Person p2, Person.Type type, float weight) { float score1 = p1.getScore(); float score2 = p2.g

如果对象属于参数中提供的特定类型,我想使用类型为的附加参数创建一个比较器,以便为属性添加更多优先级。比如说,

new Comparator<Person>(){ 
    @override
    public int compare(Person p1, Person p2, Person.Type type, float weight)
    {
        float score1 = p1.getScore();
        float score2 = p2.getScore();
        if(p1.getType==type)
            score1 = weight * score1;
        if(p2.getType==type)
            score2 = weight * score2;
        return Double.compare(score1,score2);
    }
}
newcomparator(){
@凌驾
公共整数比较(人员p1、人员p2、人员类型、浮动重量)
{
float score1=p1.getScore();
float score2=p2.getScore();
if(p1.getType==type)
分数1=重量*分数1;
if(p2.getType==type)
分数2=重量*分数2;
返回双倍。比较(分数1,分数2);
}
}

我想找到一种方法,在对象是特定类型时实现这种行为

由于额外的参数,您的
compare
方法不再实现
Comparator

要提供这些值,请将构造函数中的这些值传递到此
比较器

public class PersonComparator implements Comparator<Person>
{
    private Person.Type type;
    private float weight;
    public PersonComparator(Person.Type type, float weight) {
       this.type = type;
       this.weight = weight;
    }
}

由于额外的参数,您的
compare
方法不再实现
Comparator

要提供这些值,请将构造函数中的这些值传递到此
比较器

public class PersonComparator implements Comparator<Person>
{
    private Person.Type type;
    private float weight;
    public PersonComparator(Person.Type type, float weight) {
       this.type = type;
       this.weight = weight;
    }
}

您不能修改正在运行的接口

public int compare(T, T);
所以,如果你想增加重量和类型,我建议你添加一些东西,比如比较器的字段

public class  YourComparator implements Comparator<Person> { 
    private Person.Type type;
    private float weight;

    public YourComparator(Person.Type type, float weight) {
       this.type = type;
       this.weight = weight;
    }

    @override
    public int compare(Person p1, Person p2) {
        float score1 = p1.getScore();
        float score2 = p2.getScore();
        if(p1.getType==this.type)
            score1 = this.weight * score1;
        if(p2.getType==this.type)
            score2 = this.weight * score2;
        return Double.compare(score1,score2);
    }
}
公共类YourComparator实现Comparator{
私人型;
私人浮重;
公共比较器(Person.Type、float weight){
this.type=type;
重量=重量;
}
@凌驾
公共整数比较(人员p1、人员p2){
float score1=p1.getScore();
float score2=p2.getScore();
if(p1.getType==this.type)
score1=该重量*score1;
if(p2.getType==this.type)
score2=这个重量*score2;
返回双倍。比较(分数1,分数2);
}
}
如果要使用匿名类实现,可以在容器方法(或容器对象中的字段)中将这些属性设置为final,并直接引用它们

final Person.Type type = Person.Type.SUPER_HEROE;
final float weight = 0.38f;

Comparator<Person> comparator = new Comparator<Person>() { 
    @Override
    public int compare(Person p1, Person p2) {
        float score1 = p1.getScore();
        float score2 = p2.getScore();
        if(p1.getType==type)
            score1 = weight * score1;
        if(p2.getType==type)
            score2 = weight * score2;
        return Double.compare(score1,score2);
    }
};
final Person.Type=Person.Type.SUPER\e;
最终浮子重量=0.38f;
比较器比较器=新比较器(){
@凌驾
公共整数比较(人员p1、人员p2){
float score1=p1.getScore();
float score2=p2.getScore();
if(p1.getType==type)
分数1=重量*分数1;
if(p2.getType==type)
分数2=重量*分数2;
返回双倍。比较(分数1,分数2);
}
};

您无法修改正在运行的界面

public int compare(T, T);
所以,如果你想增加重量和类型,我建议你添加一些东西,比如比较器的字段

public class  YourComparator implements Comparator<Person> { 
    private Person.Type type;
    private float weight;

    public YourComparator(Person.Type type, float weight) {
       this.type = type;
       this.weight = weight;
    }

    @override
    public int compare(Person p1, Person p2) {
        float score1 = p1.getScore();
        float score2 = p2.getScore();
        if(p1.getType==this.type)
            score1 = this.weight * score1;
        if(p2.getType==this.type)
            score2 = this.weight * score2;
        return Double.compare(score1,score2);
    }
}
公共类YourComparator实现Comparator{
私人型;
私人浮重;
公共比较器(Person.Type、float weight){
this.type=type;
重量=重量;
}
@凌驾
公共整数比较(人员p1、人员p2){
float score1=p1.getScore();
float score2=p2.getScore();
if(p1.getType==this.type)
score1=该重量*score1;
if(p2.getType==this.type)
score2=这个重量*score2;
返回双倍。比较(分数1,分数2);
}
}
如果要使用匿名类实现,可以在容器方法(或容器对象中的字段)中将这些属性设置为final,并直接引用它们

final Person.Type type = Person.Type.SUPER_HEROE;
final float weight = 0.38f;

Comparator<Person> comparator = new Comparator<Person>() { 
    @Override
    public int compare(Person p1, Person p2) {
        float score1 = p1.getScore();
        float score2 = p2.getScore();
        if(p1.getType==type)
            score1 = weight * score1;
        if(p2.getType==type)
            score2 = weight * score2;
        return Double.compare(score1,score2);
    }
};
final Person.Type=Person.Type.SUPER\e;
最终浮子重量=0.38f;
比较器比较器=新比较器(){
@凌驾
公共整数比较(人员p1、人员p2){
float score1=p1.getScore();
float score2=p2.getScore();
if(p1.getType==type)
分数1=重量*分数1;
if(p2.getType==type)
分数2=重量*分数2;
返回双倍。比较(分数1,分数2);
}
};

那么问题是什么呢?看起来您已经在检查类型了。现在还不清楚问题是什么——这段代码不起作用吗?看起来没有。在任何情况下,这可能与此类似:那么,问题是什么?看起来您已经在检查类型了。目前还不清楚问题是什么--这段代码是否不起作用?看起来没有。在任何情况下,这可能类似于: