Advertisement
Todorov_Stanimir

04. Class Hierarchy

Nov 6th, 2019
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     class Figure {
  3.  
  4.         units = {
  5.             m: 0.01,
  6.             cm: 1,
  7.             mm: 10
  8.         }
  9.         defaultUnit;
  10.  
  11.         constructor(unit = "cm") {
  12.             this.defaultUnit = unit;
  13.         }
  14.         get area() { return NaN }
  15.  
  16.         changeUnits(x) { this.defaultUnit = x; };
  17.         calcWidthUnit(x) { return x * this.units[this.defaultUnit]; }
  18.         toString() { return `Figures units: ${this.defaultUnit} Area: ${this.area}` }
  19.     }
  20.  
  21.     class Circle extends Figure {
  22.         radius = 0;
  23.         constructor(r, ...rest) {
  24.             super(...rest);
  25.             this.radius = r;
  26.         }
  27.  
  28.         get area() { return Math.PI * Math.pow(this.calcWidthUnit(this.radius), 2); }
  29.  
  30.         toString() { return super.toString() + ` - radius: ${this.calcWidthUnit(this.radius)}` }
  31.     }
  32.  
  33.     class Rectangle extends Figure {
  34.         width = 0;
  35.         height = 0;
  36.         constructor(w, h, ...rest) {
  37.             super(...rest);
  38.             this.width = w;
  39.             this.height = h;
  40.         }
  41.  
  42.         get h() { return this.calcWidthUnit(this.height); }
  43.         get w() { return this.calcWidthUnit(this.width); }
  44.         get area() { return this.w * this.h; }
  45.  
  46.         toString() { return super.toString() + ` - width: ${this.w}, height: ${this.h}` }
  47.     }
  48.  
  49.     return {Figure, Circle, Rectangle}
  50. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement