zheng 5 天之前
父节点
当前提交
cd835c3fc9

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

@@ -0,0 +1,33 @@
+(function () {
+    /**
+     * readonly
+     * static
+     * private 私有的 只能使用类去进行获取修改
+     * public 共有的
+     * protected 受保护的 只能在当前类或者当前类的子类中访问
+     */
+    class Person {
+        constructor(name, age) {
+            this.name = name;
+            this.age = age;
+        }
+        getName() {
+            return console.log(this.name);
+        }
+        get name1() {
+            return this.name;
+        }
+        set name1(val) {
+            this.name = val;
+        }
+    }
+    let p = new Person("蜡笔小新", 3);
+    console.log(p, '修改前');
+    // p.name = '图图';
+    // console.log(p.name,'修改后')
+    console.log(Person.name, '哈哈');
+    p.getName();
+    p.name1 = '图图';
+    // p.age = 12;
+    // console.log(p.name1,p.age)
+})();

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

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

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

@@ -0,0 +1,35 @@
+(function() {
+    /**
+     * readonly
+     * static
+     * private 私有的 只能使用类去进行获取修改
+     * public 共有的
+     * protected 受保护的 只能在当前类或者当前类的子类中访问
+     */
+    class Person {
+        private name:string;
+        protected age:number;
+        constructor(name:string,age:number) {
+            this.name = name;
+            this.age = age;
+        }
+        getName() {
+            return console.log(this.name)
+        }
+        get name1() {
+            return this.name;
+        }
+        set name1(val) {
+            this.name = val;
+        }
+    }
+    let p = new Person("蜡笔小新",3);
+    console.log(p,'修改前')
+    // p.name = '图图';
+    // console.log(p.name,'修改后')
+    console.log(Person.name,'哈哈')
+    p.getName()
+    p.name1 = '图图';
+    // p.age = 12;
+    // console.log(p.name1,p.age)
+})()