zheng 6 dienas atpakaļ
vecāks
revīzija
f4a83c5340

+ 1 - 1
ts/4.面向对象/dist/2.构造函数和this.js

@@ -7,9 +7,9 @@
 // 箭头函数() => xx
 // 匿名函数
 // let xxx =function() {}
-// 构造函数:构造器(属性) 原型(方法)
 // function Fn1() {
 // }
+// 构造函数:构造器(属性) 原型(方法)
 class Person1 {
     // 构造器
     constructor(x, y) {

+ 31 - 0
ts/4.面向对象/dist/3.继承.js

@@ -0,0 +1,31 @@
+(function () {
+    /**
+     * 继承
+     * 因为想让多个子类同时继承父类的属性及方法 所以采用继承
+     * 如果子类中的方法与父类中相同 则显示子类中方法 这种情况成为方法重写
+    */
+    class Person2 {
+        constructor(name, age) {
+            this.name = name;
+            this.age = age;
+        }
+        say() {
+            console.log(`我叫${this.name},今年${this.age}岁`);
+        }
+    }
+    class Child1 extends Person2 {
+        say() {
+            console.log("haha哈");
+        }
+        hello() {
+            console.log("你好a");
+        }
+    }
+    let p2 = new Person2('图图', 3);
+    console.log(p2);
+    let child1 = new Child1("LiLi", 20);
+    console.log(child1);
+    child1.say();
+    child1.hello();
+})();
+// 自动编译 tsc --watch / tsc -w

+ 27 - 0
ts/4.面向对象/dist/4.super.js

@@ -0,0 +1,27 @@
+(function () {
+    class Animal {
+        constructor(type, color) {
+            this.type = type;
+            this.color = color;
+        }
+        say() {
+            console.log(`这有一只${this.type}`);
+        }
+    }
+    /**
+     * super 调用父类中的属性
+     * 若子类中添加新的属性
+     * 子类的构造器需要对父类的构造器中的属性使用super继承
+     */
+    class Child2 extends Animal {
+        constructor(type, color, action) {
+            super(type, color);
+            this.action = action;
+        }
+    }
+    let a = new Animal('小猫', '白');
+    let c = new Child2("小狗", '黑', '汪汪');
+    console.log(a);
+    a.say();
+    console.log(c);
+})();

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

@@ -6,6 +6,6 @@
     <title>Document</title>
 </head>
 <body>
-    <script src="./dist/2.构造函数和this.js"></script>
+    <script src="./dist/4.super.js"></script>
 </body>
 </html>

+ 35 - 0
ts/4.面向对象/src/3.继承.ts

@@ -0,0 +1,35 @@
+(function(){
+    /**
+     * 继承
+     * 因为想让多个子类同时继承父类的属性及方法 所以采用继承
+     * 如果子类中的方法与父类中相同 则显示子类中方法 这种情况成为方法重写
+    */
+    class Person2 {
+        name:string;
+        age:number;
+        constructor(name:string,age:number) {
+            this.name = name;
+            this.age = age;
+        }
+        say() {
+            console.log(`我叫${this.name},今年${this.age}岁`)
+        }
+    }
+
+    class Child1 extends Person2 {
+        say() {
+            console.log("haha哈")
+        }
+        hello() {
+            console.log("你好a")
+        }
+    }
+
+    let p2 = new Person2('图图',3)
+    console.log(p2)
+    let child1 = new Child1("LiLi",20);
+    console.log(child1);
+    child1.say()
+    child1.hello()
+})()
+// 自动编译 tsc --watch / tsc -w

+ 30 - 0
ts/4.面向对象/src/4.super.ts

@@ -0,0 +1,30 @@
+(function(){
+    class Animal {
+        type:string;
+        color:string;
+        constructor(type:string,color:string) {
+            this.type = type;
+            this.color = color;
+        }
+        say(){
+            console.log(`这有一只${this.type}`)
+        }
+    }
+    /**
+     * super 调用父类中的属性
+     * 若子类中添加新的属性
+     * 子类的构造器需要对父类的构造器中的属性使用super继承
+     */
+    class Child2 extends Animal {
+        action: string;
+        constructor(type:string,color:string,action:string) {
+            super(type,color);
+            this.action = action;
+        }
+    }
+    let a = new Animal('小猫','白')
+    let c = new Child2("小狗",'黑','汪汪')
+    console.log(a);
+    a.say()
+    console.log(c);
+})()