![]() ![]() I would totally understand declining abstract properties if there were no abstract methods but when you already have the concept of abstract things I can't understand why not all methods can be abstract. But that doesn't mean that it's the clearest way to express yourself. Its true that abstract classes cannot be instantiated, but since they are classes they surely allow creation of constructors. But you can manage just fine without abstract methods as well by having an abstract class implement an interface. We could probably think of some strange edge case where some library expect a good getter but I would say no. Or is there a pattern in TypeScript that you just can't achieve without abstract properties? #ABSTRACT PROPERTY CANNOT BE ACCESSED IN THE CONSTRUCTOR CODE#Mixing Java style getters with good getters makes the code more confusing. A property should never have a side effect, methods might. Mostly, however I would argue that it's about intention as well. Just as abstract properties forces subclasses to implement them. Whilst I was trying to produce this I did think at one point that abstract setter or getter would again be useful, in this example because the setter does something each time with the DataTO value, but as you can see generics are the way of course abstract methods are valuable, they force subclasses to implement them. I guessed at what you're trying to achieve, again correct me if I'm wrong, but I think a better pattern for this would be the following. Var c = new QuoteDataEvent(new TradeTO()) Ĭonsole.log(c.data.quoteNum()) // run time error ![]() Return this._data as QuoteTO // avoid casting if possible Before a constructor runs, the instance variables for the class are set by default. You can declare methods and properties as abstract, that is, a method or property. In particular this means you cannot pass an object property to a method which. Return this._data as TradeTO // avoid casting if possible Access Control and the Declaration of Protected Properties and Methods. And to initialize the child class' objects, constructor is to be called. Constructors are allowed because of the concept of inheritance. * todo: make abstract when typescript supports abstract getters Abstract classes cannot be instantiated because abstract methods have no body, so they will make the program to stop at run-time if an object calls any of such methods. The same applies to getType with the only difference that here it is implemented as a method which is called in an inherited getter type, but it realy seems that it would be much more preferable to just use an abstract get type(). In the example above I need all DataEvents ( TradeDataEvent, QuoteDataEvent) to be forced to strictly specify the return type of data field. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |