kidobreva

inheritance

Mar 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Person (name, age, isMale) {
  2.     this.name = name;
  3.     this.age = age;
  4.     this.isMale = isMale;
  5. }
  6.  
  7. Person.prototype.showPersonInfo = function () {
  8.     console.log(this.name + ' is a ' + this.age + ' years old ' + (this.isMale === 'true' ? 'man' : 'woman') + '.');
  9. };
  10.  
  11. function Student (name, age, isMale, score) {
  12.     Person.call(this, name, age, isMale);
  13.     this.score = score;
  14. }
  15.  
  16. Student.prototype = Object.create(Person.prototype);
  17. Student.prototype.constructor = Student;
  18. Student.prototype.super = Person;
  19.  
  20. Student.prototype.showStudentInfo = function () {
  21.     this.showPersonInfo();
  22.     console.log(this.name + ' has ' + this.score + ' scores.');
  23. };
  24.  
  25. function Employee (name, age, isMale, daySalary) {
  26.     Person.call(this, name, age, isMale);
  27.     this.daySalary = daySalary;
  28. }
  29.  
  30. Employee.prototype = Object.create(Person.prototype);
  31. Employee.prototype.constructor = Employee;
  32. Employee.prototype.super = Person;
  33.  
  34. Employee.prototype.showEmployeeInfo = function () {
  35.     this.showPersonInfo();
  36.     console.log(this.name + ' earns ' + this.daySalary + ' lv per day.');
  37. };
  38.  
  39. Employee.prototype.calculateOvertime = function (hours) {
  40.     if (this.age < 18) {
  41.         return 0;
  42.     }
  43.     var moneyPerHours = this.daySalary / 8;
  44.  
  45.     return (hours * moneyPerHours * 1.5).toFixed(2);
  46. };
  47.  
  48. var arrPerson = [],
  49.     id = 1;
  50. for (var i = 0; i < 10; i++) {
  51.     arrPerson.push(new Person('Person' + id++, 15 + Math.round(Math.random() * 30), ['true', 'false'][Math.round(Math.random())]));
  52. }
  53.  
  54. for (var i = 0; i < 2; i++) {
  55.     arrPerson.push(new Person('Person' + id++,
  56.                                 15 + Math.round(Math.random() * 30),
  57.                                 ['true', 'false'][Math.round(Math.random())]));
  58.     arrPerson.push(new Student('Person' + id++,
  59.                                 15 + Math.round(Math.random() * 30),
  60.                                 ['true', 'false'][Math.round(Math.random())],
  61.                                 Math.round(Math.random() * 10)));
  62.     arrPerson.push(new Employee('Person' + id++,
  63.                                 15 + Math.round(Math.random() * 30),
  64.                                 ['true', 'false'][Math.round(Math.random())],
  65.                                 10 + Math.round(Math.random() * 20)));
  66. }
  67.  
  68.  
  69. arrPerson.forEach(function(person) {
  70.     //console.log(person);
  71.     if (person instanceof Student) {
  72.         person.showStudentInfo();
  73.     } else if (person instanceof Employee) {
  74.         person.showEmployeeInfo();
  75.     } else {
  76.         person.showPersonInfo();
  77.     }
  78. });
  79.  
  80. console.log('-------------------------');
  81. for (var i = 0; i < arrPerson.length; i++) {
  82.     //console.log(arrPerson[i])
  83.     if (arrPerson[i] instanceof Employee) {
  84.         console.log('For 2 hours overtime, ' + arrPerson[i].name + ' earns ' + arrPerson[i].calculateOvertime(2));
  85.     }
  86. }
  87.  
  88.  
  89. //зад.5
  90. Person.prototype.showInfo = function () {
  91.     console.log(this.name + ' is a ' + this.age + ' years old ' + (this.isMale === 'true' ? 'man' : 'woman') + '.');
  92. };
  93.  
  94. Student.prototype = Object.create(Person.prototype);
  95. Student.prototype.constructor = Student;
  96. Student.prototype.super = Person;
  97.  
  98. Student.prototype.showInfo = function () {
  99.     Person.prototype.showInfo.call(this);
  100.     console.log(this.name + ' has ' + this.score + ' scores.');
  101. };
  102.  
  103. Employee.prototype = Object.create(Person.prototype);
  104. Employee.prototype.constructor = Employee;
  105. Employee.prototype.super = Person;
  106.  
  107. Employee.prototype.showInfo = function () {
  108.     Person.prototype.showInfo.call(this);
  109.     console.log(this.name + ' earns ' + this.daySalary + ' lv per day.');
  110. };
  111.  
  112. arrPerson.forEach(function(person) {
  113.     person.showInfo();
  114. }
Add Comment
Please, Sign In to add comment