Typescript 学习笔记五:类
目录:
- Typescript 学习笔记一:介绍、安装、编译
- Typescript 学习笔记二:数据类型
- Typescript 学习笔记三:函数
- Typescript 学习笔记四:回忆Es5 中的类
- Typescript 学习笔记五:类
- Typescript 学习笔记六:接口
- Typescript 学习笔记七:泛型
类的定义
- ES5 中定义:
function Person (name) {
this.name=name;
this.run = function () {
console.log(this.name)
}
}
var p = new Person('张三');
p.run();
- ts 中定义:
使用class关键词。
class Person {
name:string; // 属性,前面省略了 public 关键词
constructor (name:string) { // 构造函数,实例化类的时候触发的方法
this.name = name;
}
run ():void {
console.log(`${this.name}在运动`);
}
getName ():string {
return this.name;
}
setName (name:string):void {
this.name = name;
}
}
let p = new Person('李四');
p.run();
p.setName('王五');
console.log(p.getName());
继承
使用关键词extends、super。
// 父类
class Person {
name:string; // 属性,前面省略了 public 关键词
constructor (name:string) { // 构造函数,实例化类的时候触发的方法
this.name = name;
}
run ():void {
console.log(`${this.name}在运动`);
}
}
let p = new Person('李四');
p.run(); // 李四在运动
// 子类继承父类
class Child extends Person {
constructor (name:string) {
super(name); // 初始化父类的构造函数
}
}
let c = new Child('周六');
c.run(); // 周六在运动
父类的方法和子类的方法一致,子类会执行子类自己的方法
// 父类
class Person {
name:string; // 属性,前面省略了 public 关键词
constructor (name:string) { // 构造函数,实例化类的时候触发的方法
this.name = name;
}
run ():void {
console.log(`${this.name}在运动`);
}
}
let p = new Person('李四');
p.run(); // 李四在运动
// 子类继承父类
class Child extends Person {
constructor (name:string) {
super(name); // 初始化父类的构造函数
}
run ():void {
console.log(`${this.name}在运动--子类`);
}
work ():void {
console.log(`${this.name}工作--子类`);
}
}
let c = new Child('周六');
c.run(); // 周六在运动--子类
c.work(); // 周六在工作--子类
类里面的修饰符
Typescript 里面定义属性的时候给我们提供了三种修饰符:
public:公有,在当前类里面、子类、类外面都可以访问
protected:保护类型,在当前类里面、子类里面可以访问,在类外部没法访问
private:私有,在当前类里面可以访问,子类、类外部都没法访问
属性如果不加修饰符,默认就是公有(public)public:公有,在当前类里面、子类、类外面都可以访问
class Person {
public name:string; // 公有
constructor (name:string) {
this.name = name;
}
run ():void {
console.log(`${this.name}在运动`); // 在类里能访问
}
}
let p = new Person('李四');
p.run();
console.log(p.name); // 在类外面能访问
class Child extends Person {
constructor (name:string) {
super(name);
}
run ():void {
console.log(`${this.name}在运动--子类`); // 子类能访问
}
}
let c = new Child('周六');
c.run(); // 周六在运动--子类
console.log(c.name); // 在类外面能访问
- protected:保护类型,在当前类里面、子类里面可以访问,在类外部没法访问
class Person {
protected name:string; // 保护
constructor (name:string) {
this.name = name;
}
run ():void {
console.log(`${this.name}在运动`); // 在类里能访问
}
}
let p = new Person('李四');
p.run();
// console.log(p.name); // 报错,在类外面不能访问
class Child extends Person {
constructor (name:string) {
super(name);
}
run ():void {
console.log(`${this.name}在运动--子类`); // 子类能访问
}
}
let c = new Child('周六');
c.run(); // 周六在运动--子类
// console.log(c.name); // 报错,在类外面能访问
- private:私有,在当前类里面可以访问,子类、类外部都没法访问
class Person {
private name:string; // 私有
constructor (name:string) {
this.name = name;
}
run ():void {
console.log(`${this.name}在运动`); // 在类里能访问
}
}
let p = new Person('李四');
p.run();
// console.log(p.name); // 报错,在类外面不能访问
class Child extends Person {
constructor (name:string) {
super(name);
}
run ():void {
// console.log(`${this.name}在运动--子类`); // 报错,子类能访问
}
}
let c = new Child('周六');
c.run(); // 周六在运动--子类
// console.log(c.name); // 报错,在类外面能访问
静态属性 静态方法
- ES5
function Person (name) {
this.name = name;
}
Person.age = 24; // 静态属性
Person.run = function () { // 静态方法
console.log(Person.age);
}
Person.run(); // 静态方法的调用
- jquery
$('#box').css('color', 'red'); // 实例方法
$.get('url', function () { // 静态方法
})
$(element) { // 实例
return new Base(element);
}
$.get = function (url, callback) { // 静态方法
}
function Base (element) {
this.element = element;
this.css = function (attr, value) {
this.element.style[attr] = value;
}
}
- ts
class Person {
public name:string; // 公有
public age:number = 25;
static sex:string = 'man'; // 静态属性
constructor (name:string) {
this.name = name;
}
public run ():void { // 公有方法
console.log(`${this.name}在运动`); // 在类里能访问
}
// 静态方法
static print ():void {
console.log(`静态方法,性别:${Person.sex}`);
}
}
// 静态属性和方法的调用
console.log(Person.sex);
Person.print(); // 静态方法,性别:man
多态
多态:父类定义一个方法不去实现,让继承它的子类去实现,每一个子类有不同的表现。
多态属于继承。
class Animal {
name:string;
constructor (name:string) {
this.name = name;
}
eat () { // 具体吃什么,不知道。具体吃什么,由继承它的子类去实现,每一个子类的表现不一样
console.log('吃的方法');
}
}
class Dog extends Animal {
constructor (name:string) {
super(name);
}
eat () { // 子类实现父类的 eat 方法
console.log(`${this.name}喜欢吃骨头`);
}
}
class Cat extends Animal {
constructor (name:string) {
super(name);
}
eat () { // 子类实现父类的 eat 方法
console.log(`${this.name}喜欢吃老鼠`);
}
}
抽象类
- Typescript 中的抽象类:它是提供其他类继承的基类,不能直接被实例化。
- 用
abstract关键字定义抽象类和抽象方法,抽象类中的抽象方法不包含具体实现并且必须在派生类(子类)中实现。 - abstract 抽象方法只能放在抽象类里面。
- 抽象类和抽象方法用来定义标准。比如:标准:Animal 这个类要求它的子类必须包含eat 方法。
// 抽象类,标准
abstract class Animal {
name:string;
constructor (name:string) {
this.name = name;
}
abstract eat ():any; // 抽象方法不包含具体实现并且必须在派生类中实现。
}
// let animal = new Animal(); // 错误,抽奖类不能被实例化
class Dog extends Animal {
constructor (name:string) {
super(name);
}
eat () { // 抽象类的子类必须实现抽象类里面的抽象方法
console.log(`${this.name}喜欢吃骨头`);
}
}
let dog = new Dog('小黑');
dog.eat();
Typescript 学习笔记五:类的更多相关文章
- Typescript 学习笔记四:回忆ES5 中的类
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
- Typescript 学习笔记七:泛型
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
- Typescript 学习笔记六:接口
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
- Typescript 学习笔记二:数据类型
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
- Typescript 学习笔记三:函数
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
- Typescript 学习笔记一:介绍、安装、编译
前言 整理了一下 Typescript 的学习笔记,方便后期遗忘某个知识点的时候,快速回忆. 为了避免凌乱,用 gitbook 结合 marketdown 整理的. github地址是:ts-gitb ...
- (转)Qt Model/View 学习笔记 (五)——View 类
Qt Model/View 学习笔记 (五) View 类 概念 在model/view架构中,view从model中获得数据项然后显示给用户.数据显示的方式不必与model提供的表示方式相同,可以与 ...
- C#可扩展编程之MEF学习笔记(五):MEF高级进阶
好久没有写博客了,今天抽空继续写MEF系列的文章.有园友提出这种系列的文章要做个目录,看起来方便,所以就抽空做了一个,放到每篇文章的最后. 前面四篇讲了MEF的基础知识,学完了前四篇,MEF中比较常用 ...
- java之jvm学习笔记五(实践写自己的类装载器)
java之jvm学习笔记五(实践写自己的类装载器) 课程源码:http://download.csdn.net/detail/yfqnihao/4866501 前面第三和第四节我们一直在强调一句话,类 ...
随机推荐
- API接口认证
restful API接口可以很方便的让其他系统调用,为了让指定的用户/系统可以调用开放的接口,一般需要对接口做认证; 接口认证有两种方式: 1.认证的token当做post/get的参数,serve ...
- 详细分析MySQL事务日志(redo log和undo log) 表明了为何mysql不会丢数据
innodb事务日志包括redo log和undo log.redo log是重做日志,提供前滚操作,undo log是回滚日志,提供回滚操作. undo log不是redo log的逆向过程,其实它 ...
- numpy.asmatrix的用法
学习的过程中,遇到了asmatrix的用法,看了一下官方文档,明白了. numpy.asmatrix numpy.asmatrix(data, dtype=None)[source] Interpre ...
- CF 225C Barcode(DP)
传送门:点我 You've got an n × m pixel picture. Each pixel can be white or black. Your task is to change t ...
- 254. Factor Combinations 返回所有因数组合
[抄题]: Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write ...
- [leetcode]75. Sort Colors三色排序
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- jquery判断是否是空对象 不含任何属性
code function isEmptyObject(e) { var t; for (t in e) return !1; return !0 }
- win10系统配置jdk环境不能用%JAVA_HOME% 代替目录
发现以前配好的java环境变量和tomcat环境变量全都清空了,在重新配置的时候总是出现问题,即在cmd命令窗口下,输入java,显示正常,输入java -version 也是显示正常,唯独输入jav ...
- ABP框架系列之四十二:(Object-To-Object-Mapping-对象映射)
Introduction It's a common to map a similar object to another object. It's also tedious and repeatin ...
- spass按位置编码,进行排序题处理与分析
本范例即需建立Q4_1至Q4_4 等四个变项, 各变量的数值则是排序的内容,共有0.1.2.3.4 等五种可能,0代表该选项没有被受测者选取,1.2.3.4分别代表被受测者指为第一至第四顺位. htt ...