定义类Shape表⽰⼀般⼆维图形...
定义类Shape表⽰⼀般⼆维图形。Shape具有抽象⽅法area和perimeter,分别计算形状的⾯积和周长。试定义⼀些⼆维形状类(如矩形、三⾓形、圆形等),这些类均为Shape类的⼦类。
代码:
package ListSixTen;
abstract class Shape {
public abstract double area();
public abstract double perimeter();
}
package ListSixTen;
class Rectangle extends Shape{ //矩形
private double wide; //宽
private double longs; //长
public Rectangle(){
}
public Rectangle(double wide,double longs){
super();
this.wide=wide;
this.longs=longs;
}
alexandrepublic void tWide(double wide){
this.wide=wide;
dec}
public double getWide(){
return wide;
}护士英文
public void tLongs(double longs){
this.longs=longs;柚子的英文
}
public double longs(){
return longs;
}
public double area() {
return this.longs*this.wide;
}
public double perimeter() {
return (this.longs+this.wide)*2;
}
}
package ListSixTen;
class Triangle extends Shape { //三⾓形
private double edgea;
private double edgeb;
private double edgec;
public Triangle(){}
may daypublic Triangle(double edgea,double edgeb,double edgec){
super();rugo
this.edgea=edgea;
this.edgeb=edgeb;
this.edgec=edgec;
}
public double area(){
return this.edgea*this.edgeb/2;
}
public double perimeter(){
return this.edgea+this.edgeb+this.edgec;
}
public void tEdgea(double edgea){
this.edgea=edgea;
}
public double getEdgea(){
bdy
return edgea;
}
public void tEdgeb(double edgeb){
this.edgeb=edgeb;
}
public double getEdgeb(){
北京化妆学校return edgeb;
}
北卡罗来纳州立大学public void tEdgec(double edgec){
this.edgec=edgec;
}
public double getEdgec(){
return edgec;
}
}
package ListSixTen;
class Round extends Shape{
private double radius;
public Round(){}
public Round(double radius){
super();
this.radius=radius;
}
public double area(){
return this.radius*this.radius*Math.PI;
}
public double perimeter(){
return this.radius*2*Math.PI;
}artistic
public void tRadius(double radius){
this.radius=radius;
}
public double getRadius(){
return radius;
}
}
package ListSixTen;
public class TestDemo10 {
public static void main(String[] args) {
Shape rectangle = new Rectangle(10.5, 20.6);
Shape triangle = new Triangle(10.1, 20.2, 30.3);
Shape round = new Round(30.3) ;
System.out.println("矩形⾯积:" + rectangle.area() + ",矩形周长:" + rectangle.perimeter()); System.out.println("三⾓形⾯积:" + triangle.area() + ",三⾓形周长:" + triangle.perimeter()); System.out.println("圆形⾯积:" + round.area() + ",圆形周长:" + round.perimeter());
}
}
结果:
矩形⾯积:216.3,矩形周长:62.2
三⾓形⾯积:102.00999999999999,三⾓形周长:60.599999999999994
圆形⾯积:2884.2647993342534,圆形周长:190.38051480754146