zoukankan      html  css  js  c++  java
  • ts中类的属性的封装

    ts中类的属性的封装

    
    (function(){
        /* 
            public 修饰的属性可以在任意位置访问(修改)默认值
            private :私有属性,私有属性只能在类的内部进行访问
                    - 通过在类中添加方法使得私有属性可以被外部访问
    
            protected 受保护的属性,只能在当前类的子类中访问(修改)
        */
    
        class Person {
            private _name :string;
            private _age :number;
    
            constructor(name:string,age:number){
                this._name = name
                this._age = age
            }
    
            /* 
            getter方法用来读取属性
            setter方法用来设置属性
            */
    
            get name(){
                return this._name
            }
    
            set name(value){
                this.name = value
            }
    
            get age(){
                return this._age
            }
    
            set age(value){
                if(value >0){
                    console.log('age合法');
                }
    
                this._age = value
            }
    
        }
    
        let per = new Person("张三",18);
        per.age =10
        // console.log(per.age);
    
        class A {
            protected num:number
    
            constructor(num){
                this.num = num 
            }
        }
    
        class B extends A {
    
            test(){
                
                console.log(this.num);
            }
        }
    
        const b = new B(10);
        b.test()
    
    
    
    })()
    
  • 相关阅读:
    centos 用户管理
    rsync 实验
    文件共享和传输
    PAT 1109 Group Photo
    PAT 1108 Finding Average
    PAT 1107 Social Clusters
    PAT 1106 Lowest Price in Supply Chain
    PAT 1105 Spiral Matrix
    PAT 1104 Sum of Number Segments
    PAT 1103 Integer Factorization
  • 原文地址:https://www.cnblogs.com/malong1992/p/15376491.html
Copyright © 2011-2022 走看看