e 1 month ago
parent
commit
563ad97e22

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

@@ -0,0 +1,20 @@
+"use strict";
+(function () {
+    class Person {
+        constructor(name1, age1) {
+            this.name = name1;
+            this.age = age1;
+        }
+    }
+    let p = new Person('猪八戒', 20);
+    let obj1;
+    obj1 = {
+        name: '孫悟空',
+        age: 10
+    };
+    let f = '12';
+    const obj = {
+        name: 'qw',
+        age: 0
+    };
+})();

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

@@ -0,0 +1,72 @@
+"use strict";
+// 属性 封装:令属性更安全
+(function () {
+    /**
+     * readonly 只读
+     * public 公共的
+     * private 私有的 只能在当前类中进行使用(访问及修改)
+     * protected 受保护的 当前字段在当前类及当前子类中只能访问
+     */
+    // class Person {
+    //     public name:string;
+    //     private age:number;
+    //     constructor(name:string,age:number) {
+    //         this.name = name;
+    //         this.age = age;
+    //     }
+    //     /***
+    //      * 属性封装
+    //      * getter 获取属性值 get
+    //      * setter 设置属性值 set
+    //      */
+    //     get name1() {
+    //         return this.age+'1';
+    //     }
+    //     set name1(value:string) {
+    //          this.name = value
+    //     }
+    //     // getName() {
+    //     //     return this.name;
+    //     // }
+    //     // setName(value:string) {
+    //     //     return this.name = value;
+    //     // }
+    // }
+    // let p = new Person("孙悟空",10)
+    // // p.setName("猪八戒")
+    // // console.log(p.getName(),'实例化对象')
+    // console.log(Person,'类')
+    // // p.age = 12;
+    // p.name1 = '唐僧'
+    // console.log(p.name1)
+    // class A {
+    //     public name:string;
+    //     public age:number;
+    //     constructor(name:string,age:number) {
+    //         this.name = name;
+    //         this.age = age;
+    //     }
+    // }
+    // class B {
+    //     constructor( public name:string,public age:number) {
+    //         this.name = name;
+    //         this.age = age;
+    //     }
+    // }
+    class C {
+        constructor(num) {
+            this.num = num;
+        }
+    }
+    class D extends C {
+        sayHello() {
+            console.log("你好");
+        }
+    }
+    let c = new C(12);
+    console.log(c, 'c');
+    // c.num = 99;
+    let d = new D(23);
+    console.log(d);
+    // d.num = 88;
+})();

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

@@ -6,6 +6,6 @@
     <title>Document</title>
 </head>
 <body>
-    <script src="./dist/5.抽象类.js"></script>
+    <script src="./dist/7.封装的属性.js"></script>
 </body>
 </html>

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

@@ -0,0 +1,73 @@
+// 属性 封装:令属性更安全
+(function(){
+    /**
+     * readonly 只读
+     * public 公共的
+     * private 私有的 只能在当前类中进行使用(访问及修改)
+     * protected 受保护的 当前字段在当前类及当前子类中只能访问
+     */
+    // class Person {
+    //     public name:string;
+    //     private age:number;
+    //     constructor(name:string,age:number) {
+    //         this.name = name;
+    //         this.age = age;
+    //     }
+    //     /***
+    //      * 属性封装
+    //      * getter 获取属性值 get
+    //      * setter 设置属性值 set
+    //      */
+    //     get name1() {
+    //         return this.age+'1';
+    //     }
+    //     set name1(value:string) {
+    //          this.name = value
+    //     }
+    //     // getName() {
+    //     //     return this.name;
+    //     // }
+    //     // setName(value:string) {
+    //     //     return this.name = value;
+    //     // }
+    // }
+    // let p = new Person("孙悟空",10)
+    // // p.setName("猪八戒")
+    // // console.log(p.getName(),'实例化对象')
+    // console.log(Person,'类')
+    // // p.age = 12;
+    // p.name1 = '唐僧'
+    // console.log(p.name1)
+
+    // class A {
+    //     public name:string;
+    //     public age:number;
+    //     constructor(name:string,age:number) {
+    //         this.name = name;
+    //         this.age = age;
+    //     }
+    // }
+    // class B {
+    //     constructor( public name:string,public age:number) {
+    //         this.name = name;
+    //         this.age = age;
+    //     }
+    // }
+    class C {
+        protected num:number;
+        constructor(num:number) {
+            this.num = num;
+        }
+    }
+    class D extends C {
+        sayHello() {
+            console.log("你好")
+        }
+    }
+    let c = new C(12);
+    console.log(c,'c')
+    // c.num = 99;
+    let d = new D(23);
+    console.log(d)
+    // d.num = 88;
+})()