e 2 months ago
parent
commit
e6d44cf6cc

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

@@ -0,0 +1,12 @@
+(function () {
+    // 使用接口
+    class Main {
+        constructor(name, color, price) {
+            this.name = name;
+            this.color = color;
+            this.price = price;
+        }
+    }
+    let news = new Main('牡丹', '红色', 100);
+    console.log(news);
+})();

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

@@ -0,0 +1,29 @@
+(function () {
+    /**
+     * readonly
+     * static
+     * protected 受保护的:只能在当前类的子类中访问
+     * public 共用的
+     * private:只能在当前类进行使用和修改
+     */
+    class Person {
+        constructor(name, age) {
+            this.name = name;
+            this.age = age;
+        }
+        getName() {
+            return console.log;
+        }
+        get name1() {
+            return this.name;
+        }
+        set name1(val) {
+            this.name = val;
+        }
+    }
+    let newPerSon = new Person('哆啦A梦', 7);
+    newPerSon.name1 = '图图';
+    console.log(newPerSon);
+    console.log(newPerSon.name1);
+    newPerSon.getName();
+})();

+ 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>

+ 24 - 0
14.ts/4.面向对象/src/6.接口.ts

@@ -0,0 +1,24 @@
+(function() {
+    // 接口 定义数据的规范
+    interface Flower {
+        name:string,
+        color: string,
+    }
+    interface Flower {
+        price?: number
+    }
+
+    // 使用接口
+    class Main implements Flower {
+        name:string;
+        color: string;
+        price: number;
+        constructor(name:string,color:string,price:number) {
+            this.name = name;
+            this.color = color;
+            this.price = price;
+        }
+    }
+    let news = new Main('牡丹','红色',100)
+    console.log(news)
+})()

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

@@ -0,0 +1,31 @@
+(function() {
+    /**
+     * readonly
+     * static
+     * protected 受保护的:只能在当前类的子类中访问
+     * public 共用的
+     * private:只能在当前类进行使用和修改 
+     */
+    class Person {
+        name: string;
+        private age: number;
+        constructor(name:string,age:number) {
+            this.name = name;
+            this.age = age;
+        }
+        getName(){
+            return console.log;
+        }
+        get name1() {
+            return this.name;
+        }
+        set name1(val) {
+             this.name = val;
+        }
+    }
+    let newPerSon = new Person('哆啦A梦', 7);
+    newPerSon.name1 = '图图';
+    console.log(newPerSon);
+    console.log(newPerSon.name1);
+    newPerSon.getName();
+})()