Rectangle.java
package graph;
class Rectangle {
public Rectangle(int length, int width) {
this.length = length;
this.width = width;
}
int length;
int width;
public void similarity(){
System.out.print("长度为:"+length+" ");
System.out.print("宽度为:"+width+" ");
}
public void area(){
int area=length*width;
System.out.print("\n面积为:"+area+" ");
}
public void perimeter(){
int perimeter=2*length+2*width;
System.out.print("周长为:"+perimeter+" ");
}
}
Cuboid.java
package graph;
class Cuboid extends Rectangle{
int height;
public Cuboid(int length, int width,int height){
super(length,width);
this.height = height;
}
public void area(){
System.out.print("高度为:"+height+" ");
int area=2*length*width+height*width*4;
System.out.print("\n面积为:"+area+" ");
}
public void perimeter(){
int perimeter=4*length+4*width+4*height;
System.out.print("周长为:"+perimeter+" ");
}
public void volume(){
int volume=length*width*height;
System.out.println("体积为:"+volume);
}
}
TestGraph.java
package graph;
public class TestGraph {
public static void main(String[] args){
Rectangle r1=new Rectangle(20,10);
r1.similarity(); //继承Rectangle类的共性方法
r1.area(); //新方法1
r1.perimeter(); //新方法2
System.out.println("\n------------------------------");
Cuboid c1=new Cuboid(3,4,5);
c1.similarity(); //继承Rectangle类的共性方法
c1.area(); //新方法1覆盖
c1.perimeter(); //新方法2覆盖
c1.volume(); //新方法3
}
}
Result