定义类

class Person {
name: string; //属性
constructor(_name: string) {
this.name = _name;
} //构造函数
sayHello(): string {
return "Hi,everyone"
} //方法
} let firstOne = new Person("Fred") //实例化类

继承:继承使用关键字extends,调用父类使用super,子类继承父类的属性和方法,并且子类可以改写父类的属性和方法

class Animal {
name: string;
constructor(_name: string) {
this.name = _name;
}
skinColour(color: string = "black"): void {
console.log(`${this.name} skin colour is ${color}`)
}
} class Horse extends Animal {
constructor(name: string) { super(name) }
skinColour(color: string = "brown"): void {
console.log(`I'am ${this.name}`);
super.skinColour("brown");
}
} let horse = new Horse("horse");
horse.skinColour()
// I'am horse
// horse skin colour is brown

public、private、protected、readonly

  • public(不声明默认都为public,也可以显示的设置为public)

    class Person {
    public name: string; //属性
    public constructor(_name: string) {
    this.name = _name;
    } //构造函数
    public sayHello(): string {
    return "Hi,everyone"
    } //方法
    } let firstOne = new Person("Fred") //实例化类
  • private(private的成员不能被外部访问;比较带有privateprotected成员的类型时,两个类型兼容的条件是private或protected的成员必须相同切来至同一个声明(同一个类))
    class Person {
    private name: string;
    public constructor(_name: string) {
    this.name = _name;
    }
    } class Employee {
    private name: string;
    public constructor(_name: string) {
    this.name = _name;
    }
    } let firstOne = new Person("Fred")
    console.log(firstOne.name) //error: Property 'name' is private;
    let lastOne = new Employee("Fred")
    firstOne = lastOne // error: Type 'Employee' is not assignable to type 'Person'.Types have separate declarations of a private property 'name'.
  • protected(protected和private相似,但protected成员可以在派生类中访问(能被继承,但不能在实例中访问,若构造函数是protected,则不能被实例化,只能被继承))
    class Person {
    protected name: string;
    protected constructor(_name: string) {
    this.name = _name;
    }
    } class Employee extends Person {
    private department: string;
    public constructor(name: string,department:string) {
    super(name);
    this.department = department;
    }
    } let Bob = new Person; //error: Constructor of class 'Person' is protected
    let fred = new Employee("fred","test");
    console.log(fred.name) //error: Property 'name' is protected
  • readonly(设置属性为只读,必须在声明时或构造函数里初始化)
    class Person {
    readonly name: string;
    constructor(_name: string) {
    this.name = _name;
    }
    } let fred = new Person("fred");
    fred.name = "Bob" //error: Cannot assign to 'name' because it is a constant or a read-only property.

    参数属性(参数属性通过给构造函数参数添加一个访问限定符来声明(public,private,protected),把声明和赋值合并至一处)

    class Person {
    constructor(private name: string) { }
    sayHello(): void {
    console.log(`my name is ${this.name}`)
    }
    } let fred = new Person("fred");
    fred.sayHello() //my name is fred

    存取器(get、set   只带有 get不带有set的存取器自动被推断为readonly

    let passcode = "secret passcode";
    
    class Employee {
    private _fullName: string; get fullName(): string {
    return this._fullName;
    } set fullName(newName: string) {
    if (passcode && passcode == "secret passcode") {
    this._fullName = newName;
    }
    else {
    console.log("Error: Unauthorized update of employee!");
    }
    }
    } let employee = new Employee();
    employee.fullName = "Bob Smith";
    if (employee.fullName) {
    console.log(employee.fullName);
    }

    静态属性(static,不能被实例访问,在类里面访问时,需要加上类名)

    class Person {
    static height:number = ;
    constructor(private name: string) { }
    sayHello(): void {
    console.log(`my name is ${this.name}, I height is ${Person.height}`)
    }
    } let fred = new Person("fred");
    fred.sayHello() //my name is fred, I height is 180

    抽象类(abstract,抽象类做为其它派生类的基类使用。 它们一般不会直接被实例化。抽象类中的抽象方法不包含具体实现并且必须在派生类中实现)

    abstract class Person {
    constructor(public name: string) { }
    abstract sayHello():void;
    } class Empoloy extends Person{
    constructor(){
    super("Fred")
    }
    sayHello(){
    console.log(`my name is ${this.name}`)
    }
    } let firstOne = new Empoloy();
    firstOne.sayHello(); //my name is Fred

ts中类的继承的更多相关文章

  1. python中类的继承

    python中类的继承 在python中面向对象编程中实现继承,以下面一个实例进行说明. class SchoolMenber(): # __init__类似于c++中的构造函数 # __init__ ...

  2. Java中类的继承,属性和方法的四种修饰符的作用范围,final关键字,java的三大特点中的2个:封装和多态,以及多态的一个设计模式,模板方法模式(template method)

    (一)Java中的继承: 关于继承,在Java中类的继承只能是单继承,不像C+++那样灵活,可以多继承,多继承的后果就是各种关系乱套,就相当于一个孩子有2个母亲一样,社会关系的复杂,不利于程序后期的开 ...

  3. typescript中类的继承

    typescript中类的继承用到的是:extends和super 先看一下typescript中类的写法: class Demo{ //类的属性 name:string; age:number; / ...

  4. Java中类的继承深入剖析

    在Java开发中,我们常常用到继承这一概念,可以说继承是Java这类面向对象编程语言的基石.正是有了继承这个概念,使得我们可以创建分等级层次的类.今天小编就和大家一起来深入聊聊Java语言的继承. 在 ...

  5. 《挑战30天C++入门极限》图例实解:C++中类的继承特性

        图例实解:C++中类的继承特性 整个c++程序设计全面围绕面向对象的方式进行,类的继承特性是c++的一个非常非常重要的机制,继承特性可以使一个新类获得其父类的操作和数据结构,程序员只需在新类中 ...

  6. PHP中类的继承与方法重写

    php中类的继承与方法重写,欢迎大神补充指点! <?php namespace _1009; class Demo5 { //实例属性 public $product; public $pric ...

  7. Python中类的继承代码实例

    Python中类的继承代码实例 这篇文章主要介绍了Python中类的继承代码实例,本文直接给出代码及运行效果,需要的朋友可以参考下 相对于C 的继承编写,Python更简洁,而且效率也是很高的,下面编 ...

  8. 第7.6节 Python中类的继承机制详述

    在本章第一节,介绍了面向对象程序设计的三个特征:封装.继承和多态,前面章节重点介绍了封装和多态,由于Python语言是多态语言,对象的类型不再由继承等方式决定,而由实际运行时所表现出的具体行为来决定, ...

  9. Typescript 中类的继承

    Typescript中类的定义与继承与后端开发语言java/C#等非常像,实现起来非常方便,而且代码便于阅读. 用Typescript写较大项目时是非常有优势的. /** * BaseClass */ ...

随机推荐

  1. 如何将一个已有的项目托管到github或是码云上?git的配置

    场景一:已有的一个项目,要把它托管到Git上去,步骤和方法如下: 方法一: ①在工程的路径下 : git init 建一个裸仓库. ②远程仓库地址 :将本地的仓库和远程仓库关联 git remote ...

  2. EMC VNX5200/5400存储 新增LUN与Hosts映射操作

    EMC VNX5200/5400 1.创建RAID  Groups 1.1        进入EMC VNX5200/5400主界面,依次选择Storage——Storage Pools——RAID ...

  3. Java RMI之HelloWorld经典入门案例

    Java RMI 指的是远程方法调用 (Remote Method Invocation).它是一种机制,能够让在某个 Java 虚拟机上的对象调用另一个 Java 虚拟机中的对象上的方法.可以用此方 ...

  4. 崂山白花蛇草水 权值线段树套KDtree

    Description 神犇Aleph在SDOI Round2前立了一个flag:如果进了省队,就现场直播喝崂山白花蛇草水.凭借着神犇Aleph的实 力,他轻松地进了山东省省队,现在便是他履行诺言的时 ...

  5. eas之事件

    选择事件 选择事件是在选择块发生变化后发出.table.addKDTSelectListener(new KDTSelectListener(){    public void tableSelect ...

  6. 【剑指Offer】45、扑克牌顺子

      题目描述:   LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到 ...

  7. 15.5.2 【Task实现细节】骨架方法的结构

    尽管骨架方法中的代码非常简单,但它暗示了状态机的职责.代码清单15-11生成的骨架方 法如下所示: [DebuggerStepThrough] [AsyncStateMachine(typeof(De ...

  8. nyoj51-管闲事的小明

    管闲事的小明 时间限制:4000 ms  |  内存限制:65535 KB 难度:2 描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端 ...

  9. Linux思维导图之计划任务

    查漏补缺,理解概念,及时总结,互相交流,欢迎拍砖. 用yum install -y vixie-cron这个命令进行安装计划任务服务,可以在安装之前使用crontab -e进行检测一下,服务器是否安装 ...

  10. Linux—Ubuntu14.0.5设置MySQL的字符集

    1.mysql配置文件地址 cd /etc/mysql/my.cnf 2.在[mysqld]在下方添加以下代码 [mysqld] init_connect='SET collation_connect ...