抽象类
package Demo2;
public abstract class Shape {
double area;
double per;
String color;
public Shape(){
super();
}
public Shape(String color){
super();
this.color=color;
}
public abstract double getArea();
public abstract double getper();
public abstract void showAll();
public String getColor() {
// TODO Auto-generated method stub
return color;
}
}
矩形类
package Demo2;
public class Rectangle extends Shape {
double width;
double height;
public Rectangle(){
super();
}
public Rectangle(String color,double width,double height){
super(color);
this.height=height;
this.width=width;
}
@Override
public double getArea() {
// TODO Auto-generated method stub
return this.height*this.width;
}
@Override
public double getper() {
// TODO Auto-generated method stub
return 2*(this.height+this.width);
}
@Override
public void showAll() {
// TODO Auto-generated method stub
System.out.println("長方形");
System.out.println("長"+this.width+"寬"+this.height+"顔色"+super.getColor()+"面積"+this.getArea()
+"周長"+this.getper());
}
}
圆形类
package Demo2;
public class Circle extends Shape {
double radius;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public Circle(){
super();
}
@Override
public double getArea() {
// TODO Auto-generated method stub
return area=radius*radius*3.14;
}
@Override
public
double getper() {
// TODO Auto-generated method stub
return radius*2*3.14;
}
@Override
public void showAll() {
// TODO Auto-generated method stub
System.out.println("園形");
System.out.println("半徑"+this.radius+"顔色"+super.getColor()+"面積"+this.getArea()
+"周長"+this.getper());
}
}
测试类
package Demo2;
public class Circle extends Shape {
double radius;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public Circle(){
super();
}
@Override
public double getArea() {
// TODO Auto-generated method stub
return area=radius*radius*3.14;
}
@Override
public
double getper() {
// TODO Auto-generated method stub
return radius*2*3.14;
}
@Override
public void showAll() {
// TODO Auto-generated method stub
System.out.println("園形");
System.out.println("半徑"+this.radius+"顔色"+super.getColor()+"面積"+this.getArea()
+"周長"+this.getper());
}
}