追问大神---向上转型和向下转型的实现。
package com.syq.test;
public class Shape {
public void draw() {
System.out.println("绘制图形…………");
}
public double getPerimter() {
return 0;
}
public double getArea() {
return 0;
}
public void print() {
System.out.println("这是父类的print方法");
}
}
package com.syq.test;
public class Triangle extends Shape {
private double sid1;
private double sid2;
private double sid3;
public Triangle() {
}
public Triangle(double sid1, double sid2, double sid3) {
this.setSid1(sid1);
this.setSid2(sid2);
this.setSid3(sid3);
}
// 求三边中最长的边
public double sidMax() {
return (sid1 > sid2) ? sid1 : (sid2 > sid3) ? sid2 : sid1;
}
// 重写toString方法,用于描述对象
public String toString() {
return "三角形周长:" + this.getPerimter() + "\n三角形面积:" + this.getArea()
+ "\n最长的边长:"+this.sidMax();
}
public void draw() {
System.out.println("绘制三角形…………三边长:"+this.getSid1()+" "+this.getSid2()+" "+this.getSid3());
}
// 绘制三角形,返回是否符合三角形的定义
public void draw(Triangle triangle) {
if (this.sid1 + sid2 > sid3 & sid3 - sid1 < sid2) {
this.setSid1(sid1);
this.setSid2(sid2);
this.setSid3(sid3);
System.out.println("成功绘制三角形!");
}else {
System.out.println("不符合绘制三角形的条件!");
}
}
// 计算周长
public double getPerimter() {
return this.sid1 + this.sid2 + this.sid3;
}
// 计算三角形面积
public double getArea() {
double p = this.getPerimter() / 2;
double area = Math.sqrt(p * (p - sid1) * (p - sid2) * (p - sid3));
return p;
}
public double getSid1() {
return sid1;
}
public void setSid1(double sid1) {
this.sid1 = sid1;
}
public double getSid2() {
return sid2;
}
public void setSid2(double sid2) {
this.sid2 = sid2;
}
public double getSid3() {
return sid3;
}
public void setSid3(double sid3) {
this.sid3 = sid3;
}
}
package com.syq.test;
public class Circle extends Shape {
private double r;
public Circle() {
}
public Circle(double r) {
this.setR(r);
}
// 计算圆的直径
public double diameter() {
return 2 * r;
}
public void draw() {
System.out.println("绘制圆…………半径:"+this.getR());
}
// 绘制圆
public void draw(Circle circle) {
if (this.r > 0) {