日期:2014-05-20  浏览次数:20725 次

关于类的设计
想要设计一个图形的抽象类,有面积,周长的抽象方法,有颜色的属性;设计一个矩形类由图形类继承,输入长和宽,颜色,可得到周长与面积并输出颜色值;设计一个圆形类由图形类继承,输入半径以及颜色,可得到周长与面积并输出颜色值。不知怎么下手,求高手帮忙。


------解决方案--------------------
package extend;

import java.awt.Color;
import java.lang.Math;

abstract class MyGraphics{
protected Color color;
public abstract double getArea();
public abstract double getPerimeter();
}

class MyRectangle extends MyGraphics{

private double length;
private double width;

public void setColor(Color color)
{
this.color = color;
}
public Color getColor()
{
return this.color;
}

public void setLength(double length)
{
this.length = length;
}
public void setWidth(double width)
{
this.width = width;
}
public double getArea()
{
return length*width;
}
public double getPerimeter()
{
return 2*(length+width);
}

}

class MyCircle extends MyGraphics{

private double radius;

public void setColor(Color color)
{
this.color = color;
}
public Color getColor()
{
return this.color;
}

public void setRadius(double radius)
{
this.radius = radius;
}
public double getArea()
{
return java.lang.Math.PI*radius*radius;
}
public double getPerimeter()
{
return 2*java.lang.Math.PI*radius;
}
}

public class GraphicsTest {

/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args) {
// TODO: Add your code here
}
}

------解决方案--------------------
抽象Shape类:
abstract class Shap {
public float getArea();
public float getCircumference();
protected float color;
}

矩形:
class Rectangular extends Shape {
public float width;
public float height;

public Rectangular(float width, float height, float color) {
this.width = width;
this.height = height;
this.color = color;
}
public float getArea() {
return this.widht * this.height;
}
public float getCircumference() {
return 2 * (this.widht + this.height);
}
}
以后的,参照这个看一下,自己也做做。