TypeScript----类
一、类的属性
public: 公有,
private: 私有,不能在声明它的类的外部访问,只能在类内部访问
protect: 保护,不能在声明它的类的外部访问,但继承者除外
readonly 只读属性,必须在声明时或构造函数里被初始化
static静态属性,无需实例化就可以访问静态成员
super()方法,它会执行基类的构造方法
class Grid {
static origin = {x: , y: };
calculateDistanceFromOrigin(point: {x: number; y: number;}) {
let xDist = (point.x - Grid.origin.x);
let yDist = (point.y - Grid.origin.y);
return Math.sqrt(xDist * xDist + yDist * yDist) / this.scale;
}
constructor (public scale: number) { }
} let grid1 = new Grid(1.0); // 1x scale
let grid2 = new Grid(5.0); // 5x scale console.log(grid1.calculateDistanceFromOrigin({x: , y: }));
console.log(grid2.calculateDistanceFromOrigin({x: , y: }));
二、类的存取,set() 和 get()
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) {
alert(employee.fullName);
}
三、类的继承 extends
class Animal {
name:string;
constructor(theName: string) { this.name = theName; }
move(distanceInMeters: number = ) {
console.log(`${this.name} moved ${distanceInMeters}m.`);
}
} class Snake extends Animal {
constructor(name: string) { super(name); }
move(distanceInMeters = ) {
console.log("Slithering...");
super.move(distanceInMeters);
}
} class Horse extends Animal {
constructor(name: string) { super(name); }
move(distanceInMeters = ) {
console.log("Galloping...");
super.move(distanceInMeters);
}
} let sam = new Snake("Sammy the Python");
let tom: Animal = new Horse("Tommy the Palomino"); sam.move();
tom.move();
四、抽象类:abstract,派生类的基类使用。它们一般不会直接被实例化。
abstract class Department{
constructor(public name:string){}
printName():void{
console.log('Department name:'+this.name);
}
abstract printMeeting():void;//抽象类中的抽象方法不包含具体实现并且必须在派生类中实现。
}
class AcountingDepartment extends Department{
constructor(){
super('Accounting and Auditing');
}
printMeeting():void{
console.log('The Accounting Department meets each Monday at 10am.');
}
generateReports():void{
console.log('Generating accounting reports...');
}
}
let department:Department;
department=new Department() //error
let department=new AcountingDepartment();
department.printName();
department.printMeeting();
//department.generateReports();
TypeScript----类的更多相关文章
- 从C#到TypeScript - 类
总目录 从C#到TypeScript - 类型 从C#到TypeScript - 高级类型 从C#到TypeScript - 变量 从C#到TypeScript - 接口 从C#到TypeScript ...
- .NET手撸绘制TypeScript类图——上篇
.NET手撸绘制TypeScript类图--上篇 近年来随着交互界面的精细化,TypeScript越来越流行,前端的设计也越来复杂,而类图正是用简单的箭头和方块,反映对象与对象之间关系/依赖的好方式. ...
- .NET手撸绘制TypeScript类图——下篇
.NET手撸绘制TypeScript类图--下篇 在上篇的文章中,我们介绍了如何使用.NET解析TypeScript,这篇将介绍如何使用代码将类图渲染出来. 注:以防有人错过了,上篇链接如下:http ...
- TypeScript:类(Classes)
返回TypeScript手册总目录 传统的Javascript关注的是函数(function)和基于原型(prototype-based)的继承作为构建可重复使用组件的基本方式,但是与更舒服地使用面向 ...
- typescript类的修饰符
学习过java的小姐姐,小哥哥应该很好理解,但还是啰嗦的写出来! typescript里面定义属性的时候给我们提供了 三种修饰符 public :公有 在当前类里面. 子类 .类外面都可以访问 pro ...
- typescript类(学习笔记非干货)
我们声明一个 Greeter类.这个类有3个成员:一个叫做greeting的属性,一个构造函数和一个greet方法. We declare a Greeter class. This class ha ...
- typescript类与继承
/* 1.vscode配置自动编译 1.第一步 tsc --inti 生成tsconfig.json 改 "outDir": "./js", 2.第二步 任务 ...
- typescript类 继承 修饰符
//1.ts中类的定义 /* es5: function Person(name){ this.name=name; this.run=function(){ console.log(this.nam ...
- typescript 类(类的定义、继承、修饰符、抽象类)
代码: // 本节内容 // 1.类的定义 // 2.类的继承 // 3.访问修饰符 // 4.静态属性和静态方法 // 5.抽象类和多态 // js // function Person(name) ...
- TypeScript入门四:TypeScript的类(class)
TypeScript类的基本使用(修饰符) TypeScript类的抽象类(abstract) TypeScript类的高级技巧 一.TypeScript类的基本使用(修饰符) TypeScript的 ...
随机推荐
- 快速掌握用python写并行程序
目录 一.大数据时代的现状 二.面对挑战的方法 2.1 并行计算 2.2 改用GPU处理计算密集型程序 3.3 分布式计算 三.用python写并行程序 3.1 进程与线程 3.2 全局解释器锁GIL ...
- 九度oj 1002 Grading 2011年浙江大学计算机及软件工程研究生机试真题
#include<iostream> #include<queue> #include<cstdio> #include<cstring> #inclu ...
- FocusBI:SSAS体系结构(原创)
关注微信公众号:FocusBI 查看更多文章:加QQ群:808774277 获取学习资料和一起探讨问题. SSAS是微软BI组件系列中最核心的组件,商业智能的心脏所有的数据都从这里统一输出,它能把 ...
- 实体类的状态与Hibernate缓存
一.Hibernate中实体类的三种状态 1.瞬时态 该状态下实体类对象的id属性没有值,该对象和session也没有关系. 实例: UserEntity user = new UserEntity( ...
- jmeter(3)——录制
其实自己之前做web功能自动化就接触过录制,不管是火狐浏览器的插件录制,还是QTP的录制,可能刚开始接触你会觉得录制很low,不过,时间长了,有时候录制也是很考验人的,更何况,不管是录制还是脚本,只要 ...
- imx6. android6.0经常修改或者用到的目录(未完)
系统应用apk存放的文件: out/target/product/sabresd_6dq/system/app 系统配置存放目录:(我自己的,也许不对) out/target/product/sabr ...
- android添加系统(服务、应用)
1. 添加系统服务 1.1 添加方式1:(不加入servicemanager统一管理的) 看Android6.0.1 init.rc解析中的第2章和第3章 方式1: 1). 写一个测试脚本test.s ...
- 【极客学院-idea教程】
极客学院idea教程: http://whudoc.qiniudn.com/2016/IntelliJ-IDEA-Tutorial/index.html
- HTTP Error 502.5 - Process Failure asp.net core error in IIS
在windows server 2012 上安装完dotnet-win-x64.1.1.1.exe 和 DotNetCore.1.0.4_1.1.1-WindowsHosting.exe后,没有重启服 ...
- 基于bootstrap的内容折叠功能
加入js及css支持: <link rel="stylesheet" href="css/bootstrap.min.css"/> <scri ...