Browse Source

7.6: Creating and using classes.

Frederic G. MARAND 5 năm trước cách đây
mục cha
commit
8ad83e0a64
2 tập tin đã thay đổi với 31 bổ sung5 xóa
  1. 9 4
      app/app.ts
  2. 22 1
      app/classes.ts

+ 9 - 4
app/app.ts

@@ -1,6 +1,6 @@
 import {Book, DamageLogger, Librarian} from './interfaces';
 import {Category} from "./enums";
-import {UniversityLibrarian} from "./classes";
+import {ReferenceItem, UniversityLibrarian} from "./classes";
 
 function GetAllBooks(): Book[] {
   const books = [
@@ -160,6 +160,11 @@ let myBook: Book = {
 // let logDamage: DamageLogger = reason => console.log(`Damage reported: ${reason}.`);
 // logDamage('Coffee stains');
 
-let favoriteLibrarian: Librarian = new UniversityLibrarian();
-favoriteLibrarian.name = 'Sharon';
-favoriteLibrarian.assistCustomer('Lynda');
+// let favoriteLibrarian: Librarian = new UniversityLibrarian();
+// 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);

+ 22 - 1
app/classes.ts

@@ -10,4 +10,25 @@ class UniversityLibrarian implements Librarian {
   }
 }
 
-export { UniversityLibrarian };
+class ReferenceItem {
+  static department = 'Research';
+  private _publisher: string;
+
+  constructor(public title: string, private year: number) {
+  }
+
+  printItem(): void {
+    console.log(`${this.title} was published in ${this.year}`);
+    console.log(`Department: ${ReferenceItem.department}.`);
+  }
+
+  get publisher(): string {
+    return this._publisher.toUpperCase();
+  }
+
+  set publisher(p: string) {
+    this._publisher = p;
+  }
+}
+
+export { ReferenceItem, UniversityLibrarian };