5.抽象类.ts 613 B

1234567891011121314151617181920212223242526
  1. (function() {
  2. /**
  3. * 抽象类 与其他类差别不大 abstract
  4. * 抽象类不是为了实例化对象
  5. * 它是因继承而产生的类
  6. */
  7. abstract class Animal {
  8. name: string;
  9. constructor(name: string) {
  10. this.name = name;
  11. }
  12. // 抽象类中 方法没有具体内容 只有方法体
  13. // 具体方法 需要在继承的子类中重写
  14. abstract eat():void;
  15. }
  16. class A extends Animal {
  17. eat() {
  18. console.log(this.name + "吃了很多饭");
  19. }
  20. }
  21. let a = new A("狗狗");
  22. a.eat();
  23. })()