zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Pascal Analyzer 4 代码分析使用简要说明
    界面动态配置:持久化反持久化
    (Sql Server)数据的拆分和合并
    javascript中的promise和deferred:实践(二)
    对面向接口编程、按分层建项目的反思和新的分层结构思路
    revel框架教程之权限控制
    等待与通知机制
    实现代码编辑器
    自定义html标签
    javascript生成自定义的arcgis simpletoolbar
  • 原文地址:https://www.cnblogs.com/wanjiang/p/5590068.html
Copyright © 2011-2022 走看看