//----------------------------------------------------------------- //Static Factory Methods public static Rectangle fromWidthHeight(int w, int h) { //Construct and return a basic rectangle return new Rectangle(10, 5); } public static Rectangle fromSide(int side) { //Construct and return a square //I.e., a rectangle with equal width and height return new Rectangle(side, side); }
private Rectangle(int w, int h) { this.width = w; this.height = h; } private Rectangle() { this(0, 0); }
Rectangle rec, square; //Construct a basic rectangle rec = Rectangle.fromWidthHeight(10, 5); //10x5 //Construct a squuare square = Rectangle.fromSide(8); //8x8
//----------------------------------------------------------------- //Static Factory Methods (Public) static fromWidthHeight(w, h) { //Construct and return a basic rectangle return new Rectangle(10, 5); } static fromSide(side) { //Construct and return a square //I.e., a rectangle with equal width and height return new Rectangle(side, side); }
constructor(w, h) { //Use Nullish_coalescing_operator "??" (args are optional) //default is 0 for each this.w = w ?? 0; this.h = h ?? 0; }
var rec, square; //Construct a basic rectangle rec = Rectangle.fromWidthHeight(10, 5); //10x5 //Construct a squuare square = Rectangle.fromSide(8); //8x8