e 1 年之前
父節點
當前提交
c1dc784d6d

+ 75 - 0
ts/4.面向对象/dist/7.属性的封装.js

@@ -0,0 +1,75 @@
+"use strict";
+// 属性的封装:令属性更加安全
+(function () {
+    /**
+     * readonly 只读
+     * static
+     * public 共有的
+     * private 私有的 只能在当前类中访问及修改
+     *  若想要更改 则需在类中定义方法
+     * protected 受保护的 只能在当前累及当前类的子类中访问及使用
+     */
+    class Person {
+        constructor(name1, age1) {
+            this.age1 = age1;
+            this.name1 = name1;
+        }
+        /**
+         * 在属性的封装中
+         * getter 获取属性值 get
+         * setter 设置属性值 set
+         */
+        get name() {
+            return this.name1;
+        }
+        set name(value) {
+            this.name1 = value;
+        }
+        get age() {
+            return this.age1;
+        }
+        set age(value) {
+            if (value > 0) {
+                this.age1 = value;
+            }
+        }
+    }
+    let p = new Person("孙悟空", 20);
+    // p.name = "猪八戒";
+    // p.age = 10;
+    // console.log(p.getName());
+    // console.log(p.setName("哈哈"));
+    // console.log(p.setAge(-10))
+    p.name = '小小';
+    p.age = 1;
+    console.log(p);
+    console.log(p.name);
+    // console.log(Person.age)
+    // class A {
+    //     public name: string;
+    //     public age: number;
+    //     constructor(name: string, age: number) {
+    //         this.name = name;
+    //         this.age = age;
+    //     }
+    // }
+    // class A {
+    //     constructor(public name:string,public age:number) {
+    //         this.name = name
+    //         this.age = age
+    //     }
+    // }
+    class B {
+        constructor(num) {
+            this.num = num;
+        }
+    }
+    class C extends B {
+        say() {
+            console.log(this.num);
+        }
+    }
+    let c = new C(100);
+    console.log(c);
+    // C.num = 1;
+})();

+ 32 - 0
ts/4.面向对象/dist/8.泛型.js

@@ -0,0 +1,32 @@
+"use strict";
+(function () {
+    // function fn1(name:string):string {
+    //     return name;
+    // }
+    // function fn2(name:any):any {
+    //     return name;
+    // }
+    // 泛型:先用字符去指代未知类型 使用时传入具体类型
+    function fn3(name) {
+        return name;
+    }
+    fn3(12); //若传入时 未直接指出类型 则ts默认解析传入类型
+    fn3("12"); // 若传入是  直接指出类型  将类型写在<>内
+    // 传入多个值
+    function fn4(a, b) {
+        return [a, b];
+    }
+    fn4("11", true);
+    // 若函数中泛型继承接口 则传入的值必须符合接口条件
+    function fn5(a) {
+        return a;
+    }
+    fn5({ jump: "0" });
+    class Ending {
+        constructor(name) {
+            this.name = name;
+        }
+    }
+    let end = new Ending({ jump: "唐僧啊" });
+    console.log(end);
+})();

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

@@ -6,6 +6,6 @@
     <title>Document</title>
 </head>
 <body>
-    <script src="./dist/6.接口.js"></script>
+    <script src="./dist/8.泛型.js"></script>
 </body>
 </html>

+ 100 - 0
ts/4.面向对象/src/7.属性的封装.ts

@@ -0,0 +1,100 @@
+// 属性的封装:令属性更加安全
+(function(){
+    /**
+     * readonly 只读
+     * static
+     * public 共有的
+     * private 私有的 只能在当前类中访问及修改
+     *  若想要更改 则需在类中定义方法
+     * protected 受保护的 只能在当前累及当前类的子类中访问及使用
+     */
+    class Person {
+        public name1:string;
+        age1:number
+        constructor(name1:string,age1:number) {
+            this.age1 = age1;
+            this.name1 = name1;
+        }
+        /**
+         * 在属性的封装中
+         * getter 获取属性值 get
+         * setter 设置属性值 set
+         */
+        get name() {
+            return this.name1;
+        }
+        set name(value) {
+            this.name1 = value;
+        }
+        get age() {
+            return this.age1;
+        }
+        set age(value) {
+            if(value > 0) {
+                this.age1 = value;
+            }
+        }
+        // getName() {
+        //     return this.name;
+        // }
+        // setName(value) {
+        //     return this.name = value;
+        // }
+        // getAge() {
+        //     return this.age;
+        // }
+        // setAge(value) {
+        //     if(value > 0 ) {
+        //         return this.age = value;
+        //     }
+        // }
+
+    }
+    let p = new Person("孙悟空",20);
+    // p.name = "猪八戒";
+    // p.age = 10;
+    
+    // console.log(p.getName());
+    // console.log(p.setName("哈哈"));
+    // console.log(p.setAge(-10))
+    p.name = '小小';
+    p.age = 1;
+    console.log(p);
+    console.log(p.name)
+
+    // console.log(Person.age)
+
+    // class A {
+    //     public name: string;
+    //     public age: number;
+    //     constructor(name: string, age: number) {
+    //         this.name = name;
+    //         this.age = age;
+    //     }
+    // }
+
+    // class A {
+    //     constructor(public name:string,public age:number) {
+    //         this.name = name
+    //         this.age = age
+    //     }
+    // }
+
+    class B {
+        protected num: number;
+        constructor(num: number) {
+            this.num = num;
+        }
+    }
+
+    class C extends B {
+        say() {
+            console.log(this.num);
+        }
+    }
+    let c = new C(100);
+    console.log(c);
+    // C.num = 1;
+
+    
+})()

+ 42 - 0
ts/4.面向对象/src/8.泛型.ts

@@ -0,0 +1,42 @@
+(function() {
+    // function fn1(name:string):string {
+    //     return name;
+    // }
+    // function fn2(name:any):any {
+    //     return name;
+    // }
+    // 泛型:先用字符去指代未知类型 使用时传入具体类型
+    function fn3<T>(name:T):T {
+        return name;
+    }
+    fn3(12); //若传入时 未直接指出类型 则ts默认解析传入类型
+    fn3<string>("12"); // 若传入是  直接指出类型  将类型写在<>内
+
+    // 传入多个值
+    function fn4<T,W>(a:T,b:W):[T,W] {
+        return [a,b];
+    }
+    fn4<string,boolean>("11",true);
+
+    // 接口
+    interface happy {
+        jump:string;
+    }
+    // 若函数中泛型继承接口 则传入的值必须符合接口条件
+    function fn5<T extends happy>(a:T):T {
+        return a;
+    }    
+
+    fn5({jump:"0"});
+
+    class Ending<T extends happy> {
+        name:T;
+        constructor(name:T) {
+            this.name = name;
+        }
+    }
+
+    let end = new Ending({jump:"唐僧啊"});
+    console.log(end);
+
+})()