TypeScript 支持所有面向对象的功能,例如类、接口等。
class class_name {
// 类作用域
}
定义类的关键字是class,后面是类名。 该类可以包含以下模块(该类的数据成员):
创建一个 Person 类:
class Person {
}
var Person = /** @class */ (function () {
function Person() {
}
return Person;
}());
在下面的示例中,我们声明类Car,包括数组引擎,构造函数在类实例化后初始化数组引擎。
this关键字表示当前类实例化的对象。 注意构造函数的参数名与数组名相同,this.engine代表类的数组。
另外,我们还在类中定义了一个方法disp()。
class Car {
// 字段
engine:string;
// 构造函数
constructor(engine:string) {
this.engine = engine
}
// 方法
disp():void {
console.log("发动机为 : "+this.engine)
}
}
编译上面的代码,得到下面的JavaScript代码:
var Car = /** @class */ (function () {
// 构造函数
function Car(engine) {
this.engine = engine;
}
// 方法
Car.prototype.disp = function () {
console.log("发动机为 : " + this.engine);
};
return Car;
}());
var object_name = new class_name([ arguments ])
实例化类时会调用构造函数,例如:
var obj = new Car("Engine 1")
类中的数组属性和方法可以使用 . 象征:
// 访问属性
obj.field_name
// 访问方法
obj.function_name()
以下示例创建一个 Car 类,然后使用关键字 new 创建一个对象并访问其属性和方法:
class Car {
// 字段
engine:string;
// 构造函数
constructor(engine:string) {
this.engine = engine
}
// 方法
disp():void {
console.log("函数中显示发动机型号 : "+this.engine)
}
}
// 创建一个对象
var obj = new Car("XXSY1")
// 访问字段
console.log("读取发动机型号 : "+obj.engine)
// 访问方法
obj.disp()
编译上面的代码,得到下面的JavaScript代码:
var Car = /** @class */ (function () {
// 构造函数
function Car(engine) {
this.engine = engine;
}
// 方法
Car.prototype.disp = function () {
console.log("函数中显示发动机型号 : " + this.engine);
};
return Car;
}());
// 创建一个对象
var obj = new Car("XXSY1");
// 访问字段
console.log("读取发动机型号 : " + obj.engine);
// 访问方法
obj.disp();
输出是:
读取发动机型号 : XXSY1
函数中显示发动机型号 : XXSY1
TypeScript支持继承类,即我们在创建类时可以继承现有的类。 这个现有的类称为父类,继承它的类称为基类。
类继承使用关键字extends。 子类不但不能继承父类的私有成员(方法和属性)和构造函数,其他的都可以继承。
TypeScript 一次只能继承一个类,不支持多个类的继承。 但是,TypeScript 支持多重继承(A 继承 B,B 继承 C)。
语法格式如下:
class child_class_name extends parent_class_name
类继承:实例中创建Shape类,Circle类继承Shape类。 Circle类可以直接使用Area属性:
class Shape {
Area:number
constructor(a:number) {
this.Area = a
}
}
class Circle extends Shape {
disp():void {
console.log("圆的面积: "+this.Area)
}
}
var obj = new Circle(223);
obj.disp()
编译上面的代码,得到下面的JavaScript代码:
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var Shape = /** @class */ (function () {
function Shape(a) {
this.Area = a;
}
return Shape;
}());
var Circle = /** @class */ (function (_super) {
__extends(Circle, _super);
function Circle() {
return _super !== null && _super.apply(this, arguments) || this;
}
Circle.prototype.disp = function () {
console.log("圆的面积: " + this.Area);
};
return Circle;
}(Shape));
var obj = new Circle(223);
obj.disp();
输出是:
圆的面积: 223
需要注意的是,一个基类只能继承一个父类。 TypeScript 不支持继承多个类,但支持多重继承,如下例所示:
class Root {
str:string;
}
class Child extends Root {}
class Leaf extends Child {} // 多重继承,继承了 Child 和 Root 类
var obj = new Leaf();
obj.str ="hello"
console.log(obj.str)
编译上面的代码,得到下面的JavaScript代码:
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var Root = /** @class */ (function () {
function Root() {
}
return Root;
}());
var Child = /** @class */ (function (_super) {
__extends(Child, _super);
function Child() {
return _super !== null && _super.apply(this, arguments) || this;
}
return Child;
}(Root));
var Leaf = /** @class */ (function (_super) {
__extends(Leaf, _super);
function Leaf() {
return _super !== null && _super.apply(this, arguments) || this;
}
return Leaf;
}(Child)); // 多重继承,继承了 Child 和 Root 类
var obj = new Leaf();
obj.str = "hello";
console.log(obj.str);
输出是:
hello
类继承后typescript调用ios方法,子类可以重新定义父类的方法。 这个过程称为方法重绘。
super关键字是对父类的直接引用,可以引用父类的属性和技能。
class PrinterClass {
doPrint():void {
console.log("父类的 doPrint() 方法。")
}
}
class StringPrinter extends PrinterClass {
doPrint():void {
super.doPrint() // 调用父类的函数
console.log("子类的 doPrint()方法。")
}
}
编译上面的代码,得到下面的JavaScript代码:
var obj = new StringPrinter()
obj.doPrint()
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var PrinterClass = /** @class */ (function () {
function PrinterClass() {
}
PrinterClass.prototype.doPrint = function () {
console.log("父类的 doPrint() 方法。");
};
return PrinterClass;
}());
var StringPrinter = /** @class */ (function (_super) {
__extends(StringPrinter, _super);
function StringPrinter() {
return _super !== null && _super.apply(this, arguments) || this;
}
StringPrinter.prototype.doPrint = function () {
_super.prototype.doPrint.call(this); // 调用父类的函数
console.log("子类的 doPrint()方法。");
};
return StringPrinter;
}(PrinterClass));
var obj = new StringPrinter();
obj.doPrint();
输出是:
父类的 doPrint() 方法。
子类的 doPrint()方法。
static 关键字用于将类的数据成员(属性和技术)定义为静态。 静态成员可以通过类名直接调用。
class StaticMem {
static num:number;
static disp():void {
console.log("num 值为 "+ StaticMem.num)
}
}
StaticMem.num = 12 // 初始化静态变量
StaticMem.disp() // 调用静态方法
编译上面的代码,得到下面的JavaScript代码:
var StaticMem = /** @class */ (function () {
function StaticMem() {
}
StaticMem.disp = function () {
console.log("num 值为 " + StaticMem.num);
};
return StaticMem;
}());
StaticMem.num = 12; // 初始化静态变量
StaticMem.disp(); // 调用静态方法
输出是:
num 值为 12
instanceof 运算符用于确定对象是否属于指定类型。 如果是,则返回 truetypescript调用ios方法,否则返回 false。
class Person{ }
var obj = new Person()
var isPerson = obj instanceof Person;
console.log("obj 对象是 Person 类实例化来的吗? " + isPerson);
编译上面的代码,得到下面的JavaScript代码:
var Person = /** @class */ (function () {
function Person() {
}
return Person;
}());
var obj = new Person();
var isPerson = obj instanceof Person;
console.log(" obj 对象是 Person 类实例化来的吗? " + isPerson);
输出是:
obj 对象是 Person 类实例化来的吗? true
在 TypeScript 中,您可以使用访问控制来保护对类、变量、方法和构造函数的访问。 TypeScript 支持 3 种不同的访问权限。
以下示例定义了两个变量 str1 和 str2。 str1 是公共的,str2 是私有的。 实例化后,可以访问str1。 如果访问str2,就会出现编译错误。
class Encapsulate {
str1:string = "hello"
private str2:string = "world"
}
var obj = new Encapsulate()
console.log(obj.str1) // 可访问
console.log(obj.str2) // 编译错误, str2 是私有的
类可以使用关键字implements 来实现套接字,并使用兴趣字段作为类的属性。
以下示例使用实现 ILoan 接口的 AgriLoan 类:
interface ILoan {
interest:number
}
class AgriLoan implements ILoan {
interest:number
rebate:number
constructor(interest:number,rebate:number) {
this.interest = interest
this.rebate = rebate
}
}
var obj = new AgriLoan(10,1)
console.log("利润为 : "+obj.interest+",抽成为 : "+obj.rebate )
编译上面的代码,得到下面的JavaScript代码:
var AgriLoan = /** @class */ (function () {
function AgriLoan(interest, rebate) {
this.interest = interest;
this.rebate = rebate;
}
return AgriLoan;
}());
var obj = new AgriLoan(10, 1);
console.log("利润为 : " + obj.interest + ",抽成为 : " + obj.rebate);
输出是:
利润为 : 10,抽成为 : 1