【问题标题】:Creating a Circle Class in Java with Point center?用点中心在Java中创建一个Circle类?
【发布时间】:2012-04-24 22:47:20
【问题描述】:

写一个封装圆概念的类,假设圆有以下属性:一个Point代表圆心,圆的半径,一个整数。包括构造函数、访问器和修改器,以及 toString 和 equals 方法。还包括返回圆的周长(2*PI*radius)和面积(PI*radius^2)的方法。

import java.awt.*;


public class Circle {

    private int radius;

    public Circle() {

        radius = 1;
    }

    public Circle(int x, int y, int r) {
        super(x, y, c);
        radius = r;
    }

    public int getRadius() {
        return radius;
    }

    public double getArea() {
        return Math.PI * radius * radius;
    }

    public double getPerimeter() {
        return 2 * Math.PI * radius;
    }
}

我可以做到这一点,我只是对向我的类添加点构造函数、访问器和修改器有点困惑。

会是这个样子吗?

  protected int x, y;

  public point() {

      setPoint(0,0);

  }

  public point(int coordx, int coordy) {
      setPoint(coordx,coordy);
  }

  public void setPoint(int coordx, int coordy) {
      x = coordx;
      y = coordy;
  }
  public int getX() {
      return x;
  }

  public int getY() {
      return y;
  }

  public String toPrint() {
      return "[" + x + "," + y + "]";
  }

  }

是否可以将两者结合在 1 个类中?我试了一下,里面的每一行都有一个错误,说 Circle 没有返回类型。任何洞察力都将是一种回报。再次感谢大家。

【问题讨论】:

  • 一个圆有以下属性:一个Point表示圆的中心,圆的半径,一个整数。那么 Circle 类中的 Point 声明在哪里?
  • 如果我没记错的话,Point 可以看作是半径固定为零的圆的超类。因此,我会编写一个 Point 类,然后派生一个 Circle 类。
  • @BigMike 如果 Point 是 Circle 的超类,它有半径吗?恕我直言,没有。我认为两者之间的共同点太少,无法创建像“点是半径为零的圆”这样的关系......
  • 什么是超类/超构造函数?
  • @Betlista 对不起我的英语不好。当然一个点没有半径,圆通过添加它来扩展它。对我来说,用代码比用英语更容易描述。

标签: java


【解决方案1】:

您说的是在一个类中拥有多个对象的构造函数。那是做不到的。 Java 认为public point() 是一个没有返回类型的方法,因此在语法上是不正确的。

你不需要为了点而上课。 Java 已经提供了java.awt.Point。只需将 Point 的类级别字段添加到您的 Circle 类中即可。

Circle 看起来像这样:

public class Circle {

    private int radius;
    private Point point;

    public Circle() {
        point = new Point(0, 0);
        radius = 1;
    }

    public Circle(int x, int y, int r) {
        point = new Point(x, y);
        radius = r;
    }

    public int getRadius() {
        return radius;
    }

    public double getArea() {
        return Math.PI * radius * radius;
    }

    public double getPerimeter() {
        return 2 * Math.PI * radius;
    }
}

【讨论】:

  • 啊,明白了。所以一节课。那么如何在我的 Circle 类中设置 (0,0) 点?除非我使用 GUI 并画一个圆圈,否则我从来没有遇到过这样的事情。
  • 实际上,更好的方法是从默认构造函数Circle() 调用this(0, 0, 1);。这将更符合 OOP,但这是一个非常简单的示例,我的目的是向您展示如何设置字段。
【解决方案2】:

这里有一些代码,圆是一个点的延伸,增加了一个半径

public class Point {
   public Point(int x, int y) {
      // .. set x and Y coord
   }

   // Getters and Setters
   public int getX() {
      return x;
   }

  public int getY() {
      return y;
  }

  public String toPrint() {
      return "[" + x + "," + y + "]";
  }
  // Your other Point methods...


  private int x = 0;
  private int y = 0;
}


public class Circle extends Point {
  int rad;

  public Circle (int x, int y, int radius) {
    super(x, y);
    rad = radius;
  }
  // Your other Circle methods
}

正如承诺的那样:另一种没有扩展的方式可能是:

class Point {
  int x;
  int y;

  public Point (int x, int y) {
     this.x = x;
     this.y = y;
  }
  // Getter/Setters and other methods
}


public class Circle {

  Point centre = null;
  int radius = 0;

  public Circle(int x, int y, int rad) {
      centre = new Point(x, y);
      radius = rad;
  }
   // Your other Circle methods
}

【讨论】:

  • 啊,这更有意义。谢谢你。从Circle调用Point,然后继续前进。我感激不尽。
  • @user1316703 请注意,同样可以使用带有简单 Point 数据成员的 Circlce 类来实现。两种方式可能都很好。也会以其他方式添加。
  • 否 - 圆不是一个点 - 因此它不应该扩展点类。改用合成
  • @scibuff 这两种方式对我来说同样有效,扩展一个类也不是为了良好的 oop 设计,而只是为了避免代码重写。顺便说一句,我的几何学研究的时间很远,但我几乎可以肯定点和圆之间存在某种关系。
  • 你不需要重写任何东西,只需要在 Circle 类中使用 Point 类(见下面我的例子)。 p.s.点和圆之间肯定没有关系(除了圆是一组与给定中心等距的“点”)
【解决方案3】:

我会这样做

import java.awt.Point;

public class Circle {

    private Point center;
    private int radius;

    public Circle(){
        this( new Point( 0, 0 ) );
    }

    public Circle( int x, int y, int radius ){
        this( new Point( x, y ), radius );
    }

    public Circle( Point center ){
        this( center, 1 );
    }

    public Circle( Point center, int radius ){   
        this.setCenter( center );
        this.radius = radius;
    }

    public int getRadius(){ return this.radius; }
    public Point getCenter(){ return this.center; }

    public double getPerimeter(){ return 2 * Math.PI * this.radius; }
    public double getArea(){ return Math.PI * this.radius * this.radius; }

    public void setCenter( int x, int y ){
        this.setCenter( new Point( x, y ) );
    } 

    public void setCenter( Point center ){
        this.center = center;
    }

    public boolean equals( Object o ){ 

        if ( o == this ){ return true; }
        if ( o == null || o.getClass() != this.getClass() ){ return false; }

        Circle c = (Circle) o;
        return ( o.radius == this.radius && o.center.equals( this.center ) );
    }

    public string toString(){
        return "Circle[" + this.center.toString() + ", " + this.radius + "]";
    }
}

【讨论】:

  • 头脑=吹。这对我来说有点难以解释哈哈。不过,我想我明白了您要向我展示的内容。
  • 没什么大不了的......上面的代码使用方法重载 - 将根据提供的参数调用正确的构造函数 - 优点是它最大限度地减少了代码重复,即相关的初始化代码在其中一个构造函数中,其他人调用它。
【解决方案4】:

你在构造函数中调用超级构造函数:

super(x, y, c); 

【讨论】:

    猜你喜欢
    • 2022-11-21
    • 2015-10-11
    • 2014-08-01
    • 2017-04-15
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多