日期:2014-05-17 浏览次数:20691 次
class Shape {
private int x = 2;
private int y = 3;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
void draw(){
System.out.println("画图形中…… \n"+"x是"+x+",y是"+y);
}
}
class Rectangle extends Shape{
private int length = 5;
private int width = 2;
public void draw(){
System.out.println("我画正方形,长是:"+length+"宽是"+width);
}
}
class Circle extends Shape{
private int radius = 3;
public void draw(){
System.out.println("我画圆,半径是:"+radius);
}
}
public class Test{
public static void main(String[] args) {
//父类声明指向子类对象体现多态
Shape r = new Rectangle();
Shape c = new Circle();
r.draw();
c.draw();
//继承
System.out.println("正方形继承来的x是"+r.getX()+",y是"+r.getY());
System.out.println("圆继承来的x是"+c.getX()+",y是"+c.getY());
}
}