如何在类型脚本中进行类型转换

时间:2018-02-13 18:04:23

标签: typescript

我正在尝试找到类型转换在Typescript中的工作方式。我尝试使用下面的代码无效。可以取悦我所缺少的任何帮助。

// Code goes here

class DemoA { // A class declaration.

  // Implicitly null parametrized void type functions are declared.
  show1() {
    document.write("showA() function in class A");
  }
  show2() {
    document.write("after implicit/explicit casting this function cannot be called!");
  }
}
class DemoB { // Another class declaration.
  showB() {}
}

// An object creation of DemoA class type.
var objA: DemoA = new DemoA();

/* A function of class DemoA is being called through an object reference. NOTE: this function is being called before implicitly/explicitly cast occurs. */
objA.show1();

/* An object creation of DemoB class type. NOTE: this object is being created as “any” type. */
var objB: any = new DemoB;

/* An explicit cast from 'any' DemoB object type to 'DemoA' object type. NOTE: here a < > syntax is being used which indicates an explicit casting. */
objA = < DemoA > objB;

/* Another function of class DemoA is being trying to called through an object reference. NOTE: this function is being called after implicitly/explicitly cast has occurred. This function will not be called/invoked. */
objA.show2();

0 个答案:

没有答案