File tree Expand file tree Collapse file tree
02.interfaces-and-classes
01.problem.implementing-interfaces
02.problem.programming-to-abstractions
02.problem.method-overriding
04.polymorphism/01.problem.substitutability
05.composition-vs-inheritance/01.problem.when-to-use Expand file tree Collapse file tree Original file line number Diff line number Diff line change 992 . Create a ` ShoppingCart ` class that holds products
10103 . Instantiate and use these classes
1111
12- 💰 Class syntax:
13-
14- ``` ts
15- class ClassName {
16- field: Type
17-
18- constructor (param : Type ) {
19- this .field = param
20- }
21-
22- method(): ReturnType {
23- return value
24- }
25- }
26- ```
12+ 💰 A class includes fields, a constructor, and methods.
2713
2814📜 [ TypeScript Handbook - Classes] ( https://www.typescriptlang.org/docs/handbook/2/classes.html )
Original file line number Diff line number Diff line change 2020// console.log(cart.getTotal())
2121
2222// 🐨 Export your classes so we can verify your work
23- // 💰 export { Product, ShoppingCart }
23+ // 💰 Export the classes you created
Original file line number Diff line number Diff line change @@ -42,22 +42,8 @@ account.#balance // ❌ Error! Private field not accessible
42422 . Use default parameter values
43433 . Use ` # ` prefix for fields that should be private
4444
45- 💰 Parameter property shorthand (reference only):
45+ 💰 This runtime doesn’t support parameter property shorthand.
4646
47- ``` ts
48- constructor (public name : string , public email : string ) {}
49- ```
50-
51- 💰 Private field syntax:
52-
53- ``` ts
54- class Counter {
55- #count: number = 0
56-
57- increment() {
58- this .#count ++
59- }
60- }
61- ```
47+ 💰 Use ` # ` to declare a private field.
6248
6349📜 [ MDN - Private class features] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields )
Original file line number Diff line number Diff line change 2828// const customConfig = new Config('example.com', 8080, true)
2929
3030// 🐨 Export your classes so we can verify your work
31- // 💰 export { User, BankAccount, Config }
31+ // 💰 Export the classes you created
Original file line number Diff line number Diff line change @@ -10,20 +10,6 @@ interfaces to ensure all payment methods follow the same contract.
10103 . Create a ` PayPal ` class that implements ` PaymentMethod `
11114 . Each class should have its own implementation of ` pay() `
1212
13- 💰 Interface syntax:
14-
15- ``` ts
16- interface InterfaceName {
17- method(): ReturnType
18- }
19-
20- class ClassName implements InterfaceName {
21- method(): ReturnType {
22- // Implementation
23- }
24- }
25- ```
26-
27- 💰 The class must implement all methods defined in the interface.
13+ 💰 Classes that implement an interface must define all its methods.
2814
2915📜 [ TypeScript Handbook - Interfaces] ( https://www.typescriptlang.org/docs/handbook/2/objects.html#interfaces-vs-intersections )
Original file line number Diff line number Diff line change 2020// console.log(paypal.pay(50))
2121
2222// 🐨 Export your classes so we can verify your work
23- // 💰 export { CreditCard, PayPal }
23+ // 💰 Export the classes you created
Original file line number Diff line number Diff line change @@ -11,15 +11,6 @@ concrete classes.
11113 . Write a function that processes payments using the interface type
12124 . Test with different payment method implementations
1313
14- 💰 Use interfaces as parameter types:
15-
16- ``` ts
17- function processPayment(method : PaymentMethod , amount : number ) {
18- return method .pay (amount ) // Works with any PaymentMethod implementation
19- }
20- ```
21-
22- 💰 This makes your code flexible—you can add new payment methods without
23- changing the processor code.
14+ 💰 Use the interface type for parameters so different implementations work.
2415
2516📜 [ TypeScript Handbook - Using Interfaces] ( https://www.typescriptlang.org/docs/handbook/2/objects.html#using-interfaces )
Original file line number Diff line number Diff line change 2525// console.log(processPayment(paypal, 50))
2626
2727// 🐨 Export your classes and function so we can verify your work
28- // 💰 export { CreditCard, PayPal, processPayment }
28+ // 💰 Export the classes and function you created
Original file line number Diff line number Diff line change @@ -11,18 +11,7 @@ it with specific shapes like `Circle` and `Rectangle`.
11114 . Create a ` Rectangle ` class that extends ` Shape `
12125 . Add ` width ` and ` height ` fields to ` Rectangle `
1313
14- 💰 Inheritance syntax:
15-
16- ``` ts
17- class Base {
18- field: Type
19- }
20-
21- class Derived extends Base {
22- // Inherits field from Base
23- // Can add more fields and methods
24- }
25- ```
14+ 💰 Use ` extends ` to inherit fields and methods from a base class.
2615
2716💰 Don't forget to call ` super() ` in the constructor if the parent has a
2817constructor.
Original file line number Diff line number Diff line change 2323// console.log(rectangle.width, rectangle.height) // ✅ Should work
2424
2525// 🐨 Export your classes so we can verify your work
26- // 💰 export { Shape, Circle, Rectangle }
26+ // 💰 Export the classes you created
You can’t perform that action at this time.
0 commit comments