一、在ES5中类定义和继承
1、定义类
function Peson(name,age){
this.name = name;
this.age = age;
}
Person.prototype.display = function(){
console.log(this.name + '\t'+ this.age)
}
var p = new Person();
2、静态方法
Person.disp = function(){
console.log('我是静态方法')
}
3、继承
function Man(name,age){
Person.call(this,[name,age]);
this.sex = 1;
}
Man.prototype = Person.prototype ;
var man = new Man('zzz',12);
man.display();
二、在TS中类定义和继承
1、类定义
class Animal {
//定义属性
public name:string;
// 构造函数
constructor(name:string){
this.name = name;
}
// 方法
display():void{
console.log(this.name);
}
}
var animal = new Animal("cat");
2、继承
class Cat extends Animal {
constructor(name:string){
super(name);
}
}
3、类的修饰符
public /protected/private
public 在当前类、子类、类外部都可以访问
protected 在当前类、子类可以访问,类外部不能访问
private 在当前类可以访问,子类和类外部不能访问
如果不写是公有的属性
4、静态属性和静态方法
class Dog extends Animal {
private age:number;
// 静态属性
private static count:number = 1;
constructor(name:string,age:number){
super(name);
this.age = age;
}
// 静态方法
static print():void {
console.log("print" + Dog.count)
}
}
Dog.print();
5、多态
class Animal {
private name:string;
constructor(name:string){
this.name = name;
}
public eat():void{
console.log("eat");
}
}
// 具体的子类实现eat方法
class Dog extends Animal {
constructor(name:string){
super(name);
}
public eat() : void {
}
}
6、抽象类、抽象方法
abstract class Animal {
private name:string;
constructor(name:string){
this.name = name;
}
public abstract eat():void;
}
class Dog extends Animal {
constructor(name:string){
super(name);
}
public eat():void{
}
}
一、在ES5中类定义和继承
1、定义类
function Peson(name,age){
this.name = name;
this.age = age;
}
Person.prototype.display = function(){
console.log(this.name + '\t'+ this.age)
}
var p = new Person();
2、静态方法
Person.disp = function(){
console.log('我是静态方法')
}
3、继承
function Man(name,age){
Person.call(this,[name,age]);
this.sex = 1;
}
Man.prototype = Person.prototype ;
var man = new Man('zzz',12);
man.display();
二、在TS中类定义和继承
1、类定义
class Animal {
//定义属性
public name:string;
// 构造函数
constructor(name:string){
this.name = name;
}
// 方法
display():void{
console.log(this.name);
}
}
var animal = new Animal("cat");
2、继承
class Cat extends Animal {
constructor(name:string){
super(name);
}
}
3、类的修饰符
public /protected/private
public 在当前类、子类、类外部都可以访问
protected 在当前类、子类可以访问,类外部不能访问
private 在当前类可以访问,子类和类外部不能访问
如果不写是公有的属性
4、静态属性和静态方法
class Dog extends Animal {
private age:number;
// 静态属性
private static count:number = 1;
constructor(name:string,age:number){
super(name);
this.age = age;
}
// 静态方法
static print():void {
console.log("print" + Dog.count)
}
}
Dog.print();
5、多态
class Animal {
private name:string;
constructor(name:string){
this.name = name;
}
public eat():void{
console.log("eat");
}
}
// 具体的子类实现eat方法
class Dog extends Animal {
constructor(name:string){
super(name);
}
public eat() : void {
}
}
6、抽象类、抽象方法
abstract class Animal {
private name:string;
constructor(name:string){
this.name = name;
}
public abstract eat():void;
}
class Dog extends Animal {
constructor(name:string){
super(name);
}
public eat():void{
}
}
|
请发表评论