/*
需求: 描述图形、矩形、 圆形三个类,首先要把三者之间的关系描述清楚,
所有的图形都有计算面积以及周长的方法,但是每种图形的计算面积与周长方式并不一致。
*/
//图形类
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 Demo5
{
public static void main(String[] args)
{
///System.out.println("Hello World!");
Rect r = new Rect(3,4);
r.getLength();
r.getArea();
Circle c = new Circle(3.0);
c.getLength();
c.getArea();
}
}
注意事项:
/*
abstract不能与以下关键字配合使用:
1. abstract 不能与private配合使用。
2. abstract 不能与final关键字配合使用。
3. abstract 不能与static关键字配合使用。
*/
//图形类
abstract class MyShape{
public abstract void getArea();
}
class Demo6
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}