Browse Source

6.12: implementing interfaces with classes.

Frederic G. MARAND 5 years ago
parent
commit
0ec8059e40
3 changed files with 19 additions and 13 deletions
  1. 5 12
      app/app.ts
  2. 13 0
      app/classes.ts
  3. 1 1
      app/interfaces.ts

+ 5 - 12
app/app.ts

@@ -1,5 +1,6 @@
-import {Author, Book, DamageLogger, Librarian} from './interfaces';
+import {Book, DamageLogger, Librarian} from './interfaces';
 import {Category} from "./enums";
+import {UniversityLibrarian} from "./classes";
 
 function GetAllBooks(): Book[] {
   const books = [
@@ -159,14 +160,6 @@ let myBook: Book = {
 // let logDamage: DamageLogger = reason => console.log(`Damage reported: ${reason}.`);
 // logDamage('Coffee stains');
 
-let favoriteAuthor: Author = {
-  email: 'foo@example.com',
-  numBooksPublished: 1,
-  name: 'Foo',
-};
-let favoriteLibrarian: Librarian = {
-  email: "jane@example.com",
-  name: 'Jane Doe',
-  departement: 'Sales',
-  assistCustomer: (custName: string) => console.log(`Helping ${custName}.'), 
-}
+let favoriteLibrarian: Librarian = new UniversityLibrarian();
+favoriteLibrarian.name = 'Sharon';
+favoriteLibrarian.assistCustomer('Lynda');

+ 13 - 0
app/classes.ts

@@ -0,0 +1,13 @@
+import {Author, Book, DamageLogger, Librarian} from './interfaces';
+
+class UniversityLibrarian implements Librarian {
+  department: string;
+  email: string;
+  name: string;
+
+  assistCustomer(custName: string): void {
+    console.log(`${this.name} is assisting ${custName}.`);
+  }
+}
+
+export { UniversityLibrarian };

+ 1 - 1
app/interfaces.ts

@@ -25,7 +25,7 @@ interface Author extends Person {
 }
 
 interface Librarian extends Person {
-  departement: string;
+  department: string,
   assistCustomer: (custName: string) => void,
 }