abstract class MyShape{
public abstract void getArea();
public abstract void getLength();
}
//矩形属于图形中一种
class Rect extends MyShape{
int height ;
int width;
public Rect(int height,int width){
this.height = height;
this.width = width;
}
public void getArea(){
System.out.println("矩形的面积:"+width*height);
}
public void getLength(){
System.out.println("矩形的周长:"+2*(height+width));
}
}
//圆形也是属于图形中一种
class Circle extends MyShape{
public static final double PI = 3.14;//常量
double r ;
public Circle(double r){
this.r = r;
}
public void getArea(){
System.out.println("圆形的面积:"+PI*r*r);
}
public void getLength(){
System.out.println("圆形的周长:"+2*PI*r);
}
}
class Demo59
{
public static void main(String[] args)
{
Rect r = new Rect(3,4);
r.getLength();
r.getArea();
Circle c = new Circle(3.0);
c.getLength();
c.getArea();
}
}
JAVA小练习59——abstract练习2 需求: 描述图形、矩形、 圆形三个类,首先要把三者之间的关系描述清楚, 所有的图形都有计算面积以及周长的方法,但是每种图形的计算面积与周长方式并不一致
最新推荐文章于 2021-12-07 09:09:43 发布