zheng 1 settimana fa
parent
commit
111ec1b138

+ 17 - 0
11.ts/面向对象/dist/2.构造函数和this.js

@@ -0,0 +1,17 @@
+"use strict";
+// 构造函数
+// function fn1() {
+// };
+// fn1();
+// new fn1();
+class Person1 {
+    constructor(name, age) {
+        this.name = name;
+        this.age = age;
+    }
+    hello() {
+        console.log("hi");
+    }
+}
+let p2 = new Person1('八戒', 10);
+p2.hello();

+ 34 - 0
11.ts/面向对象/dist/3.继承.js

@@ -0,0 +1,34 @@
+"use strict";
+// function fn1() {}
+// fn1()
+// 立即执行函数
+(function () {
+    // 可以保证作用域 唯一性
+    class Money {
+        constructor(name, num) {
+            this.name = name;
+            this.num = num;
+        }
+        say() {
+            console.log("你好");
+        }
+    }
+    /**
+     * 继承
+     * 因为想让多个子类同时拥有父类的属性及方法 所以采用继承
+     * 继承后 子类就会拥有父类相同的内容
+     * 若子类中 定义的方法与父类相同 则会覆盖父类的方法 方法重写
+     */
+    class A extends Money {
+        say() {
+            console.log("大家好");
+        }
+    }
+    class B extends Money {
+    }
+    let aa = new A('孙悟空', 100);
+    let bb = new B('这个', 100);
+    console.log(aa);
+    aa.say();
+    bb.say();
+})();

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

@@ -6,6 +6,6 @@
     <title>Document</title>
 </head>
 <body>
-    <script src="./1.类.js"></script>
+    <script src="./3.继承.js"></script>
 </body>
 </html>

+ 20 - 0
11.ts/面向对象/src/2.构造函数和this.ts

@@ -0,0 +1,20 @@
+// 构造函数
+// function fn1() {
+
+// };
+// fn1();
+// new fn1();
+
+class Person1 {
+    name:string;
+    age:number;
+    constructor(name:string,age:number) {
+        this.name = name;
+        this.age = age;
+    }
+    hello() {
+        console.log("hi")
+    }
+}
+let p2 = new Person1('八戒',10);
+p2.hello()

+ 36 - 0
11.ts/面向对象/src/3.继承.ts

@@ -0,0 +1,36 @@
+// function fn1() {}
+// fn1()
+
+// 立即执行函数
+(function(){
+    // 可以保证作用域 唯一性
+    class Money {
+        name:string;
+        num: number;
+        constructor(name:string,num:number) {
+            this.name = name;
+            this.num = num;
+        }
+        say() {
+            console.log("你好")
+        }
+    }
+    /**
+     * 继承
+     * 因为想让多个子类同时拥有父类的属性及方法 所以采用继承
+     * 继承后 子类就会拥有父类相同的内容
+     * 若子类中 定义的方法与父类相同 则会覆盖父类的方法 方法重写
+     */
+    class A extends Money {
+        say() {
+            console.log("大家好")
+        }
+    }
+    class B extends Money {
+    }
+    let aa = new A('孙悟空',100);
+    let bb = new B('这个',100);
+    console.log(aa);
+    aa.say()
+    bb.say()
+})()