简体   繁体   中英

Can we create an instance of a class by combining constructors in Java?

Please have a look at the snippet of the code below which I stumbled upon while reading this java tutorial on creating objects .

    // Declare and create a point object and two rectangle objects.
    Point originOne = new Point(23, 94);
    Rectangle rectOne = new Rectangle(originOne, 100, 200);
    Rectangle rectTwo = new Rectangle(50, 100);

The rectOne object is created by passing an object of Point class ie originOne and width and height of the rectangle. If you have a look at the documentation of Rectangle class, you come to know that there is no such constructor in the documentations which is accepting three parameters (ie A point, width and height). However there are seperate constructors, one of them taking a point of class Point as parameter

Rectangle(Point p)

and the other one taking width and height of the rectangle as parameter

Rectangle(int width, int height)

I was wondering can you combine constructors, as done by the snippet of the code I shared above from the tutorial?

Combining constructors? No such thing in Java .You can convert

Point originOne = new Point(23, 94);
Rectangle rectOne = new Rectangle(originOne, 100, 200);

To

Rectangle rectOne = new Rectangle(new Point(23, 94), 100, 200);//not a Combining.

Rectangle already has a constructor that accepts Point.

No, you can't combine two constructors the way you suggest. Rectangle does have a 3 parameters constructor :

public Rectangle(Point p, int w, int h) {
    origin = p;
    width = w;
    height = h;
}

The Rectangle class you referred to in your documentation link in not related to the one used in the tutorial.

What you could do is call one constructor from another constructor. For example, a constructor with 3 parameters can first call a constructor that only has the first two parameters, and then initialize the 3rd parameter.

For example, the Rectangle constructor can be re-written as :

public Rectangle(Point p, int w, int h) {
    this (p);
    width = w;
    height = h;
}

or

public Rectangle(Point p, int w, int h) {
    this (w,h);
    origin = p;
}

In the example you do not use the java.awt.Rectangle . In this tutorial they use an own implementation of the Rectangle class which have such a constructor.

No, you can't. You can only use the constructors that have already been defined as they are. The best approach is to use the constructor that is most suitable for your use case and use setter methods to assign your additional properties.

Point originOne = new Point(23, 94);
Rectangle rectOne = new Rectangle(originOne);
rectOne.setSize(100, 200);

or

Rectangle rectOne = new Rectangle(originOne.getX(), originOne.getY(), 100, 200);

or

Dimension dimension = new Dimension(100, 200);
Rectangle rectOne = new Rectangle(originOne, dimension);

It depends on you :)

Rectangle offers 7 constructors with different arguments of types int , Point and Dimension . Unfortunalely there is no constructor mixing int with Point or Dimension as you asked for.

Instead you have the following options:

  1. pass only ints (by getting them from the point)

     Point originOne = new Point(23, 94); Rectangle rectOne = new Rectangle(originOne.x, originOne.y, 100, 200); 
  2. pass a dimension as the second argument

     Point originOne = new Point(23, 94); Dimension sizeOne = new Dimension(100, 200); Rectangle rectOne = new Rectangle(originOne, sizeOne); 

For the rectangle without origin you can use

    Dimension sizeTwo = new Dimension(50, 100);
    Rectangle rectTwo = new Rectangle(sizeTwo);

This way you can even easily reuse the origin or the size for different rectangles.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM