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

fumotvh3  于 2023-02-14  发布在  其他
关注(0)|答案(6)|浏览(135)

我如何在Dart中调用一个超级构造函数?有可能调用命名的超级构造函数吗?

kqqjbcuj

kqqjbcuj1#

是的,它是,语法接近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);  

  Baz.named(int c, int d) : super.named(c, d);  
}

如果你想初始化子类中的示例变量,super()调用必须在初始化器列表的最后。

class CBar extends Foo {
  int c;

  CBar(int a, int b, int cParam) :
    c = cParam,
    super(a, b);
}

您可以在/r/dartlang上阅读此super()拜访指南背后的动机。

b4lqfgs4

b4lqfgs42#

这是我和你分享的一个文件,按原样运行它。你将学习如何调用超级构造函数,以及如何调用超级参数化构造函数。

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

ozxc1zmp3#

具有可选参数的构造函数的大小写

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

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

fivyi3re4#

我可以调用超类的私有构造函数吗?

可以,但前提是你创建的超类和子类在同一个库中。(因为私有标识符在定义它们的整个库中都可见)私有标识符是以下划线开头的。

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

0wi1tuuw5#

由于dart支持将一个类实现为接口(隐式接口),如果你实现了它,你就不能调用父构造函数,你应该使用extends。如果你使用了implements,把它改为extends,并使用Eduardo Copat的解决方案。

u0sqgete

u0sqgete6#

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

相关问题