简体   繁体   English

一个类作为角度打字稿中的接口

[英]A class as an interface in angular typescript

In a component he uses something like: 在组件中,他使用类似以下内容的方法:

Method (Obj: MyClass) { .... }

It's a shorthand for Obj = new MyClass ? 这是Obj = new MyClass的简写吗?

And if in the constructor of MyClass there is an argument needed, this should be Obj = new MyClass (argument) ? 并且如果在MyClass的构造函数中需要一个参数,则应为Obj = new MyClass (argument) and the Obj: MyClass , still works? Obj: MyClass ,仍然有效吗?

It's a shorthand for Obj = new MyClass ? 这是Obj = new MyClass的简写吗?

No, it is not. 不它不是。 It only specifies that Method receives an argument ( Obj reference inside the method) of type MyClass . 它仅指定Method接收类型为MyClass的参数(方法内的Obj引用)。 It means the argument passed to Method must be an instance of MyClass . 这意味着传递给Method的参数必须是MyClass的实例。

Example of correct usage of Method : 正确使用Method示例:

const ob = new MyClass(); // supose `MyClass` constructor does NOT requieres any argument
Method(ob);

Example of incorrect usage of Method : 错误使用Method示例:

Method();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM