简体   繁体   English

Typescript中的类和接口

[英]classes and Interfaces in Typescript

I am in the beginning of learning TypeScript. 我正在学习TypeScript。 I come from a strongly-typed language (c#) and have some knowledge in JS. 我来自强类型语言(c#),并且在JS中有一些知识。

Quite at the beginning I fell over the following example: 在一开始我摔倒了下面的例子:

class Student {
    fullName: string;
    constructor(public firstName, public middleInitial, public lastName) {
        this.fullName = firstName + " " + middleInitial + " " + lastName;
    }
}

interface Person {
    firstName: string;
    lastName: string;
}

function greeter(person : Person) {
    return "Hello, " + person.firstName + " " + person.lastName;
}

var user = new Student("Jane", "M.", "User");

document.body.innerHTML = greeter(user);

Now I am a bit confused. 现在我有点困惑。 I would expect something like Student:Person (c#) or Student implements Person (Java) So: Why does greeter() accept an object of class " Student "? 我希望像Student:Person (c#)或Student implements Person (Java)那样:为什么greeter()接受类“ Student ”的对象? I did not see any clue that " Student " implements " Person ". 我没有看到“ Student ”实施“ Person ”的任何线索。

is it just about the property names? 这只是属性名称吗? So if I add another class 所以,如果我添加另一个类

class Teacher {
  salaray:int,
  firstName:string,
  lastName:string
}

an object of that class would also be a valid parameter for greeter() ? 该类的对象也是greeter()的有效参数?

Yes, you're correct, the compiler checks whether or not the object that is passed to greeter satisfies the Person interface, and since the Student class contains all of the needed properties then the compiler is happy about it. 是的,你是对的,编译器检查传递给greeter的对象是否满足Person接口,并且由于Student类包含所有需要的属性,编译器对此感到高兴。

And yes, you can do this: 是的,你可以这样做:

class Teacher {
    salaray: number;
    firstName:string;
    lastName:string;
}

document.body.innerHTML = greeter(new Teacher());

( code in playground ) 游乐场代码

Yes you are right about your assumption. 是的,你对自己的假设是正确的。 You can see the code that typescript transpiles into. 您可以看到打字稿转化为的代码。 The code that you posted (I changed the last line to an alert) looks like this 您发布的代码(我将最后一行更改为警报)如下所示

var Student = (function () {
    function Student(firstName, middleInitial, lastName) {
        this.firstName = firstName;
        this.middleInitial = middleInitial;
        this.lastName = lastName;
        this.fullName = firstName + " " + middleInitial + " " + lastName;
    }
    return Student;
}());

function greeter(person) {
    return "Hello, " + person.firstName + " " + person.lastName;
}

var user = new Student("Jane", "M.", "User");
alert(greeter(user));

Once the typescript is transpiled to javascript, all that knowledge of those interfaces and classes is lost. 将typescript转换为javascript后,对这些接口和类的所有知识都将丢失。 You can see that from the transpiled example above. 您可以从上面的转换示例中看到。 In this example, it makes sense that it would run correctly because the incoming variable person actually does have the properties that the method is accessing. 在这个例子中,它可以正确运行是有道理的,因为传入的变量person确实具有该方法正在访问的属性。 So Typescript can see that the transpiled version of your code actually does make sense and will not complain about it. 因此,Typescript可以看到代码的转换版本确实有意义并且不会抱怨它。

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

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