ts学习05-typeScript中的类

类的定义

class Person {
  name: string; //属性  前面省略了public关键词
  constructor(n: string) {
    //构造函数   实例化类的时候触发的方法
    this.name = n;
  }

  run(): void {
    console.log(this.name);
  }
}
var p = new Person("张三");

p.run();

在这里插入图片描述

class Person {
  name: string;

  //构造函数   实例化类的时候触发的方法
  constructor(name: string) {
    this.name = name;
  }

  getName(): string {
    return this.name;
  }

  setName(name: string): void {
    this.name = name;
  }
}
var p = new Person("张三");
console.log(p.getName());
p.setName("李四");
console.log(p.getName());

在这里插入图片描述

继承

class Person {
  name: string;

  constructor(name: string) {
    this.name = name;
  }

  run(): string {
    return `${this.name}在运动`;
  }
}
var p=new Person('王五');
console.log(p.run())

class Web extends Person {
  constructor(name: string) {
    super(name); /*初始化父类的构造函数*/
  }
}

var w = new Web("李四");
console.log(w.run());

在这里插入图片描述

ts中继承的探讨 父类的方法和子类的方法一致

class Person {
  name: string;
  constructor(name: string) {
    this.name = name;
  }

  run(): string {
    return `${this.name}在运动`;
  }
}
// var p=new Person('王五');
// alert(p.run())

class Web extends Person {
  constructor(name: string) {
    super(name); /*初始化父类的构造函数*/
  }
  run(): string {
    return `${this.name}在运动-子类`;
  }
  work() {
    alert(`${this.name}在工作`);
  }
}

var w = new Web("李四");
// alert(w.run());
// w.work();
console.log(w.run());

在这里插入图片描述

类里面的修饰符

  • public :公有 在当前类里面、 子类 、类外面都可以访问
  • protected:保护类型 在当前类里面、子类里面可以访问 ,在类外部没法访问
  • private :私有 在当前类里面可以访问,子类、类外部都没法访问
> 属性如果不加修饰符 默认就是 公有 (public)

public

在类里面、 子类 、类外面都可以访问

class Person {
  public name: string; /*公有属性*/
  constructor(name: string) {
    this.name = name;
  }

  run(): string {
    //内部访问
    return `${this.name}在运动`;
  }
}
class Student extends Person{
  constructor(name:string){
    super(name)
  }
}
var p = new Person("哈哈哈");
// 内部访问
console.log(p.run());
// 类外部访问公有属性;
console.log(p.name);
var s = new Student('里斯')
// 子类内部访问
console.log(s.run());
// 子类外部访问公有属性;
console.log(s.name);

在这里插入图片描述

protected

在类里面、子类里面可以访问 ,在类外部没法访问

class Person {
  protected name: string; /*公有属性*/
  constructor(name: string) {
    this.name = name;
  }

  run(): string {
    //内部访问
    return `${this.name}在运动`;
  }
}
class Student extends Person {
  constructor(name: string) {
    super(name);
  }
  run(): string {
    //内部访问
    return `${this.name}在运动`;
  }
}
var p = new Person("哈哈哈");
// 内部访问
console.log(p.run());
// 类外部访问公有属性;
console.log(p.name);
var s = new Student("里斯");
// 子类内部访问
console.log(s.run());
// 子类外部访问公有属性;
console.log(s.name);

在这里插入图片描述

private

在类里面可以访问,子类、类外部都没法访问

class Person {
  private name: string; /*公有属性*/
  constructor(name: string) {
    this.name = name;
  }

  run(): string {
    //内部访问
    return `${this.name}在运动`;
  }
}
class Student extends Person {
  constructor(name: string) {
    super(name);
  }
  run(): string {
    //内部访问
    return `${this.name}在运动`;
  }
}
var p = new Person("哈哈哈");
// 内部访问
console.log(p.run());
// 类外部访问公有属性;
console.log(p.name);
var s = new Student("里斯");
// 子类内部访问
console.log(s.run());
// 子类外部访问公有属性;
console.log(s.name);

在这里插入图片描述

静态属性 静态方法

class Per {
  public name: string;
  public age: number = 20;
  //静态属性
  static sex = "男";
  constructor(name: string) {
    this.name = name;
  }
  run() {
    /*实例方法*/

    alert(`${this.name}在运动`);
  }
  work() {
    alert(`${this.name}在工作`);
  }
  static print() {
    /*静态方法  里面没法直接调用类里面的属性*/

    console.log("print方法" + Per.sex);
  }
}

// var p=new Per('张三');

// p.run();

Per.print();

console.log(Per.sex);

在这里插入图片描述

多态

class Animal {
  name: string;
  constructor(name: string) {
    this.name = name;
  }
  eat() {
    //具体吃什么  不知道   ,  具体吃什么?继承它的子类去实现 ,每一个子类的表现不一样
    console.log("吃的方法");
  }
}

class Dog extends Animal {
  constructor(name: string) {
    super(name);
  }
  //覆盖父类方法
  eat() {
    return this.name + "吃粮食";
  }
}

class Cat extends Animal {
  constructor(name: string) {
    super(name);
  }
  //覆盖父类方法
  eat() {
    return this.name + "吃老鼠";
  }
}

let dog = new Dog("小汪");
console.log(dog.eat());

let cat = new Cat("小嗨");
console.log(cat.eat());


在这里插入图片描述

抽象类

typescript中的抽象类:它是提供其他类继承的基类,不能直接被实例化。用abstract关键字定义抽象类和抽象方法,抽象类中的抽象方法不包含具体实现并且必须在派生类中实现。abstract抽象方法只能放在抽象类里面,抽象类和抽象方法用来定义标准 。 标准:Animal 这个类要求它的子类必须包含eat方法。

abstract class Animal {
  public name: string;
  constructor(name: string) {
    this.name = name;
  }
  abstract eat(): any; //抽象方法不包含具体实现并且必须在派生类中实现。

  run() {
    console.log("其他方法可以不实现");
  }
}
// var a=new Animal() /*错误的写法*/
class Dog extends Animal {
  //抽象类的子类必须实现抽象类里面的抽象方法
  constructor(name: any) {
    super(name);
  }
  eat() {
    console.log(this.name + "吃粮食");
  }
}

var d = new Dog("小花花");
d.eat();

class Cat extends Animal {
  //抽象类的子类必须实现抽象类里面的抽象方法
  constructor(name: any) {
    super(name);
  }
  run() {}
  eat() {
    console.log(this.name + "吃老鼠");
  }
}

var c = new Cat("小花猫");
c.eat();

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

假装我不帅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值