普通的类 class
class Greeter {
greeting: string;
constructor(message: string) {
this.greeting = message;
}
greet() {
return "Hello," + this.greeting;
}
}
let greeter = new Greeter("world")
console.log(greeter)
类继承 externds
class An {
move(num: number = 0) { //默认值
console.log("move" + num)
}
}
class Dog extends An {
bark() {
console.log("ddddd")
}
}
const dog = new Dog()
dog.bark(); //ddddd
dog.move(10); //move10
super 调用父类的构造函数里面的方法
class Animal {
name: string;
constructor(theName: string) { this.name = theName; }
move(distanceInMeters: number = 0) {
console.log(`${this.name} moved ${distanceInMeters}m.`);
}
}
class Snake extends Animal {
constructor(name: string) { super(name); } //super(name) 调用父类的构造函数里面的方法
move(distanceInMeters = 5) {
console.log("Slithering...");
super.move(distanceInMeters); //子类调用父类的函数
}
}
class Horse extends Animal {
constructor(name: string) { super(name); }
move(distanceInMeters = 45) {
console.log("Galloping...");
super.move(distanceInMeters);
}
}
let sam = new Snake("Sammy the Python");
let tom: Animal = new Horse("Tommy the Palomino");
sam.move(); //Slithering... Sammy the Python moved 5m.
tom.move(34); //Galloping... Tommy the Palomino moved 34m.
public 修饰符 默认修饰符
//public 默认修饰符
class AA {
public name: string;
public constructor(theName: string) { this.name = theName };
public move(mm: number) {
console.log("move" + mm)
}
}
private 修饰符 受保护的私有修饰符
//private 受保护的私有修饰符
class BB {
private name: string;
public age: number;
constructor(thsName: string) { this.name = thsName; this.age = 10; }
}
protected 修饰符 和private相似,但在他的派生类中可以访问
//protected 和private相似,但可以在派生类中可以访问
class CC {
protected name: string;
constructor(name: string) { this.name = name }
}
class DD extends CC {
private department: string;
constructor(name: string, department: string) {
super(name); //super this.name = name;
this.department = department;
}
public getElevatorPitch() {
return `Hello, my name is ${this.name} and I work in ${this.department}.`;
}
}
let howard = new DD("Howard", "Sales");
console.log(howard.getElevatorPitch()); //Hello, my name is Howard and I work in Sales.
// console.log(howard.name); // 错误 name不能在CC类外面使用,但是可以在DD内部使用
static 静态属性
//static 静态属性
class ann {
static oo = { x: 10, y: 20 };
gett() {
let x: number = ann.oo.x;
return x;
}
constructor(public ss: number) { }
}
console.log(ann.oo.x) //10 直接获取类的静态属性
let aaa = new ann(12);
console.log(aaa.gett()) //10 通过类的方法获取类的静态属性
abstract 抽象类
抽象类中的抽象函数不会直接定义,需要在继承他的子类中定义,否则会报错
abstract class Department {
constructor(public name: string) {
}
printName(): void {
console.log('Department name: ' + this.name);
}
abstract printMeeting(): void; // 必须在派生类中实现
}
class AccountingDepartment extends Department {
constructor() {
super('Accounting and Auditing'); // 在派生类的构造函数中必须调用 super()
}
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(); // 错误: 不能创建一个抽象类的实例
department = new AccountingDepartment(); // 允许对一个抽象子类进行实例化和赋值
department.printName();
department.printMeeting();
// department.generateReports(); // 错误: 方法在声明的抽象类中不存在
构造函数
class gree {
greeting: string;
constructor(msg: string) {
this.greeting = msg;
}
greet() {
return "123"
}
}
let g: gree;
g = new gree("456")
console.log(g) //gree{greeting:'456'}