e 1 year ago
parent
commit
f91acba1a3

+ 0 - 5
ts/3.编译选项/dist/m.js

@@ -1,6 +1 @@
 export const mm = '妹妹';
-function fn1(x, y) {
-    console.log(this, 'this');
-    return x + y;
-}
-fn1(2, '43');

+ 0 - 5
ts/3.编译选项/src/m.ts

@@ -1,6 +1 @@
 export const mm = '妹妹';
-function fn1(x:any,y:string) {
-    console.log(this,'this')
-    return x+y;
-}
-fn1(2,'43');

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

@@ -0,0 +1,40 @@
+"use strict";
+// (function(){
+// 立即执行函数 可以保证作用域 名字的唯一性
+// })()
+(function () {
+    // 父类
+    class Money {
+        constructor(name, num) {
+            this.name = name;
+            this.num = num;
+        }
+        say() {
+            console.log("你猜我有多少,哈哈哈哈");
+        }
+    }
+    /**
+     * 继承
+     * 因为想让多个子类同时拥有父类的属性及方法 所以采用继承
+     * 继承后 子类就会拥有父类相同的内容
+     * 若子类中 定义的方法与父类相同 则会覆盖父类该方法 称为 方法重写
+     * 若想添加新的方法 则在子类中自行添加即可
+     */
+    // 子类
+    class A extends Money {
+        say() {
+            console.log("我继承到了");
+        }
+        back() {
+            console.log("返回");
+        }
+    }
+    class B extends Money {
+    }
+    // 调用
+    let aa = new A("悟空", 20);
+    aa.say();
+    aa.back();
+    let bb = new B("八戒", 10);
+    bb.say();
+})();

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

@@ -0,0 +1,26 @@
+"use strict";
+(function () {
+    // 父类
+    class Animal {
+        constructor(name) {
+            this.name = name;
+        }
+        eat() {
+            console.log("吃的真香");
+        }
+    }
+    class Cat extends Animal {
+        /**
+         * 若子类继承父类
+         * 子类的构造函数中必须对父类的构造函数进行重新
+         */
+        constructor(name, age) {
+            super(name);
+            this.age = age;
+        }
+    }
+    let cat = new Cat('喵喵', 3);
+    console.log(cat.name);
+    console.log(cat.age);
+    cat.eat();
+})();

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

@@ -0,0 +1,20 @@
+"use strict";
+(function () {
+    /**
+     * 抽象类 与其他类差别不大 abstract
+     * 抽象类不是为了实例化对象
+     * 它是因继承而产生的类
+     */
+    class Animal {
+        constructor(name) {
+            this.name = name;
+        }
+    }
+    class A extends Animal {
+        eat() {
+            console.log(this.name + "吃了很多饭");
+        }
+    }
+    let a = new A("狗狗");
+    a.eat();
+})();

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

@@ -0,0 +1,23 @@
+"use strict";
+(function () {
+    // 类型别名 定义数据
+    const obj = {
+        name: "孙悟空",
+        age: 20,
+    };
+    // 继承 extends
+    // 接口 interface implements
+    class Person {
+        constructor(name, age, sex, num) {
+            this.name = name;
+            this.age = age;
+            this.num = num;
+            this.sex = sex;
+        }
+        say() {
+            console.log(this.name, this.age, this.num, this.sex, '我是个好人');
+        }
+    }
+    let a = new Person("猪八戒", 20, "男", 1);
+    a.say();
+})();

+ 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/6.接口.js"></script>
 </body>
 </html>

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

@@ -0,0 +1,48 @@
+// (function(){
+    // 立即执行函数 可以保证作用域 名字的唯一性
+// })()
+
+(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("我继承到了")
+        }
+        back() {
+            console.log("返回")
+        }
+    }
+
+    class B extends Money {
+
+    }
+    
+    // 调用
+    let aa = new A("悟空",20);
+    aa.say();
+    aa.back()
+
+    let bb = new B("八戒",10);
+    bb.say()
+})()

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

@@ -0,0 +1,33 @@
+(function() {
+    // 父类
+    class Animal {
+        name: string;
+        constructor(name: string) {
+            this.name = name;
+        }
+        eat() {
+            console.log("吃的真香");
+        }
+    }
+
+    class Cat extends Animal {
+        age: number;
+        /**
+         * 若子类继承父类
+         * 子类的构造函数中必须对父类的构造函数进行重新
+         */
+        constructor(name: string, age: number) {
+            super(name);
+            this.age = age;
+        }
+        // haha() {
+        //     super.eat();
+        // }
+        // super()
+    }
+
+    let cat = new Cat('喵喵',3);
+    console.log(cat.name);
+    console.log(cat.age)
+    cat.eat()
+})()

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

@@ -0,0 +1,26 @@
+(function() {
+    /**
+     * 抽象类 与其他类差别不大 abstract
+     * 抽象类不是为了实例化对象
+     * 它是因继承而产生的类
+     */
+    abstract class Animal {
+        name: string;
+        constructor(name: string) {
+            this.name = name;
+        }
+        // 抽象类中 方法没有具体内容 只有方法体
+        // 具体方法 需要在继承的子类中重写
+        abstract eat():void;
+    }
+
+    class A extends Animal {
+        eat() {
+            console.log(this.name + "吃了很多饭");
+        }
+    }
+
+    let a = new A("狗狗");
+    a.eat();
+
+})()

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

@@ -0,0 +1,48 @@
+(function() {
+    type happy = {
+        name: string,
+        age: number,
+    }
+    // 类型别名 定义数据
+    const obj:happy = {
+        name: "孙悟空",
+        age: 20,
+    }
+    /**
+     * 接口 换而言之 也是一种定义数据的规范
+     * 
+     */
+    interface sad {
+        name: string,
+        age: number,
+    }
+
+    interface sad {
+        sex: string
+    }
+
+    // 继承 extends
+    // 接口 interface implements
+    class Person implements sad {
+        // num: number;
+        // constructor(num: number) {
+        //     this.num = num;
+        // }
+        name: string;
+        age: number;
+        num: number;
+        sex: string;
+        constructor(name: string, age: number, sex: string,num:number) {
+            this.name = name;
+            this.age = age;
+            this.num = num;
+            this.sex = sex;
+        }
+        say() {
+            console.log(this.name, this.age, this.num, this.sex,'我是个好人');
+        }
+
+    }
+    let a = new Person("猪八戒", 20, "男",1);
+    a.say()
+})()