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

Inheritance 如何在Dart中调用超级构造函数?,inheritance,constructor,dart,superclass,extends,Inheritance,Constructor,Dart,Superclass,Extends,如何在Dart中调用超级构造函数?可以调用命名的超级构造函数吗?是的,语法接近,下面是一个同时使用默认构造函数和命名构造函数的示例: class-Foo{ Foo(内部a、内部b){ //建造商代码 } Foo.named(int c,int d){ //命名构造函数的代码 } } 类栏扩展了Foo{ 酒吧(INTA,INTB):超级(a,b); } 类Baz扩展了Foo{ Baz(intc,intd):超级命名(c,d); } 如果要初始化子类中的实例变量,则super()调用必须是初始值设

如何在Dart中调用超级构造函数?可以调用命名的超级构造函数吗?

是的,语法接近,下面是一个同时使用默认构造函数和命名构造函数的示例:

class-Foo{
Foo(内部a、内部b){
//建造商代码
}
Foo.named(int c,int d){
//命名构造函数的代码
}
}
类栏扩展了Foo{
酒吧(INTA,INTB):超级(a,b);
}
类Baz扩展了Foo{
Baz(intc,intd):超级命名(c,d);
}
如果要初始化子类中的实例变量,则
super()
调用必须是初始值设定项列表中的最后一个

类CBar扩展了Foo{ INTC; CBar(内部a、内部b、内部cParam): c=cParam, 超级(a,b); } 您可以阅读此
super()
调用指南背后的动机。

我可以调用超类的私有构造函数吗? 是的,但前提是您正在创建的超类和子类位于同一个库中。(因为私有标识符在整个库中都是可见的,所以在中定义了它们)。私有标识符是以下划线开头的标识符

class Foo{
Foo._private(内部a、内部b){
//私有命名构造函数的代码
}
}
类栏扩展了Foo{
酒吧(内部a、内部b):超级私人(a、b);
}

由于dart支持将类实现为接口(),如果实现了它,则不能调用父构造函数。您应该使用扩展。
如果您使用实现将其更改为扩展并使用Eduardo Copat的解决方案。

这是我与您共享的文件,请按原样运行。您将学习如何调用超级构造函数,以及如何调用超级参数化构造函数

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

具有可选参数的构造函数的情况

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

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

在条形图的构造函数中,“a”和“b”参数是“动态”类型。您错过了静态类型语言的优点。
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: ");
}
class Animal {
  String? color = null;
  Animal() {}
  void eat() {
    print('Eat!');
  }
}

class Dog extends Animal {
  String? breed = null;
  Dog(String breed, String color){
    this.breed = breed;
    super.color = color;
  }
  void bark() {
    print('Bark!!!');
  }
}