You can specify the property on the interface, but you can't enforce whether getters and setters are used, like this:
interface IExample {
Name: string;
}
class Example implements IExample {
private _name: string = "Bob";
public get Name() {
return this._name;
}
public set Name(value) {
this._name = value;
}
}
var example = new Example();
alert(example.Name);
In this example, the interface doesn't force the class to use getters and setters, I could have used a property instead (example below) - but the interface is supposed to hide these implementation details anyway as it is a promise to the calling code about what it can call.
interface IExample {
Name: string;
}
class Example implements IExample {
// this satisfies the interface just the same
public Name: string = "Bob";
}
var example = new Example();
alert(example.Name);
And lastly, =>
is not allowed for class methods - you could start a discussion on Codeplex if you think there is a burning use case for it. Here is an example:
class Test {
// Yes
getName = () => 'Steve';
// No
getName() => 'Steve';
// No
get name() => 'Steve';
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…