瀏覽代碼

7.11: Using class expressions.

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

+ 22 - 3
app/app.ts

@@ -1,6 +1,11 @@
 import {Book, DamageLogger, Librarian} from './interfaces';
 import {Category} from "./enums";
-import {Encyclopedia, ReferenceItem, UniversityLibrarian} from "./classes";
+import {
+  Encyclopedia,
+  Journal,
+  ReferenceItem,
+  UniversityLibrarian
+} from "./classes";
 
 function GetAllBooks(): Book[] {
   const books = [
@@ -169,5 +174,19 @@ let myBook: Book = {
 // ref.printItem();
 // console.log(ref.publisher);
 
-let refBook = new Encyclopedia('WorldPedia', 1900, 10);
-refBook.printItem();
+let Newspaper = class extends ReferenceItem {
+  printCitation(): void {
+    console.log(`Newspaper: ${this.title}.`);
+  }
+};
+
+let myPaper = new Newspaper('The gazette', 2016);
+myPaper.printCitation();
+
+class Novel extends class { title: string } {
+  mainCharacter: string;
+}
+let favoriteNovel = new Novel();
+favoriteNovel.mainCharacter = 'Hero';
+favoriteNovel.title = 'Plot';
+console.log(favoriteNovel);

+ 12 - 3
app/classes.ts

@@ -10,11 +10,11 @@ class UniversityLibrarian implements Librarian {
   }
 }
 
-class ReferenceItem {
+abstract class ReferenceItem {
   static department = 'Research';
   private _publisher: string;
 
-  constructor(public title: string, protected year: number) {
+  constructor(public title: string, protected year?: number) {
   }
 
   printItem(): void {
@@ -29,6 +29,8 @@ class ReferenceItem {
   set publisher(p: string) {
     this._publisher = p;
   }
+
+  abstract printCitation(): void;
 }
 
 class Encyclopedia extends ReferenceItem {
@@ -40,6 +42,10 @@ class Encyclopedia extends ReferenceItem {
     super.printItem();
     console.log(`Edition: ${this.edition} (${this.year}).`);
   }
+
+  printCitation(): void {
+    console.log(`${this.title} - ${this.year}.`);
+  }
 }
 
 class Journal extends ReferenceItem {
@@ -49,6 +55,9 @@ class Journal extends ReferenceItem {
     super(title, year);
   }
 
+  printCitation(): void {
+    console.log(`${this.title} - ${JSON.stringify(this.contributors)}.`);
+  }
 }
 
-export { Encyclopedia, ReferenceItem, UniversityLibrarian };
+export { Encyclopedia, Journal, ReferenceItem, UniversityLibrarian };