zoukankan      html  css  js  c++  java
  • TypeScript

    class Person {
        /**
         * TS 可以再属性前增加属性的修饰符
         * public 修饰的属性可以再任意位置访问(修改)默认值
         * private 私有属性,私有属性只能在类内部进行访问和修改
         */
    
        public title: string; // 不加前缀其实就是public
        private name: string;
        private age: number;
    
        // 可以将前缀写在此处
        // constructor(public title: string, private name: string, private age: number) {
    
        constructor(title: string, name: string, age: number) {
            this.title = title;
            this.name = name;
            this.age = age;
        }
    
        // 可用get set方式来实现getName和setName
        getName() {
            return this.name
        }
    
        get _name() {
            return this.name
        }
    
        setName(val: string) {
            this.name = val
        }
    
        set _name(val: string) {
            this.name = val
        }
    
        getAge() {
            return this.age
        }
    
        setAge(val: number) {
            if (val >= 0) {
                this.age = val
            }
        }
    }
    
    const per = new Person('Front-end Developer', 'Alan', 11)
    console.log(per, 'per');
    console.log(per.getName(), 'per.getName()');
    console.log(per._name, 'per._name');
    per.setName('Faye')
    console.log(per.getName(), 'per.getName()');
    per._name = 'Alan Faye';
    console.log(per._name, 'per._name');
    per.setAge(-30);
    console.log(per.getAge(), 'per.getAge()')
    per.setAge(18);
    console.log(per.getAge(), 'per.getAge()')
  • 相关阅读:
    jedis scan实现keys功能
    java简单实现一个阻塞式线程池
    Swift运算符
    数组的使用(1)
    Linux 常用命令
    Task02:基础查询与排序
    Task01:初识数据库
    摩尔投票法
    面向对象暑期课程总结
    xpath+requests+peewee——CSDN论坛全方位爬虫
  • 原文地址:https://www.cnblogs.com/ningxin/p/15109525.html
Copyright © 2011-2022 走看看