自定义形状类

时间:2022-10-20 03:59:23
 1 package com.jdk7.chapter2.shape;
 2 
 3 /**
 4  * 抽象类不能声明为实例对象
 5  * 抽象类中的方法也为抽象方法,不予实现,必须由子类实现
 6  * @author celineluo
 7  *
 8  */
 9 public abstract class MyShape {
10     protected String name;
11 
12     public String getName() {
13         return name;
14     }
15 
16     public void setName(String name) {
17         this.name = name;
18     }
19     
20     public abstract double area();
21     public abstract double girth();
22 
23     @Override
24     public String toString() {
25         return "MyShape [name=" + name + "]";
26     }
27     
28 }

子类,一个子类只能继承一个父类,但是可以同时实现多个接口

  1 package com.jdk7.chapter2.shape;
  2 
  3 public class Triangle extends MyShape {
  4     private double sideA;
  5     private double sideB;
  6     private double sideC;
  7     private String lengthErr = "三角形边长不能小于0";
  8     private String sumSize = "两边之和不能小于第三边";
  9     
 10     //默认构造函数
 11     public Triangle(){
 12         init();
 13     }
 14     
 15     //自定义构造函数
 16     public Triangle(double a,double b,double c){
 17         if(isTriangle(a,b,c)){
 18             this.sideA = a;
 19             this.sideB = b;
 20             this.sideC = c;
 21         }else{
 22             init();
 23         }
 24     }
 25     
 26     //判断是否符合三角形条件
 27     public boolean isTriangle(double a,double b,double c){
 28         if(a<=0 || b<=0 || c<=0){
 29             System.err.println("Error: "+lengthErr);
 30             return false;
 31         }else if((a+b)<=c ||(a+c)<=b ||(c+b)<=a){
 32             System.err.println("Error: "+sumSize);
 33             return false;
 34         }else{
 35             return true;
 36         }
 37         
 38     }
 39     public void init(){
 40         this.sideA = 3;
 41         this.sideB = 4;
 42         this.sideC = 5;
 43     }
 44 
 45     @Override
 46     public double area() {
 47         double result;
 48         double a = (this.sideA + this.sideB + this.sideC)/2;
 49         result = a*(a-this.sideA)*(a-this.sideB)*(a-this.sideC);
 50         return result;
 51     }
 52 
 53     @Override
 54     public double girth() {
 55         double result;
 56         result  = this.sideA + this.sideB + this.sideC;
 57         return result;
 58     }
 59 
 60     public double getSideA() {
 61         return sideA;
 62     }
 63     
 64     //在使用对象类型时,如果没有声明一个实例对象则无法调用对象类型的变量和方法,
 65     //声明实例对象时,会进行初始化
 66     //调用对象类型变量的Setter方法,则会将设置值替换初始值进行校验
 67     public void setSideA(double sideA) {
 68         if(isTriangle(sideA, this.sideB, this.sideC)){
 69             this.sideA = sideA;
 70         }
 71     }
 72 
 73     public double getSideB() {
 74         return sideB;
 75     }
 76 
 77     public void setSideB(double sideB) {
 78         if(isTriangle(this.sideA,sideB,this.sideC)){
 79             this.sideB = sideB;
 80         }
 81     }
 82 
 83     public double getSideC() {
 84         return sideC;
 85     }
 86 
 87     public void setSideC(double sideC) {
 88         if(isTriangle(this.sideA,this.sideB,sideC)){
 89             this.sideC = sideC;
 90         }
 91     }
 92     
 93     @Override
 94     public String toString() {
 95         return "三角形名称是: " + name +", 三角形第一条边是: " + sideA + ", 三角形第二条边是: " + sideB + ", 三角形第三条边是: "
 96                 + sideC + ", 三角形面积是: "+ area() + ", 三角形周长是: " + girth();
 97     }
 98 
 99     public static void main(String[] args) {
100 //        Triangle t = new Triangle();
101 //        System.out.println(t.girth());
102 //        System.out.println(t.area());
103 //        t.setSideA(0);
104 //        t.setSideA(1);
105 //        t.setSideB(5);
106 //        t.setSideC(6);
107 //        System.out.println(t.girth());
108 //        System.out.println(t.area());
109 //        Triangle t1 = new Triangle(1,5,6);
110 //        Triangle t1 = new Triangle(0,5,6);
111         Triangle t1 = new Triangle(3.0,5.0,6.0);
112         t1.setName("三角形");
113         System.out.println(t1.girth());
114         System.out.println(t1.area());
115         System.out.println(t1.toString());
116         
117     }
118 
119 }
120 执行结果:
121 14.0
122 56.0
123 三角形名称是: 三角形, 三角形第一条边是: 3.0, 三角形第二条边是: 5.0, 三角形第三条边是: 6.0, 三角形面积是: 56.0, 三角形周长是: 14.0

 

  1 package com.jdk7.chapter2.shape;
  2 
  3 public class Rectangle extends MyShape {
  4     private double sideA;
  5     private double sideB;
  6     private String lengthErr = "边长不能小于0.";
  7     
  8     //默认构造函数
  9     public Rectangle(){
 10         init();
 11     }
 12     
 13     //自定义构造函数
 14     public Rectangle(double a,double b){
 15         if(isRectangle(a,b)){
 16             this.sideA = a;
 17             this.sideB = b;
 18         }
 19     }
 20     
 21     //初始化矩形
 22     public void init(){
 23         this.sideA = 4;
 24         this.sideB = 5;
 25     }
 26     
 27     //判断是否符合矩形条件
 28     public boolean isRectangle(double a,double b){
 29         if(a<=0 || b<=0){
 30             System.err.println(lengthErr);
 31             return false;
 32         }else{
 33             return true;
 34         }
 35     }
 36 
 37     @Override
 38     public double area() {
 39         double result;
 40         result = this.sideA * this.sideB;
 41         return result;
 42     }
 43 
 44     @Override
 45     public double girth() {
 46         double result;
 47         result = (this.sideA + this.sideB)*2;
 48         return result;
 49     }
 50     public double getSideA() {
 51         return sideA;
 52     }
 53     
 54     //在使用对象类型时,如果没有声明一个实例对象则无法调用对象类型的变量和方法,
 55     //声明实例对象时,会进行初始化
 56     //调用对象类型变量的Setter方法,则会将设置值替换初始值进行校验
 57     public void setSideA(double sideA) {
 58         if(isRectangle(sideA,this.sideB)){
 59             this.sideA = sideA;
 60         }
 61     }
 62     public double getSideB() {
 63         return sideB;
 64     }
 65     public void setSideB(double sideB) {
 66         if(isRectangle(this.sideA,sideB)){
 67             this.sideB = sideB;
 68         }
 69     }
 70     
 71     @Override
 72     public String toString() {
 73         return "矩形名称是: " + name + ", 矩形长是: " + sideA + ", 矩形宽是: " + sideB + ", 矩形面积是: "+ area() + ", 矩形周长是: " + girth();
 74     }
 75 
 76     public static void main(String[] args) {
 77         Rectangle r = new Rectangle();
 78         System.out.println(r.girth());
 79         System.out.println(r.area());
 80 //        r.setSideA(0);
 81         r.setSideA(3);
 82         System.out.println(r.girth());
 83         System.out.println(r.area());
 84         
 85         Rectangle r1 = new Rectangle(6,8);
 86         System.out.println(r1.girth());
 87         System.out.println(r1.area());
 88         r1.setName("矩形");
 89         r1.setSideA(5);
 90         System.out.println(r1.girth());
 91         System.out.println(r1.area());
 92         System.out.println(r1.toString());
 93     }
 94 
 95 }
 96 执行结果:
 97 18.0
 98 20.0
 99 16.0
100 15.0
101 28.0
102 48.0
103 26.0
104 40.0
105 矩形名称是: 矩形, 矩形长是: 5.0, 矩形宽是: 8.0, 矩形面积是: 40.0, 矩形周长是: 26.0