简体   繁体   English

比较器在Java中的接口实现

[英]Interface implementation in java by comparator

I got confused while stuyding java in deep with the core concept. 在深入学习Java的核心概念时,我感到困惑。

what i have studied is, if you have interface 我研究的是,如果您有界面

public interface interfaceconcept {

    public void heyyou(String s);
    public abstract void nono(String s);
    public void kolk(int i);


}

and you have a class which implements the interface then you have to write the body of all the methods 并且您有一个实现接口的类,那么您必须编写所有方法的主体

  public class implementation implements interfaceconcept{

    @Override
    public void heyyou(String s) {
        // TODO Auto-generated method stub

    }

    @Override
    public void nono(String s) {
        // TODO Auto-generated method stub

    }

    @Override
    public void kolk(int i) {
        // TODO Auto-generated method stub

    }

    }

if you dont want to write the methods you can use abstract class 如果您不想编写方法,则可以使用抽象类

Now i was studing about comparator, and while inpecting its class i found it is an interface 现在我正在研究比较器,在检查其类时,我发现它是一个接口

public interface Comparator<T> { 
//
}

now it implemented it in another class, firstly it gave me an error saying. 现在它在另一个类中实现了它,首先它给了我一个错误的说法。

The type Order must implement the inherited abstract method Comparator.compare(Order, Order) Order类型必须实现继承的抽象方法Comparator.compare(Order,Order)

i have added all the method by clicking add unimplemented method and remove all but one. 我已通过单击添加未实现的方法添加了所有方法,并删除了除一个以外的所有方法。

but when i compiled it, it simply compiled why? 但是当我编译它时,它只是编译了为什么? am i not supppose to add all the unimplemented methods of comparator interface as per the rules of java? 我是否应该按照Java的规则添加比较器接口的所有未实现的方法?

Also one of its method in comparator class is 比较器类中的一种方法是

default Comparator<T> reversed() {
        return Collections.reverseOrder(this);
    }

what is the point if you have written the implementation of one method in interface itself? 如果您在接口本身中编写了一种方法的实现,那又有什么意义呢? you cant write any body of the method, if im correct. 您不能写该方法的任何主体,如果我是正确的。

public class Order implements Comparator<Order>{

    @Override
    public int compare(Order o1, Order o2) {
        // TODO Auto-generated method stub
        return 0;
    }


    }

If you look at the Javadocs for Comparator<T> https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html you'll see that the only abstract method that is not default , not static , and not already implemented via inheritance from Object is compare(T, T) . 如果您查看Comparator<T>的Javadocs https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html ,就会发现唯一的default方法不是default ,不是static ,并且尚未通过Object继承实现的是compare(T, T) So it's the only method that is unimplemented in the interface, and therefore when you implement it you have implemented all the unimplemented methods in the interface. 因此,它是接口中唯一未实现的方法,因此,当您实现它时,您已经在接口中实现了所有未实现的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM