• 14.3


     1 import java.awt.*;
     2 import java.util.Scanner;
     3 
     4 import javax.swing.*;
     5 
     6 public class Test{
     7   public static void main(String[] args){
     8       GeometricObject g1 = new Square();
     9       GeometricObject g2 = new Square("yellow", 1,true);
    10       
    11       if(((Square)g1).filled)      
    12       {System.out.println("g1:");
    13           ((Square)g1).howToColor();}
    14       
    15       if(((Square)g2).filled)
    16       {System.out.println("g2:");
    17           ((Square)g2).howToColor();}
    18   }  
    19 }
    Test.java
    public interface Colorable {
        abstract void howToColor();
    }
    Colorable.java
     1 // GeometricObject.java: The abstract GeometricObject class
     2 public class GeometricObject implements Comparable{
     3   protected String color = "white";
     4   protected boolean filled;
     5   protected int a;
     6 
     7   /**Default construct*/
     8   protected GeometricObject() {
     9       filled = false;
    10   }
    11 
    12   /**Construct a geometric object*/
    13   protected GeometricObject(String color, int a,boolean filled) {
    14     this.color = color;
    15     this.a = a;
    16     this.filled = filled;
    17   }
    18 
    19   public static Comparable max(Comparable c1,Comparable c2){
    20       if(c1.compareTo(c2)>0)
    21       return c1;
    22       else return c2;
    23   }
    24   public int compareTo(Object o){
    25       if(this.getArea() > ((GeometricObject)o).getArea())
    26           return 1;
    27       else if (this.getArea() == ((GeometricObject)o).getArea())
    28           return 0;
    29       else return -1;
    30   }
    31   /**Getter method for color*/
    32   public String getColor() {
    33     return color;
    34   }
    35 
    36   /**Setter method for color*/
    37   public void setColor(String color) {
    38     this.color = color;
    39   }
    40 
    41   /**Getter method for filled. Since filled is boolean,
    42      so, the get method name is isFilled*/
    43   public boolean isFilled() {
    44     return filled;
    45   }
    46 
    47   /**Setter method for filled*/
    48   public void setFilled(boolean filled) {
    49     this.filled = filled;
    50   }
    51 
    52   /**Abstract method findArea*/
    53   public  int getArea(){
    54       return a*a;
    55   }
    56 
    57   /**Abstract method getPerimeter*/
    58   public  double getPerimeter(){
    59       return 1;
    60   }
    61 }
    GeometricObject.java
     1 public class Square extends GeometricObject implements Colorable{
     2 
     3     public Square() {
     4         // TODO Auto-generated constructor stub
     5     }
     6 
     7     protected Square(String color, int a,boolean filled) {
     8         this.color = color;
     9         this.a = a;
    10         this.filled = filled;
    11       }
    12     @Override
    13     public void howToColor() {
    14         // TODO Auto-generated method stub
    15         System.out.println("Color all four sides");
    16     }
    17 
    18 }
    Square.java
  • 相关阅读:
    .net Framework 4.5 MVC4 + RabbitMQ
    阿里云飞天系统的技术架构(转)
    ORA12899错误解决记录
    网络通讯函数测试记录
    .应该用CreateThread还是_beginthreadex(), 为什么?( 转载)
    发挥v$SQL视图的作用(oracle)
    ClickHouse笔记
    MySQL字段是JsonArray格式怎么查询数据
    Base64编码保存为图片,java工具类
    java两个线程交替打印数字
  • 原文地址:https://www.cnblogs.com/wanjiang/p/5590068.html
Copyright © 2020-2023  润新知