定义类Shape表示一般二维图形...

更新时间:2023-07-08 10:49:08 阅读: 评论:0

定义类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

本文发布于:2023-07-08 10:49:08,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/1085330.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:周长   矩形   抽象   学校   大学   图形
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图