1. //使用class关键字来定义一个类
  2. /**
  3. * 对象中主要包含了两个部分
  4. * 属性
  5. * 方法
  6. *
  7. */
  8. class Person {
  9. /**
  10. * 直接定义的属性是实例属性,需要通过对象的实例去访问
  11. * const per = new Person()
  12. * per.name
  13. * 使用static开头的属性是静态属性(类属性)可以直接通过类去访问
  14. * Person.age
  15. */
  16. //定义实例属性
  17. name: string = 'zz'
  18. //在属性前使用static关键字可以定义类属性(静态属性)
  19. static age: number = 18
  20. //readonly开头的属性表示一个只读的属性无法修改
  21. readonly gender: string = '男'
  22. //静态只读属性
  23. static readonly hobby: string = 'bick'
  24. //定义方法
  25. /**
  26. * 如果方法以static开头则方法就是类方法,可以直接通过类去调用
  27. */
  28. static sayHallo() {
  29. console.log('hallo')
  30. }
  31. }
  32. const per = new Person()
  33. console.log(per.name)
  34. console.log(Person.age)
  35. Person.sayHallo()

构造函数和this

  1. class Dog{
  2. name
  3. age
  4. //构造函数,会在对象创建时调用
  5. constructor(name:string,age:number){
  6. //在实例方法中,this就表示当前的实例
  7. //在构造函数中当前对象就是当前新建的那个对象
  8. //可以通过this向新建的对象中添加属性
  9. this.name = name,
  10. this.age = age
  11. console.log('构造函数')
  12. }
  13. bark(){
  14. //在方法中可以通过this来表示当前调用方法的对象
  15. console.log(this)
  16. console.log('wangwangwang!')
  17. }
  18. }
  19. const dog = new Dog('小黑',3)
  20. const dog2 = new Dog('小白',4)
  21. console.log(dog)
  22. console.log(dog2)

继承(extends)

  1. "use strict";
  2. (function () {
  3. class Animal {
  4. constructor(name, age) {
  5. this.name = name;
  6. this.age = age;
  7. }
  8. say() {
  9. console.log('动物叫!');
  10. }
  11. }
  12. /**
  13. * Dog extends Animal
  14. * 此时Animal被称为父类,Dog被称为子类
  15. * 使用继承后,子类将会拥有父类所有的方法和属性
  16. * 通过继承可以将多个类中共有的代码写在一个父类中
  17. * 这样只需要写一次即可让所有的子类都同时拥有父类中的属性和方法
  18. * 如果希望在子类中添加一些父类中没有的属性或方法直接加就行
  19. * 如果在子类中添加了和父类相同的方法,则子类方法会覆盖掉父类的方法
  20. * 这种子类覆盖掉父类方法的形式我们称之为 重写
  21. */
  22. //定义一个类
  23. //使Dog类继承 Animal类
  24. class Dog extends Animal {
  25. run(){
  26. console.log(`${this.name}在跑`)
  27. }
  28. say(){
  29. console.log('汪')
  30. }
  31. }
  32. //定义一个类
  33. class Cat extends Animal {
  34. say(){
  35. console.log('喵')
  36. }
  37. }
  38. const dog = new Dog('旺财', 3);
  39. const cat = new Cat('咪咪', 9);
  40. console.log(dog)
  41. console.log(cat);
  42. dog.say()
  43. cat.say()
  44. dog.run()
  45. })()

super

  1. (function(){
  2. class Animal{
  3. name:string
  4. constructor(name:string){
  5. this.name = name
  6. }
  7. say(){
  8. console.log('动物叫!')
  9. }
  10. }
  11. class Dog extends Animal{
  12. age:number
  13. constructor(name:string,age:number){
  14. //如果在子类中写了构造函数,在子类的构造函数中必须要对父类的够赞函数进行调用
  15. super(name) //调用父类的构造函数
  16. this.age=age
  17. }
  18. say(){
  19. //在类的方法中super就表示当前类的父类
  20. super.say()
  21. console.log('999')
  22. }
  23. }
  24. const dog = new Dog('旺财',3)
  25. dog.say()
  26. })()

抽象类(sbstract)

以abstract开头的类是一个抽象类
抽象类和其他类区别不大,只是不能用来创建对象
抽象类就是专门被用来继承的类
抽象类中可以添加抽象方法
抽象方法使用abstract开头,没有方法体
抽象方法只能定义在抽象类,子类必须对抽象方法进行重写

  1. (function(){
  2. /**
  3. * 以abstract开头的类是一个抽象类
  4. * 抽象类和其他类区别不大,只是不能用来创建对象
  5. * 抽象类就是专门被用来继承的类
  6. *
  7. * 抽象类中可以添加抽象方法
  8. */
  9. abstract class Animal{
  10. name:string
  11. constructor(name:string){
  12. this.name = name
  13. }
  14. //定义一个抽象方法
  15. //抽象方法使用abstract开头,没有方法体
  16. //抽象方法只能定义在抽象类,子类必须对抽象方法进行重写
  17. abstract say():void
  18. }
  19. class Dog extends Animal{
  20. say(){
  21. }
  22. }
  23. const dog = new Dog('旺财')
  24. dog.say()
  25. })()

接口(interface,implements)

接口来定义一个类结构,用来定义一个类中应该包含那些属性和方法
同时接口也可以当成类声明去使用
接口可以定义类的时候去限制类的结构
接口中所有的属性都不能有实际的值
接口定义对象的结构,而不考虑实际值
在接口中所有的方法都是抽象方法
定义类时,可以使类去实现一个接口
实现接口就是使类满足接口的需求

  1. (function(){
  2. //描述一个对象的类型
  3. type myType ={
  4. name:string,
  5. age:number
  6. }
  7. /**
  8. * 接口来定义一个类结构,用来定义一个类中应该包含那些属性和方法
  9. * 同时接口也可以当成类声明去使用
  10. */
  11. interface myInterface{
  12. name:string,
  13. age:number
  14. }
  15. interface myInterface{
  16. gender:string
  17. }
  18. const obj:myInterface ={
  19. name:'123',
  20. age:123,
  21. gender:'男'
  22. }
  23. /**
  24. * 接口可以定义类的时候去限制类的结构
  25. * 接口中所有的属性都不能有实际的值
  26. * 接口定义对象的结构,而不考虑实际值
  27. * 在接口中所有的方法都是抽象方法
  28. */
  29. interface myInter{
  30. name:string
  31. say():void
  32. }
  33. /**
  34. * 定义类时,可以使类去实现一个接口
  35. * 实现接口就是使类满足接口的需求
  36. */
  37. class myClass implements myInter{
  38. name:string
  39. constructor(name:string){
  40. this.name = name
  41. }
  42. say(){
  43. console.log('hello')
  44. }
  45. }
  46. })()

属性的封装(public,private,protected)

  1. (function () {
  2. class Person {
  3. //TS可以在属性前添加属性的修饰符
  4. /**
  5. * public 修饰的属性可以在任意位置访问(修改)是默认值
  6. * private 私有属性,私有属性只能在类内部进行修改
  7. * 通过在类中添加方法使得私有属性可以在外部被访问
  8. * protected 受保护的属性,只能在当前类的子类中访问(修改)
  9. */
  10. private name: string
  11. private age: number
  12. constructor(name: string, age: number) {
  13. this.name = name
  14. this.age = age
  15. }
  16. /**
  17. * getter方法用来读取属性
  18. * setter方法用来设置属性
  19. * 他们称为属性的存取器
  20. *
  21. */
  22. getName() {
  23. return this.name
  24. }
  25. setName(value: string) {
  26. this.name = value
  27. }
  28. getAge() {
  29. return this.age
  30. }
  31. setAge(value: number) {
  32. if (value >= 0) {
  33. this.age = value
  34. }
  35. }
  36. //ts中设置getter方法的方式
  37. get _name(){
  38. console.log('get name()!!!!')
  39. return this.name
  40. }
  41. set _name(value: string){
  42. this.name = value
  43. }
  44. }
  45. const pre = new Person('zz', 13)
  46. /**
  47. * 现在属性是在对象中设置的,属性可以任意的被修改
  48. * 属性可以任意被修改将会导致对象中的数据变得非常不安全
  49. */
  50. //console.log(pre.name)//报错
  51. pre.setName('xx')
  52. pre.setAge(-10)
  53. pre._name = 'yy'
  54. console.log(pre._name)
  55. class A{
  56. protected num:number
  57. constructor(num:number){
  58. this.num = num
  59. }
  60. }
  61. class B extends A{
  62. test(){
  63. console.log(this.num)
  64. }
  65. }
  66. const b = new B(213)
  67. //console.log(b.num) //报错
  68. // class C{
  69. // name:string
  70. // age:number
  71. // constructor(name:string,age:number){
  72. // this.name = name
  73. // this.age = age
  74. // }
  75. // }
  76. class C{
  77. //可以直接将属性定义在构造函数中
  78. constructor(public name:string,public age:number){
  79. }
  80. }
  81. const c = new C('xx',12)
  82. })()