欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java 简单练习 ——接口的应用

程序员文章站 2022-06-13 18:05:27
...

First ,这是一篇很水的博客,我写给自己,不推荐看;

题目

输入圆,比大小;

代码

public class Main {
    public static void main(String[] args) {
      ComparableCircle c1=new ComparableCircle(2.1);
      ComparableCircle c2=new ComparableCircle(2.3);
      ComparableCircle c3=new ComparableCircle(2.1);

      int a1=c1.compareTo(c2);
        int a2=c1.compareTo(c3);
         int a3=c2.compareTo(c3);

      System.out.println(a1);
        System.out.println(a2);
         System.out.println(a3);
    }
}
interface CompareObject{
    public int compareTo(Object o);
}
class Circle{
     private double radius;

     public Circle(){
         super();
     }

     public Circle(double radius){
         super();
         this.radius=radius;
     }

     public double getRadius(){
         return radius;
     }

     public void setRadius(double radius){
         this.radius=radius;
     }


}
class ComparableCircle extends Circle implements CompareObject{
    public ComparableCircle(double radius){
        super(radius);
    }
    public int compareTo(Object o){
       if(this==o){
           return 0;
       }else if(o instanceof ComparableCircle){
           ComparableCircle c=(ComparableCircle) o;

            if(this.getRadius()>c.getRadius()) return 1;
            else if(this.getRadius()<c.getRadius()) return -1;
            else return 0;
       }
        throw new RuntimeException("传入的非CompareObject对象,不可进行比较");
    }
}
相关标签: Java 练习笔记