标签:div 自动 作用 实例化 相同 public string 不同 use
class Dog { constructor(name: string) { this.name = name; } name: string; run() {} } console.log(Dog.prototype); // Dog { run: [Function] } let dog = new Dog(‘Bob‘); console.log(dog); // Dog { name: ‘Bob‘ } // name 属性只在实例上,不在原型上;run 方法只在原型上,不在实例上
class Animal { constructor(name: string) { this.name = name; } name: string; } class Dog extends Animal { constructor(name: string, leg: number) { super(name); this.leg = leg; } leg: number; intro() { console.log(`My name is ${this.name}, I hava ${this.leg} legs.`); } } let bob: Dog = new Dog(‘Bob‘, 4); bob.intro(); // My name is Bob, I hava 4 legs.
class Animal { constructor(name: string) { this.name = name; } // 默认是public,可省略 public name: string; public say () { console.log(‘hello... ...‘) } } class Dog extends Animal { constructor(name: string) { super(name); } } let bob: Dog = new Dog(‘Bob‘); bob.say(); // hello... ...
class Animal { constructor(name: string) { this.name = name; } name: string; private say () { console.log(‘hello... ...‘) } } class Dog extends Animal { constructor(name: string) { super(name); } } // private 不能用于实例 let jorge: Animal = new Animal(‘Jorge‘); jorge.say(); // Property ‘say‘ is private and only accessible within class ‘Animal‘. // private 不能用于子类 let bob: Dog = new Dog(‘Bob‘); bob.say(); // Property ‘say‘ is private and only accessible within class ‘Animal‘.
class Animal { private constructor(name: string) { this.name = name; } name: string; } // Cannot extend a class ‘Animal‘. Class constructor is marked as private. class Dog extends Animal { constructor(name: string) { super(name); } } let a: Animal = new Animal(‘aaa‘); // Constructor of class ‘Animal‘ is private and only accessible within the class declaration
class Animal { constructor(name: string) { this.name = name; } name: string; protected say() { console.log(`hello ${this.name}`) } } class Dog extends Animal { constructor(name: string) { super(name); this.say(); } } let d: Dog = new Dog(‘ddd‘); // hello ddd let a: Animal = new Animal(‘aaa‘); a.say(); // Property ‘say‘ is protected and only accessible within class ‘Animal‘ and its subclasses.
class Animal { protected constructor(name: string) { this.name = name; } name: string; } class Dog extends Animal { constructor(name: string) { super(name); } } let d: Dog = new Dog(‘ddd‘); let a: Animal = new Animal(‘aaa‘); // Constructor of class ‘Animal‘ is protected and only accessible within the class declaration.
class Dog { constructor(name: string) { this.name = name; } readonly name: string; readonly leg: number = 4; } let d: Dog = new Dog(‘Bob‘); d.name = ‘Carl‘; // Cannot assign to ‘name‘ because it is a read-only property. d.leg = 2; // Cannot assign to ‘leg‘ because it is a read-only property.
class People { constructor(name: string) { this.name = name; } name: string; static legs: number = 2; } class Student extends People { constructor(name: string) { super(name); } } console.log(People.legs); // 2 console.log(Student.legs); // 2
class People { constructor( public name: string) { this.name = name; } // name: string; // 标识符“name”重复 }
class Dog { constructor(name: string) { this.name = name; } name: string; getName() { console.log("name: ", this.name); } } let d: Dog = new Dog(‘Bob‘); d.name = ‘Carl‘; d.getName(); // name: Carl
class Dog { constructor() {} private _name: string; get name(): string { return this._name; } set name(name: string) { if(name.length > 10) { console.log(‘Error: the name is too long!‘) } else { this._name = name; } } } let d: Dog = new Dog(); d.name = ‘hello world!‘; // Error: the name is too long! console.log(d.name); // undefined d.name = ‘Bob‘; console.log(d.name); // Bob
标签:div 自动 作用 实例化 相同 public string 不同 use
原文地址:https://www.cnblogs.com/rogerwu/p/12193236.html