【问题标题】:Encapsulation, Inheritance and Polymorphis封装、继承和多态
【发布时间】:2016-11-27 00:01:49
【问题描述】:

请举例说明这会是什么样子。我有点困惑,我必须做几组。如果有人能给我一个示例代码,我将不胜感激。

变量是半径 方法: 一个没有参数的 Circle 类构造函数。该方法首先通过语句 super(); 调用不带参数的超类构造函数;然后该方法为半径变量分配默认值 1.0。

带有 newRadius 参数的 Circle 类构造函数。该方法首先通过以下语句调用不带参数的超类构造函数:super();然后,如果 newRadius 大于零,则该方法将 newRadius 的值分配给 radius 变量。否则,它将半径指定为默认值 1.0。

具有 newX、newY 和 newRadius 参数的 Circle 类构造函数,按顺序排列。该方法首先通过语句调用带有两个参数的超类构造函数: super(newX, newY);然后,如果 newRadius 大于零,则该方法将 newRadius 的值分配给 radius 变量。否则,它将半径指定为默认值 1.0。

getRadius() – 没有参数的双返回类型方法。返回此 Circle 的半径。

setRadius() – 带有 newRadius 参数的 void 返回类型方法。如果 newRadius 大于零,则此方法将 newRadius 的值分配给 radius 变量。否则,它将 radius 分配默认值 1.0。

getArea() – 没有参数的双返回类型方法。此方法通过以下公式计算并返回圆的面积:半径 * 半径 * 3.14159。 toString() – 一个没有参数的字符串返回类型方法。 此方法返回一个字符串,其中包含此类的名称、Circle、x 和 y 坐标以及此 Circle 的半径。

【问题讨论】:

  • 至少尝试一些事情并带上你的代码,然后如果有错误就发布。
  • 我不是在寻求答案,只是一个示例。
  • 到目前为止,我已经有了一个名为 shape 的类,这就是包 phase1 的样子; //shape 调用子 tri,cir,rec public abstract class Shape { //变量 private int x;私人int y;公共形状 () { x = 0; y = 0; } 公共形状 (int newx, int newy) { x = newx; y = 新的; } //获取 x public int getx () { return x; } //获取y public int gety () { return y; } }

标签: inheritance polymorphism encapsulation


【解决方案1】:
public Class Circle{
    double radius;
    public Circle(){
        super();
        radius = 1.0;
    }
    public Circle(double newRadius){
        super();
        if(newRadius>0)
            radius = newRadius;
        else
            radius = 1.0;
    }
    public Circle(int newX, int newY, double newRadius){
        super(newX,newY);
        if(newRadius>0)
            radius = newRadius;
        else
            radius = 1.0;
    }
    public double getRadius(){
        return radius;
    }
    public void setRadius(double newRadius){
        if(newRadius>0)
            radius = newRadius;
        else
            radius = 1.0;
    }
}

这是您自己的问题实现功能的结构。

【讨论】:

    猜你喜欢
    • 2016-08-01
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-19
    • 1970-01-01
    相关资源
    最近更新 更多