zheng 5 days ago
parent
commit
194004cccb

+ 23 - 0
ts/4.面向对象/dist/5.抽象类.js

@@ -0,0 +1,23 @@
+(function () {
+    /**
+     * abstract 与其他类差别不大
+     * 抽象类不是为了实例化对象
+     * 它是因为继承产生的
+    */
+    class Animal {
+        constructor(name) {
+            this.name = name;
+        }
+    }
+    class Child3 extends Animal {
+        say() {
+            console.log(`你好${this.name}`);
+        }
+    }
+    let c1 = new Child3("花花");
+    console.log(c1);
+    c1.say();
+    //    let a1 = new Animal('图图');
+    //    console.log(a1,'a1')
+    //    a1.say()
+})();

+ 13 - 0
ts/4.面向对象/dist/6.接口.js

@@ -0,0 +1,13 @@
+(function () {
+    // 使用接口
+    class Main {
+        // price: number;
+        constructor(name, color, price) {
+            this.name = name;
+            this.color = color;
+            // this.price = price;
+        }
+    }
+    let m = new Main('牡丹', '红色', 100);
+    console.log(m);
+})();

+ 1 - 1
ts/4.面向对象/index.html

@@ -6,6 +6,6 @@
     <title>Document</title>
 </head>
 <body>
-    <script src="./dist/4.super.js"></script>
+    <script src="./dist/6.接口.js"></script>
 </body>
 </html>

+ 28 - 0
ts/4.面向对象/src/5.抽象类.ts

@@ -0,0 +1,28 @@
+(function(){
+    /**
+     * abstract 与其他类差别不大
+     * 抽象类不是为了实例化对象
+     * 它是因为继承产生的
+    */
+   abstract class Animal {
+        name:string;
+        constructor(name:string) {
+            this.name = name;
+        }
+        // say(){
+        //     console.log("你好")
+        // }
+        abstract say():void
+   }
+   class Child3 extends Animal{
+    say() {
+        console.log(`你好${this.name}`)
+    }
+   }
+   let c1 = new Child3("花花");
+   console.log(c1);
+   c1.say()
+//    let a1 = new Animal('图图');
+//    console.log(a1,'a1')
+//    a1.say()
+})()

+ 23 - 0
ts/4.面向对象/src/6.接口.ts

@@ -0,0 +1,23 @@
+(function() {
+    // 定义接口
+    interface Flower {
+        name:string;
+        color:string;
+    }
+    interface Flower {
+        price?:number
+    }
+    // 使用接口
+    class Main implements Flower {
+        name:string;
+        color: string;
+        // price: number;
+        constructor(name:string,color:string,price:number){
+            this.name = name;
+            this.color = color;
+            // this.price = price;
+        }
+    }
+    let m = new Main('牡丹','红色',100)
+    console.log(m)
+})()