How do I call a super constructor in Dart?

前端 未结 4 1924
难免孤独
难免孤独 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 09:42

    Can I call a private constructor of the superclass?

    Yes, but only if the superclass and the subclass you are creating are in the same library. (Since private identifiers are visible across the whole library they are defined in). Private identifiers are those that start with underscore.

    class Foo {    
      Foo._private(int a, int b) {
        //Code of private named constructor
      }
    }
    
    class Bar extends Foo {
      Bar(int a, int b) : super._private(a,b);
    }
    
    0 讨论(0)
  • 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");
        }
    }
    
    0 讨论(0)
  • 2020-12-25 10:08

    Yes, it is, the syntax is close to C#, here is an example with both default constructor and named constructor:

    class Foo {
      Foo(int a, int b) {
        //Code of constructor
      }
    
      Foo.named(int c, int d) {
        //Code of named constructor
      }
    }
    
    class Bar extends Foo {
      Bar(int a, int b) : super(a, b);
    }
    
    class Baz extends Foo {
      Baz(int c, int d) : super.named(c, d);  
    }
    

    If you want to initialize instance variables in the subclass, the super() call must be last in an initializer list.

    class CBar extends Foo {
      int c;
    
      CBar(int a, int b, int cParam) :
        c = cParam,
        super(a, b);
    }
    

    You can read about the motivation behind this super() call guideline on /r/dartlang.

    0 讨论(0)
  • 2020-12-25 10:09

    As dart supports implementing a class as interface (Implicit interfaces), you can't call the parent constructor if you implemented it you should use extends. If you use implements change it to extends and use Eduardo Copat's Solution.

    0 讨论(0)
提交回复
热议问题