实现一组自定义形状类,其中包括形状类的基类,两个具体的形状类,即三角形类、矩形类
MyShape:
package com.itheima1;
public abstract class MyShape{
protected String name;
public abstract double getGirth();
public abstract double getArea();
public abstract String toString();
public String getName(){
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
Trangle类:
package com.itheima1;
public class Triangle extends MyShape {
private double sideA;
private double sideB;
private double sideC;
public static final String SIDEERR =
"三角形的边长不能够小于0!";
public static final String SHAPEERR =
"三角形的两边之和必须大于第三边!";
public Triangle() {
init();
}
public Triangle(double sideA, double sideB, double sideC) {
if(isTrianglelegal(sideA,sideB,sideC)){
this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
}
else {
init();
}
}
private void init(){
sideA = 3;
sideB = 4;
sideC = 5;
}
private boolean isTrianglelegal(double a,double b,double c){
if((a <= 0) || (b <= 0) || (c <= 0)){
System.out.println(SIDEERR);
return false;
}
else if((a + b) <= c && (a + c) <= b || (b + c) <= a){
System.out.println(SHAPEERR);
return false;
}
return true;
}
@Override
public double getGirth() {
return this.sideA + this.sideB +this.sideC;
}
@Override
public double getArea() {
double s = (this.sideA + this.sideB +this.sideC) / 2;
return Math.sqrt(s * (s - this.sideA) * (s - this.sideB) * (s - this.sideC));
}
@Override
public String toString() {
return "三角形的名字是:"+name + ",它的三条边的边长分别是:"
+ this.sideA + ", " + this.sideB + ", " + this.sideC;
}
public double getSideA() {
return sideA;
}
public void setSideA(double sideA) {
this.sideA = sideA;
}
public double getSideB() {
return sideB;
}
public void setSideB(double sideB) {
this.sideB = sideB;
}
public double getSideC() {
return sideC;
}
public void setSideC(double sideC) {
this.sideC = sideC;
}
}
Rectangle类:
package com.itheima1;
public class Rectangle extends MyShape {
private double length;
private double width;
public static final String SIDEERR = "矩形的长和宽必须大于0!";
public Rectangle() {
init();
}
public Rectangle(double length, double width) {
if((length <= 0) || (width <= 0)){
System.out.println(SIDEERR);
init();
}
else {
this.length = length;
this.width = width;
}
}
private void init(){
this.length = 5;
this.width = 4;
}
@Override
public double getGirth() {
return (length + width) * 2;
}
@Override
public double getArea() {
return length * width;
}
@Override
public String toString() {
return "矩形的名字是:" + name
+",长为:"+length + ",宽为" + width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
}
Main:
package com.itheima1;
public class Main {
public static void main(String[] args) {
Triangle triangle = new Triangle();
triangle.setName("myTriangle");
System.out.println(triangle.toString());
System.out.println("三角形的周长为:" + triangle.getGirth());
System.out.println("三角形的面积为:"+triangle.getArea());
Rectangle rectangle = new Rectangle();
rectangle.setName("myRectangle");
System.out.println(rectangle.toString());
System.out.println("矩形的周长为:" + rectangle.getGirth());
System.out.println("矩形的面积为:"+rectangle.getArea());
}
}