class修饰符的使用及区别

public、private、protected、static、abstract

public:可以继承、实例化

class Person {
	public name: string;
	constructor(thename: string) {
		this.name = thename;
	}
}
class Jack extends Person {
	age: number;
	constructor(name: string, age: number) {
		super(name)
		this.age = age;
	}
	say() {
		console.log(`my name is ${this.name}, age ${this.age}`);
	}
}
let p1 = new Person('tom');
console.log(p1.name); // tom
let j1 = new Jack('jacker', 10);
j1.say(); // my name is jacker age 10

  private 私有属性只能在基类中访问,不能在实例、派生类中访问

class Person {
	private name: string;
	constructor(thename: string) {
		this.name = thename;
	}
	sayname() {
		console.log(`my name is ${this.name}`);
	}
}
class Jack extends Person {
	age: number;
	constructor(name: string, age: number) {
		super(name)
		this.age = age;
	}
	say() {
		// 只能在Person中访问
		// console.log(`my name is ${this.name}, age ${this.age}`); // error
	}
}
let p1 = new Person('tom');
p1.sayname(); // tom
// console.log(p1.name); // tom // error 只能在Person中访问
let j1 = new Jack('jacker', 10);
j1.sayname(); // jacker

 

   protected 受保护的,可以被继承,在派生类中可以访问,子类、父类都不能实例访问

 

class Person {
	protected name: string;
	constructor(thename: string) {
		this.name = thename;
	}
	sayname() {
		console.log(`my name is ${this.name}`);
	}
}
class Jack extends Person {
	constructor(name: string) {
		super(name)
	}
	say() {
		// 只能在Person中访问
		console.log(`my name is ${this.name}`);
	}
}
let p1 = new Person('tom');
p1.sayname(); // tom
console.log(p1.name); // tom // error 只能在Person、子类中访问
let j1 = new Jack('jacker');
j1.say(); // jacker
console.log(j1.name); // error 只能在Person、子类中访问

 

  // static 只能通过基类、子类访问,实例不能访问

 

class Person {
	static myName: string;
	constructor(name: string) {
		Person.myName = name;
	}
	sayname() {
		return Person.myName;
	}
}
class Jack extends Person {
	constructor() {
		super('jacker');
	}
}
let p1 = new Person('tom');
p1.myName; // error Person上不存在myName属性
console.log(p1.sayname());// tom
// 在类的外部访问
console.log(Person.myName); // tom
let j1 = new Jack();
// 子类实例访问基类方法
console.log(j1.sayname()); // jacker
j1.myName // error Jack 上不存在myName属性
// 子类访问静态属性
console.log(Jack.myName); // jacker

 

   // abstract 抽象类中的抽象方法不包含具体实现并且必须在派生类中实现

 

abstract class Person {
	sayname() {
		console.log('my name is sayname');
	}
	// 抽象方法不具体实现
	abstract say(): void;
}
class Jack extends Person {
	// 子类必须实现父类抽象方法
	say() {
		console.log('my name is jacker');
	}
}
// let p1 = new Person(); // 抽象类不可以被实例化
let j1 = new Jack();
j1.sayname();
j1.say()

 

  

 

 

 

 

class修饰符的使用及区别

public、private、protected、static、abstract

public:可以继承、实例化

  1.  
    // public可以继承、实例化
  2.  
    class Person {
  3.  
    public name: string;
  4.  
    constructor(thename: string) {
  5.  
    this.name = thename;
  6.  
    }
  7.  
    }
  8.  
    class Jack extends Person {
  9.  
    age: number;
  10.  
    constructor(name: string, age: number) {
  11.  
    super(name)
  12.  
    this.age = age;
  13.  
    }
  14.  
    say() {
  15.  
    console.log(`my name is ${this.name}, age ${this.age}`);
  16.  
    }
  17.  
    }
  18.  
    let p1 = new Person('tom');
  19.  
    console.log(p1.name); // tom
  20.  
    let j1 = new Jack('jacker', 10);
  21.  
    j1.say(); // my name is jacker age 10

private 私有属性只能在基类中访问,不能在实例、派生类中访问

  1.  
    class Person {
  2.  
    private name: string;
  3.  
    constructor(thename: string) {
  4.  
    this.name = thename;
  5.  
    }
  6.  
    sayname() {
  7.  
    console.log(`my name is ${this.name}`);
  8.  
    }
  9.  
    }
  10.  
    class Jack extends Person {
  11.  
    age: number;
  12.  
    constructor(name: string, age: number) {
  13.  
    super(name)
  14.  
    this.age = age;
  15.  
    }
  16.  
    say() {
  17.  
    // 只能在Person中访问
  18.  
    // console.log(`my name is ${this.name}, age ${this.age}`); // error
  19.  
    }
  20.  
    }
  21.  
    let p1 = new Person('tom');
  22.  
    p1.sayname(); // tom
  23.  
    // console.log(p1.name); // tom // error 只能在Person中访问
  24.  
    let j1 = new Jack('jacker', 10);
  25.  
    j1.sayname(); // jacker

  protected 受保护的,可以被继承,在派生类中可以访问,子类、父类都不能实例访问

  1.  
    class Person {
  2.  
    protected name: string;
  3.  
    constructor(thename: string) {
  4.  
    this.name = thename;
  5.  
    }
  6.  
    sayname() {
  7.  
    console.log(`my name is ${this.name}`);
  8.  
    }
  9.  
    }
  10.  
    class Jack extends Person {
  11.  
    constructor(name: string) {
  12.  
    super(name)
  13.  
    }
  14.  
    say() {
  15.  
    // 只能在Person中访问
  16.  
    console.log(`my name is ${this.name}`);
  17.  
    }
  18.  
    }
  19.  
    let p1 = new Person('tom');
  20.  
    p1.sayname(); // tom
  21.  
    console.log(p1.name); // tom // error 只能在Person、子类中访问
  22.  
    let j1 = new Jack('jacker');
  23.  
    j1.say(); // jacker
  24.  
    console.log(j1.name); // error 只能在Person、子类中访问

// static 只能通过基类、子类访问,实例不能访问

  1.  
    class Person {
  2.  
    static myName: string;
  3.  
    constructor(name: string) {
  4.  
    Person.myName = name;
  5.  
    }
  6.  
    sayname() {
  7.  
    return Person.myName;
  8.  
    }
  9.  
    }
  10.  
    class Jack extends Person {
  11.  
    constructor() {
  12.  
    super('jacker');
  13.  
    }
  14.  
    }
  15.  
    let p1 = new Person('tom');
  16.  
    p1.myName; // error Person上不存在myName属性
  17.  
    console.log(p1.sayname());// tom
  18.  
    // 在类的外部访问
  19.  
    console.log(Person.myName); // tom
  20.  
    let j1 = new Jack();
  21.  
    // 子类实例访问基类方法
  22.  
    console.log(j1.sayname()); // jacker
  23.  
    j1.myName // error Jack 上不存在myName属性
  24.  
    // 子类访问静态属性
  25.  
    console.log(Jack.myName); // jacker

 // abstract 抽象类中的抽象方法不包含具体实现并且必须在派生类中实现

  1.  
    abstract class Person {
  2.  
    sayname() {
  3.  
    console.log('my name is sayname');
  4.  
    }
  5.  
    // 抽象方法不具体实现
  6.  
    abstract say(): void;
  7.  
    }
  8.  
    class Jack extends Person {
  9.  
    // 子类必须实现父类抽象方法
  10.  
    say() {
  11.  
    console.log('my name is jacker');
  12.  
    }
  13.  
    }
  14.  
    // let p1 = new Person(); // 抽象类不可以被实例化
  15.  
    let j1 = new Jack();
  16.  
    j1.sayname();
  17.  
    j1.say();
  18.  

posted on 2021-02-04 19:57  ygunoil  阅读(973)  评论(0编辑  收藏  举报