Typescript-Class __typescript

Source: Internet
Author: User
Inheritance of classes: object-oriented patterns
Super: Keywords
Class Animal  {
  name:string;
  Constructor (thename:string) {
    this.name = thename;
  }
  Move (Distanceinmeters:number = 0) {
    console.log (' ${this.name} moved ${distanceinmeters}m. ');
  }

Class  Snack extends Animal {

  Constructor (name:string) {
    super (name);//Call constructor of parent class
  }
  // Overrides the parent class's method move
  (Distanceinmeters = 5) {
    console.log ("slithering ...");
    Super.move (distanceinmeters);
  }

Class Horse extends Animal {

  Constructor (name:string) {
    super (name);
  }

  Move (Distanceinmeters =) {
    Console.log ("galloping");
    Super.move (distanceinmeters);
  }

Let Sam = new Snack ("Sammy the Python");
Let Tom:animal = new Horse ("Tommy the Palomino");

Sam.move ();
Tom.move ();
Modifiers: (in Typescript, members are default to public)
Public private: can only be accessed in the life of his class protect: readonly: Read-only properties must be initialized in a declaration or in a constructor in a derived class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.