修饰符

  1. pubilc,
  2. private
  3. protecte
外部 子类
pubilc 可以 可以访问 可以访问
private 可以 不可以访问 不可以访问
protecte 可以 不可以访问 可以访问
  1. // 修饰符 1、pubilc,private protecte
  2. // 使用private修饰的变量在类外部是不可读取的
  3. // 使用pubilc修饰的变量在内外部都可以读取
  4. class Person{
  5. private name:string ="lisi";
  6. public age:number = 18
  7. sayName(){
  8. console.log(this.name);
  9. }
  10. sayAge(){
  11. console.log(this.age);
  12. }
  13. }
  14. var p:Person = new Person()
  15. console.log(p);
  16. console.log(p.age);
  17. p.age = 30
  18. console.log(p);
  19. p.sayName()
  20. p.sayAge()
  1. class Student{
  2. protected name:string ="lisi"
  3. sayName(){
  4. console.log(this.name);
  5. }
  6. }
  7. // protected 和private的区别 在子类中
  8. // protected是可以访问的
  9. // pricate 是不可以访问的
  10. class MidStudent extends Student{
  11. getName(){
  12. console.log(this.name);
  13. }
  14. }
  15. var m:MidStudent = new MidStudent()
  16. m.getName()

1、static

  1. // static 使用static修饰的变量表示类的共有变量 不用实例化就可以直接使用类名调用
  2. // 简单点在类中声明的变量默认是public
  3. class Http{
  4. /* public */private static baseUrl:string="https://douban.com"
  5. static request(){
  6. console.log("res");
  7. }
  8. }
  9. console.log(Http.baseUrl);
  10. Http.request()
  11. // 静态变量或变量方法时不能通过实例调用
  12. var h:Http =new Http()
  13. Http.baseUrl = "lisi"