📄 shape.java
字号:
package array;
public class Shape {
/**
* @param 1、写一个父类Shape他有两个属性x、y,三个子类Rectangle,
* Circle, triangle,Rectangle有两个属性width和length. Circle有一个
* 属性radius,shape有一个方法draw(),测试一下多态的例子。
*
*/
private double x;private double y;protected double area;
/*public Shape(double x,double y)
{
this.x=x;
this.y=y;
}
public Shape(){}*/
public void draw(){
//System.out.println("please draw the shape!!output the shape'area:"+area);
}
public static void main(String[] args) {
Shape[] sh={
new Rectangle(2.0,3),
new Triangle(2.5,4.0),
new Circle(2.05)
};
for(int i=0;i<sh.length;i++){
sh[i].draw();
}
Rectangle tri=new Rectangle();//逐级调默认的三个构造方法
tri.draw();
}
}
class Rectangle extends Shape{
private double width;private double length;private double area;
Rectangle(){
this(3.2);
}
Rectangle(double width){
this(width,2.3);
}
Rectangle(double width,double length){
this.width=width;
this.length=length;
}
public void draw(){
area=width*length;
System.out.println("this Rectangle'area is:"+area);
}
}
class Triangle extends Shape{
private double width;
private double high;//private double area;
Triangle(double width,double high){
this.width=width;
this.high=high;
}
public void draw(){
area=width*high/2;
System.out.println("this Triangle'area is:"+area);
}
}
class Circle extends Shape{
private double radius;
public static final double PI=3.1415926;
Circle(double radius){
this.radius=radius;
}
public void draw(){
area=PI *radius*radius;
System.out.println("this Circle'area is:"+area);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -