e 1 month ago
parent
commit
8b36734bc8

+ 8 - 8
14.ts/4.面向对象/src/1.类.js → 14.ts/4.面向对象/dist/1.类.js

@@ -1,7 +1,8 @@
+"use strict";
 // 通过class取定义类
 // 对象:属性 方法
-var Person = /** @class */ (function () {
-    function Person() {
+class Person {
+    constructor() {
         this.name = '孙悟空';
         this.sex = '男';
     }
@@ -12,13 +13,12 @@ var Person = /** @class */ (function () {
      * 规避name属性
      * readonly 只读不修改
      */
-    Person.sayHello = function () {
+    static sayHello() {
         console.log("你好");
-    };
-    Person.age = 20;
-    return Person;
-}());
-var p = new Person();
+    }
+}
+Person.age = 20;
+let p = new Person();
 // p.name = '猪八戒'
 // console.log(p.)
 // p.sayHello()

+ 10 - 8
14.ts/4.面向对象/src/2.构造函数.js → 14.ts/4.面向对象/dist/2.构造函数.js

@@ -1,3 +1,4 @@
+"use strict";
 /**
  *  构造函数 => 函数
     普通函数
@@ -9,18 +10,19 @@
     立即执行函数
     (function() {
     })()
+    箭头函数
+    () => {}
  */
-var Person1 = /** @class */ (function () {
-    function Person1(name, age) {
-        this.name = name;
+class Person1 {
+    constructor(name, age) {
+        this.a = name;
         // age = b;
         this.age = age;
     }
-    Person1.prototype.hello = function () {
+    hello() {
         console.log("你好我好大家好", this);
-    };
-    return Person1;
-}());
-var p1 = new Person1("孙悟空", 20);
+    }
+}
+let p1 = new Person1('12', 20);
 console.log(p1, 'p1', this);
 p1.hello();

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

@@ -0,0 +1,33 @@
+"use strict";
+(function () {
+    // 父类
+    class Money {
+        constructor(name, num) {
+            this.names = name;
+            this.nums = num;
+        }
+        say() {
+            console.log("你猜猜我有多少钱");
+        }
+    }
+    /**
+     * 继承
+     * 想让多个子类同时拥有父类的属性和方法 所以采用继承
+     * 继承之后 子类就拥有和父类相同的内容
+     * 若子类中 定义的方法与父类相同 则会覆父类的方法 称为方法重写
+     * 若想添加新的方法 则在子类中自行添加即可
+     */
+    class A extends Money {
+        say() {
+            console.log("法海你不懂爱");
+        }
+        back() {
+            console.log("回去吧");
+        }
+    }
+    let m = new Money('唐僧', 100);
+    let aa = new A('法海', 1000);
+    console.log(aa);
+    aa.say();
+    aa.back();
+})();

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

@@ -0,0 +1,28 @@
+"use strict";
+(function () {
+    // 父类
+    class Animal {
+        constructor(name, color) {
+            this.name = name;
+            this.color = color;
+        }
+        eat() {
+            console.log("好吃爱吃多吃");
+        }
+    }
+    // 猫
+    class Cat extends Animal {
+        /**
+         * 若子类继承父类
+         * 子类的构造函数中必须对父类的构造函数进行重写
+         */
+        constructor(a, b, c) {
+            super(a, c);
+            this.age = b;
+        }
+    }
+    let animal = new Animal("猫", '白');
+    let cat = new Cat('妹妹', 3, '黄');
+    console.log(animal);
+    console.log(cat);
+})();

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

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

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

@@ -6,6 +6,6 @@
     <title>Document</title>
 </head>
 <body>
-    <script src="./src/2.构造函数.js"></script>
+    <script src="./dist/5.抽象类.js"></script>
 </body>
 </html>

+ 3 - 1
14.ts/4.面向对象/src/2.构造函数.ts

@@ -9,6 +9,8 @@
     立即执行函数
     (function() {
     })()
+    箭头函数
+    () => {}
  */
 class Person1 {
   a: string;
@@ -22,6 +24,6 @@ class Person1 {
     console.log("你好我好大家好",this);
   }
 }
-let p1 = new Person1(12, 20);
+let p1 = new Person1('12', 20);
 console.log(p1,'p1',this)
 p1.hello()

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

@@ -0,0 +1,34 @@
+(function(){
+    // 父类
+    class Money {
+        names:string;
+        nums:number;
+        constructor(name:string,num:number) {
+            this.names = name;
+            this.nums = num;
+        }
+        say() {
+            console.log("你猜猜我有多少钱")
+        }
+    }
+    /**
+     * 继承
+     * 想让多个子类同时拥有父类的属性和方法 所以采用继承
+     * 继承之后 子类就拥有和父类相同的内容
+     * 若子类中 定义的方法与父类相同 则会覆父类的方法 称为方法重写
+     * 若想添加新的方法 则在子类中自行添加即可 
+     */
+    class A extends Money {
+        say() {
+            console.log("法海你不懂爱")
+        }
+        back() {
+            console.log("回去吧")
+        }
+    }
+    let m = new Money('唐僧',100)
+    let aa = new A('法海',1000)
+    console.log(aa)
+    aa.say()
+    aa.back()
+})()

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

@@ -0,0 +1,30 @@
+(function() {
+    // 父类
+    class Animal {
+        name:string;
+        color:string;
+        constructor(name:string,color:string) {
+            this.name = name;
+            this.color = color;
+        }
+        eat() {
+            console.log("好吃爱吃多吃")
+        }
+    }
+    // 猫
+    class Cat extends Animal {
+        age:number;
+        /**
+         * 若子类继承父类
+         * 子类的构造函数中必须对父类的构造函数进行重写
+         */
+        constructor(a:string,b:number,c:string) {
+            super(a,c);
+            this.age = b;
+        }
+    }
+    let animal = new Animal("猫",'白');
+    let cat = new Cat('妹妹',3,'黄')
+    console.log(animal)
+    console.log(cat)
+})()

+ 26 - 0
14.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 aa = new A("妹妹")
+    aa.eat()
+})()

+ 5 - 3
14.ts/4.面向对象/tsconfig.json

@@ -8,9 +8,11 @@
         "module": "ES2015",
         "lib": ["dom","ES2015"],
         "outDir": "./dist",
-        "strict": true,
+         "strict": true,
         "noImplicitAny": false,
         "noImplicitThis": false,
-        "noEmitOnError": true
+        // "noEmitOnError": true
+
+
     }
-}
+}