重载的构造函数有两个参数,一个没有

时间:2015-02-27 16:47:49

标签: java parameters constructor

设计然后创建一个具有重载构造函数的Rectangle类。第一个构造函数不需要 参数。第二个参数有两个参数,一个用于长度,另一个用于宽度。成员变量存储 矩形的长度和宽度,以及成员方法分配和检索长度和宽度 返回矩形的面积和周长。通过编写适当的客户端代码来测试该类。

这是给我的任务,我不太明白如何开始这样的事情,它只是五个中的第一部分,我会感激一些帮助。

3 个答案:

答案 0 :(得分:1)

//Rectangle class
class Rectangle{
  private int length;
  private int width;

  Rectangle(){
    this.length=1; // assuming default length=1
    this.width=1; // assuming default width=1
  }

  Rectangle(int length, int width){
    this.length=length; 
    this.width=width; 
  }

int area(){
   return length*width;
}
int perimeter(){
  return 2*(length+width);
}
}


// test class

public class TestRectangle{
    public static void main(String args[]){
        Rectangle r1= new Rectangle();
        System.out.println("Area of r1: "+ r1.area());
        Rectangle r2= new Rectangle(2,3);
        System.out.println("Perimetr of r2: "+ r2.perimeter());
    }
}

答案 1 :(得分:0)

// http://www.cplusplus.com/doc/tutorial/classes/
//       Classes (I)
//         Overloading constructors

#include <cstdlib>  // contains EXIT_SUCCESS, EXIT_FAILURE
#include <iostream> // cout

// define class
class Rectangle
{
  int width, height;

  public:
    Rectangle ( );           // default constructor
    Rectangle ( int, int );  // overload constructor
    int area ( void ) { return ( width * height ); }
};

// default constructor
Rectangle::Rectangle ( )
{
  width  = 2;
  height = 3;
}

// overloaded constructor
Rectangle::Rectangle ( int a, int b )
{
  width  = a;
  height = b;
}

int main ( )
{
  // Two objects of class Rectangle are constructed: rectd and recto.
  // The object rectd is constructed using the default constructor
  // The object recto is constructed with two arguments (overloaded constructor)
  Rectangle recto ( 4, 5 );
  Rectangle rectd;  // empty parentheses cannot be used to call the default constructor

    std::cout << "rectangle area, default constructor:    " << rectd.area() << std::endl;
    std::cout << "expected answer:                        6 = 2 x 3\n" << std::endl;

    std::cout << "rectangle area, overloaded constructor: " << recto.area() << std::endl;
    std::cout << "expected answer:                        20 = 4 x 5"  << std::endl;

  return EXIT_SUCCESS;
}

代码用g ++编译;不需要C ++ 11或更高版本。

答案 2 :(得分:0)

为了好玩,我决定在C#

中这样做
    static void Main(string[] args)
    {

        Rectangle rect = new Rectangle();//length 2, width 1
        Rectangle yourRect = new Rectangle(12.34, 25.022);
        Console.WriteLine(rect.area() + " area of puny rect\n" + yourRect.area() + " area of bigger and better rect");
        Console.WriteLine(rect.perimeter() + " perimeter of puny rect\n" + yourRect.perimeter() + " perimeter of bigger and better!");
        //change things
        rect.length = 23.33;
        rect.width = 122222.333;
        Console.WriteLine(rect.area() + " WHOA look at that!\n" + rect.perimeter() + " WHHHAAATTT that is much bigger!");
        Console.ReadKey();//this is here to look at output without it disapearing.
    }

}
class Rectangle
{
    //simple rectangle class using C#
    /**
     * Using double will allow you to use decimals which 
     * is much more accurate than int
     * */
    public double width { get; set; }
    public double length { get; set; }

    public Rectangle()
    {
        this.length = 2.0;
        this.width = 1.0;
    }

    public Rectangle(double length, double width)
    {
        this.length = length;
        this.width = width;
    }
    public double area()
    {
        return length * width;
    }
    public double perimeter()
    {
        return 2 * (length + width);
    }
}

代码中有用的小笔记,尽情享受。顺便说一句,我从上面的Farooque Java设计中借用了基本设计。