【JAVA】+ 封装 + 父类~子类~继承

来源:互联网 发布:java策略模式实例 编辑:程序博客网 时间:2024/06/14 19:40

JAVA语言程序设计第十版~~~~

父类 :

package ThOne;public class One {     private String color = "white";     private boolean filled;     private java.util.Date date;     public One(){         date = new java.util.Date();     }     public One(String color ,boolean filled){         date = new java.util.Date();         this.color = color;         this.filled = filled;     }     public String getcolor(){         return color;     }     public void setcolor(String color){         this.color = color;     }     public boolean isFilled(){         return filled;     }     public void setFilled(boolean filled){         this.filled = filled;     }     public java.util.Date getDate(){         return date;     }     public String toString(){         return "cteated on " + date + "\ncolor : " + color + " and filled: " + filled;     }}

子类 A :

package ThOne;public class Two extends One{    public double radius;    public  Two(){        }    public Two(double radius){        this.radius = radius;    }    public Two(double radius , String color , boolean filled){        this.radius = radius;        setcolor(color);        setFilled(filled);    }    public double getRadius(){        return radius;    }    public void setRadius(double radius){        this.radius = radius;    }    public double getArea(){        return radius * radius * Math.PI;    }    public double getDia(){        return 2 * radius;    }    public double getPer(){        return 2 * radius * Math.PI;    }    public void printT(){        System.out.println("The cirle is created " + getDate() + "and the radius is " + radius);    }}

子类 B :

package ThOne;public class Three extends One{    private double width;    private double height;    public Three(){    }    public Three(double width , double height){        this.width = width;        this.height = height;    }    public Three(double width,double height, String color , boolean filled){        this.height = height;        this.width = width;        setcolor(color);        setFilled(filled);    }    public double getWidth(){        return width;    }    public double getHeight(){        return height;    }    public void setWidth(double width){        this.width = width;    }    public void setHeight(double height){        this.height = height;    }    public double getArea(){        return height * width;    }    public double getPer(){        return 2 * (width + height);    }}

Test :

package ThOne;public class TestOne{    public static void main(String[] args){        Two T = new Two(1);        System.out.println("A citcle " + T.toString());        System.out.println("The color is " + T.getcolor());        System.out.println("The radius is " + T.getRadius());        System.out.println("The area is " + T.getArea());        System.out.println("The diameter is " + T.getDate());        Three Th = new Three(2,4);        System.out.println("\nA rectangle " + Th.toString());        System.out.println("The area is " + Th.getArea());        System.out.println("The perimeter is " + Th.getPer());    }}
1 0
原创粉丝点击