zoukankan      html  css  js  c++  java
  • 14.2

     1 import java.awt.*;
     2 import java.util.Scanner;
     3 
     4 import javax.swing.*;
     5 
     6 public class Test extends JFrame {
     7   public static void main(String[] args){
     8       Comparable c1 = new ComparableCircle();
     9       System.out.println("c1 is "+new ComparableCircle());
    10       System.out.println(c1);
    11       Comparable c2 = new ComparableCircle(2.0);
    12       
    13       System.out.println("the c1's area is: "+((Circle)c1).getArea());
    14       System.out.println("the c2's area is: "+((Circle)c2).getArea());
    15       
    16       Comparable c3 = null ;
    17       System.out.println("compare's result is :"+c1.compareTo(c2));
    18       
    19       switch(c1.compareTo(c2))
    20       {
    21       case 1: { c3 = c1; break;}
    22       case -1: { c3 = c2; System.out.println("c3 = c2");break;}
    23       case 0: {c3= c1; break;}
    24       }
    25       System.out.println("c1 address is"+c1+"
    "+"c2 address is"+c2+"
    "+"c3 address is"+c3);
    26       if(c3 instanceof Circle)
    27       System.out.print("the bigger area is : "+((Circle)c3).getArea()+"
    ");
    28       else
    29           System.out.println("c1 is not ComparableCircle type");
    30   }  
    31 }
    Test.java
     1 public class ComparableCircle extends Circle implements Comparable{
     2 
     3     public ComparableCircle()
     4     {
     5         
     6     }
     7     public ComparableCircle(double radius) {
     8         // TODO Auto-generated constructor stub
     9         super(radius);
    10     }
    11 
    12     @Override
    13     public int compareTo(Object o) throws ClassCastException{
    14         // TODO Auto-generated method stub
    15         try{
    16             if(this.getArea() > ((ComparableCircle)o).getArea())
    17             return 1;
    18         else if(this.getArea() < ((ComparableCircle)o).getArea())
    19             return -1;
    20         else
    21         return 0;
    22     }
    23     catch(ClassCastException  ex)
    24     {
    25         System.out.println("the class type cannot be transfered!");
    26         return 0;
    27     }
    28     }
    29 }
    ComparableCircle.java
     1 // Circle.java: The circle class that extends GeometricObject
     2 public class Circle  {
     3   private double radius;
     4 
     5   /**Default constructor*/
     6   public Circle() {
     7     this(1.0);
     8   }
     9 
    10   /**Construct circle with a specified radius*/
    11   public Circle(double radius) {
    12     this(radius, "white", false);
    13   }
    14 
    15   /**Construct a circle with specified radius, filled, and color*/
    16   public Circle(double radius, String color, boolean filled) {
    17    
    18     this.radius = radius;
    19   }
    20 
    21   /**Return radius*/
    22   public double getRadius() {
    23     return radius;
    24   }
    25 
    26   /**Set a new radius*/
    27   public void setRadius(double radius) {
    28     this.radius = radius;
    29   }
    30 
    31   /**Implement the getArea method defined in GeometricObject*/
    32   public double getArea() {
    33     return radius*radius*Math.PI;
    34   }
    35 
    36   /**Implement the getPerimeter method defined in GeometricObject*/
    37   public double getPerimeter() {
    38     return 2*radius*Math.PI;
    39   }
    40 
    41   /**Override the equals() method defined in the Object class*/
    42   public boolean equals(Circle circle) {
    43     return this.radius == circle.getRadius();
    44   }
    45 
    46   /**Override the toString() method defined in the Object class*/
    47   public String toString() {
    48     return "[Circle] radius = " + radius;
    49   }
    50 }
    Circle.java
  • 相关阅读:
    回调那些事儿
    v-if和v-show小对比
    导出下载功能
    vue和react
    Redis 实现抢票
    MySQL 各种连接,
    MySQL的分组,降序 实现
    MySQL 窄表转宽表
    EX: 判断密码, 判断字符必须包含大写,小写,数字,特殊字符 ,并且键盘不能连续
    hive 基础
  • 原文地址:https://www.cnblogs.com/wanjiang/p/5589949.html
Copyright © 2011-2022 走看看