瀏覽代碼

7.8: Extending classes.

Frederic G. MARAND 5 年之前
父節點
當前提交
6e153c0048
共有 2 個文件被更改,包括 30 次插入7 次删除
  1. 8 5
      app/app.ts
  2. 22 2
      app/classes.ts

+ 8 - 5
app/app.ts

@@ -1,6 +1,6 @@
 import {Book, DamageLogger, Librarian} from './interfaces';
 import {Category} from "./enums";
-import {ReferenceItem, UniversityLibrarian} from "./classes";
+import {Encyclopedia, ReferenceItem, UniversityLibrarian} from "./classes";
 
 function GetAllBooks(): Book[] {
   const books = [
@@ -164,7 +164,10 @@ let myBook: Book = {
 // favoriteLibrarian.name = 'Sharon';
 // favoriteLibrarian.assistCustomer('Lynda');
 
-let ref: ReferenceItem = new ReferenceItem('Facts and Figures', 2016);
-ref.publisher = 'Random Data publisher';
-ref.printItem();
-console.log(ref.publisher);
+// let ref: ReferenceItem = new ReferenceItem('Facts and Figures', 2016);
+// ref.publisher = 'Random Data publisher';
+// ref.printItem();
+// console.log(ref.publisher);
+
+let refBook = new Encyclopedia('WorldPedia', 1900, 10);
+refBook.printItem();

+ 22 - 2
app/classes.ts

@@ -14,7 +14,7 @@ class ReferenceItem {
   static department = 'Research';
   private _publisher: string;
 
-  constructor(public title: string, private year: number) {
+  constructor(public title: string, protected year: number) {
   }
 
   printItem(): void {
@@ -31,4 +31,24 @@ class ReferenceItem {
   }
 }
 
-export { ReferenceItem, UniversityLibrarian };
+class Encyclopedia extends ReferenceItem {
+  constructor(newTitle: string, newYear: number, public edition: number) {
+    super(newTitle, newYear);
+  }
+
+  printItem(): void {
+    super.printItem();
+    console.log(`Edition: ${this.edition} (${this.year}).`);
+  }
+}
+
+class Journal extends ReferenceItem {
+  public contributors: string[];
+
+  constructor(title: string, year: number) {
+    super(title, year);
+  }
+
+}
+
+export { Encyclopedia, ReferenceItem, UniversityLibrarian };