简体   繁体   English

如何在 Dart 中调用超级构造函数?

[英]How do I call a super constructor in Dart?

How do I call a super constructor in Dart?如何在 Dart 中调用超级构造函数? Is it possible to call named super constructors?是否可以调用命名的超级构造函数?

Yes, it is, the syntax is close to C# , here is an example with both default constructor and named constructor:是的,它的语法接近C# ,这里是一个同时包含默认构造函数和命名构造函数的示例:

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.如果要在子类中初始化实例变量,则super()调用必须位于初始化列表的最后。

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 .您可以在/r/dartlang上阅读此super()调用指南背后的动机。

This is a file I am sharing with you, run it as 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");
    var dog2 = Dog("Pug", "Brown");
    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");
    }
}

case of a constructor with Optional Parameters带有可选参数的构造函数的情况

class Foo {
  String a;
  int b;
  Foo({this.a, this.b});
}

class Bar extends Foo {
  Bar({a,b}) : super(a:a, b:b);
}

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);
}

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 .由于 dart 支持将类实现为接口( 隐式接口),如果你实现了它就不能调用父构造函数,你应该使用extends If you use implements change it to extends and use Eduardo Copat's Solution.如果您使用工具,请将其更改为扩展并使用 Eduardo Copat 的解决方案。

class AppException implements Exception {
  final _message;
  final _prefix;

  //constructor with optional & not named paramters
  //if not the parameter is not sent, it'll be null
  AppException([this._message, this._prefix]);

  String toString() {
    return "$_prefix$_message";
  }
}

class FetchDataException extends AppException {
  //redirecting constructor with a colon to call parent two optional parameters
  FetchDataException([String msg]) : super(msg, "Error During Communication: ");
}

class BadRequestException extends AppException {
  BadRequestException([msg]) : super(msg, "Invalid Request: ");
}

class UnauthorisedException extends AppException {
  UnauthorisedException([msg]) : super(msg, "Unauthorised: ");
}

class InvalidInputException extends AppException {
  InvalidInputException([String msg]) : super(msg, "Invalid Input: ");
}

How do I call a super constructor in Dart?如何在 Dart 中调用超级构造函数? Is it possible to call named super constructors?是否可以调用命名的超级构造函数?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM