How do I call a super constructor in Dart?

前端 未结 4 1937
难免孤独
难免孤独 2020-12-25 09:35

How do I call a super constructor in Dart? Is it possible to call named super constructors?

4条回答
  •  长情又很酷
    2020-12-25 10:01

    this is file i am sharing with you, run it as it is. you'll learn how to call super constructor, and how to call super parameterized constructor.

    / Objectives
    // 1. Inheritance with Default Constructor and Parameterised Constructor
    // 2. Inheritance with Named Constructor
    
    void main() {
    
        var dog1 = Dog("Labrador", "Black");
    
        print("");
    
        var dog2 = Dog("Pug", "Brown");
    
        print("");
    
        var dog3 = Dog.myNamedConstructor("German Shepherd", "Black-Brown");
    }
    
    class Animal {
    
        String color;
    
        Animal(String color) {
            this.color = color;
            print("Animal class constructor");
        }
    
        Animal.myAnimalNamedConstrctor(String color) {
            print("Animal class named constructor");
        }
    }
    
    class Dog extends Animal {
    
        String breed;
    
        Dog(String breed, String color) : super(color) {
            this.breed = breed;
            print("Dog class constructor");
        }
    
        Dog.myNamedConstructor(String breed, String color) : super.myAnimalNamedConstrctor(color) {
            this.breed = breed;
            print("Dog class Named Constructor");
        }
    }
    

提交回复
热议问题