日期:2014-05-17  浏览次数:20636 次

小弟java题目求助!各位大神帮帮忙。。。
题目:编写Shape类、Rectangle类和Circle类。其中,Shape类是父类,其他两个类是子类。Shape类包含两个属性:x和y,以及一个方法draw();Rectangle类增加了两个属性:长度和宽度;Circle类增加了一个属性:半径。使用一个主方法来测试Shape中的数据和方法可以被子类集成。然后分别在两个子类中重写draw()方法并实现多态。

请帮帮忙!!!谢谢各位!!
------解决方案--------------------
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());
}
 }