zheng 1 주 전
부모
커밋
d20e6995eb

+ 1 - 1
11.ts/面向对象/dist/1.类.js

@@ -15,7 +15,7 @@ class Person {
     }
 }
 Person.age = 23;
-// 实例化
+// 实例化对象
 let p1 = new Person();
 console.log(Person.age, '1');
 // p1.name = '唐僧';

+ 33 - 0
11.ts/面向对象/dist/6.属性的封装.js

@@ -0,0 +1,33 @@
+"use strict";
+(function () {
+    class Person {
+        constructor(name, age, color) {
+            this.name = name;
+            this.age = age;
+            this.color = color;
+        }
+        get age1() {
+            return this.age;
+        }
+        set age1(val) {
+            this.age = val;
+        }
+        getValue() {
+            return this.age + 10;
+        }
+    }
+    class Child extends Person {
+        look() {
+            console.log(this.color);
+        }
+    }
+    let p = new Person("孙悟空", 20, '红色');
+    let c = new Child('图图', 2, '白色');
+    p.age1 = 30;
+    console.log(p.age1);
+    // console.log(p.age);
+    // console.log()
+    c.look();
+    console.log(p.getValue());
+    // console.log(c.color);
+})();

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

@@ -6,6 +6,6 @@
     <title>Document</title>
 </head>
 <body>
-    <script src="./5.接口.js"></script>
+    <script src="./6.属性的封装.js"></script>
 </body>
 </html>

+ 1 - 1
11.ts/面向对象/src/1.类.ts

@@ -14,7 +14,7 @@ class Person {
     }
 }
 
-// 实例化
+// 实例化对象
 let p1 = new Person();
 
 console.log(Person.age,'1')

+ 42 - 0
11.ts/面向对象/src/6.属性的封装.ts

@@ -0,0 +1,42 @@
+(function() {
+    class Person {
+        /**
+         * readonly 只读
+         * static 类本身 而不是类对象
+         * public 公开
+         * private 私有的 类内部可以访问
+         * protected 受保护的 类的内部和子类均可访问
+         */
+        name: string;
+       private age: number;
+       protected color:string;
+        constructor(name:string,age:number,color:string) {
+            this.name = name;
+            this.age = age;
+            this.color = color;
+        }
+        get age1() {
+            return this.age;
+        }
+        set age1(val) {
+            this.age = val;
+        }
+        getValue() {
+            return this.age + 10;
+        }
+    }
+    class Child extends Person {
+        look() {
+            console.log(this.color)
+        }
+    }
+    let p = new Person("孙悟空",20,'红色');
+    let c = new Child('图图',2,'白色');
+    p.age1 = 30;
+    console.log(p.age1)
+    // console.log(p.age);
+    // console.log()
+    c.look();
+    console.log(p.getValue())
+    // console.log(c.color);
+})()