zheng 2 settimane fa
parent
commit
e522c17623

+ 10 - 1
15.ts/4.面向对象/dist/4.super.js

@@ -5,8 +5,17 @@
             this.name = name;
         }
     }
+    /**
+     * 若子类中想添加新的属性
+     * 需要再构造函数中对父类的构造函数进行更新
+     */
     class Cat extends Animal {
+        constructor(name, age) {
+            super(name);
+            this.age = age;
+        }
     }
-    let c = new Cat('猫');
+    // class
+    let c = new Cat('猫', 3);
     console.log(c);
 })();

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

@@ -0,0 +1,22 @@
+"use strict";
+(function () {
+    /**
+     * 抽象类 不能被实例化
+     * 只能继承
+     * 和其他的类没有区别
+     */
+    class News {
+        constructor(name) {
+            this.name = name;
+        }
+    }
+    class B extends News {
+        say() {
+            console.log("哈哈");
+        }
+    }
+    let b = new B('淼淼');
+    // let n = new News('喜羊羊');
+    console.log(b);
+    b.say();
+})();

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

@@ -0,0 +1,16 @@
+"use strict";
+(function () {
+    const obj = {
+        name: '孙悟空',
+        age: 20
+    };
+    console.log(obj);
+    class Person5 {
+        constructor(name, age) {
+            this.name = name;
+            this.age = age;
+        }
+    }
+    let a = new Person5('猪八戒', 22);
+    console.log(a);
+})();

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

@@ -8,7 +8,7 @@
 </head>
 
 <body>
-    <script src="./dist/4.super.js"></script>
+    <script src="./dist/6.接口.js"></script>
     <!-- <script type="module"></script> -->
     <script>
         // function newPart() {

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

@@ -0,0 +1,24 @@
+(function () {
+    /**
+     * 抽象类 不能被实例化
+     * 只能继承
+     * 和其他的类没有区别
+     */
+  abstract class News {
+        name: string;
+        constructor(name: string) {
+            this.name = name
+        }
+       abstract say():void;
+    }
+
+    class B extends News {
+        say() {
+            console.log("哈哈")
+        }
+    }
+    let b = new B ('淼淼');
+    // let n = new News('喜羊羊');
+    console.log(b);
+    b.say();
+})()

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

@@ -0,0 +1,35 @@
+(function () {
+    // type 类型别名 侧重于:重复类型组合 交叉类型 联合类型(&) 没有extends 继承 
+    // 支持所有的类型
+    type happy = {
+        name: string,
+        age: number
+    }
+    const obj:happy = {
+        name:'孙悟空',
+        age:20
+    }
+    console.log(obj);
+    // 接口 一种定义数据的规范 侧重于:api定义 类实现 
+    // 只支持:对象 函数 类
+    // 不能使用交叉类型 联合类型(&)
+    interface Rain {
+        name:string;
+        age:number
+    }
+    interface Rain {
+        sex:string
+    }
+    class Person5 implements Rain {
+        name:string;
+        age: number;
+        sex:string;
+        constructor(name:string,age:number,sex:string) {
+            this.name = name;
+            this.age = age;
+            this.sex = sex;
+        }
+    }
+    let a = new Person5('猪八戒',22,'男');
+    console.log(a)
+})()