简体   繁体   English

Dart中inheritance的正确方式

[英]Proper way of inheritance in Dart

I have two parent classes:我有两个父类:

class Animal {
  final String name;
  final Home home;

  Animal(this.name, this.home);
}

class Home {
  final String location;

  Home(this.location);
}

And their corresponding child classes:以及它们对应的子类:

class Human extends Animal {
  Human(String name, Home home) : super(name, home);

  getDetails() {
    print("$name\n");
    print("${home.getLocation()}\n");
  }
}

class House extends Home {
  House(String location) : super(location);

  String getLocation() {
    return "North side of $location";
  }
}

As you can see the child class of Home , House contains a method getLocation .如您所见, Home的子 class , House包含一个方法getLocation Also the child class of Animal , Human has a method getDetails which needs to call the getLocation method.还有Animal的子 class , Human有一个方法getDetails需要调用getLocation方法。 But it says The method 'getLocation' isn't defined for the type 'Home'.但它说没有为“Home”类型定义“getLocation”方法。

This is just an example made up by me trying to understand a real usecase of entity-model where model is a child of entity which contains additional methods like toJson / fromJson .这只是我试图理解实体模型的真实用例而编写的一个示例,其中model实体的子对象,其中包含其他方法,例如toJson / fromJson I am unable to call second child class's(in this case House) toJson(like the getLocation method in this case) method from the first child class (in this case Human).我无法从第一个子 class(在本例中为 Human)调用第二个子类(在本例中为 House)toJson(如本例中的 getLocation 方法)方法。

I have two parent classes:我有两个父类:

class Animal {
  final String name;
  final Home home;

  Animal(this.name, this.home);
}

class Home {
  final String location;

  Home(this.location);
}

And their corresponding child classes:以及它们对应的子类:

class Human extends Animal {
  Human(String name, Home home) : super(name, home);

  getDetails() {
    print("$name\n");
    print("${home.getLocation()}\n");
  }
}

class House extends Home {
  House(String location) : super(location);

  String getLocation() {
    return "North side of $location";
  }
}

As you can see the child class of Home , House contains a method getLocation .如您所见, Home的子 class , House包含一个方法getLocation Also the child class of Animal , Human has a method getDetails which needs to call the getLocation method.还有Animal的子 class , Human有一个方法getDetails需要调用getLocation方法。 But it says The method 'getLocation' isn't defined for the type 'Home'.但它说没有为“Home”类型定义“getLocation”方法。

This is just an example made up by me trying to understand a real usecase of entity-model where model is a child of entity which contains additional methods like toJson / fromJson .这只是我试图理解实体模型的真实用例而编写的一个示例,其中model实体的子对象,其中包含其他方法,例如toJson / fromJson I am unable to call second child class's(in this case House) toJson(like the getLocation method in this case) method from the first child class (in this case Human).我无法从第一个子 class(在本例中为 Human)调用第二个子类(在本例中为 House)toJson(如本例中的 getLocation 方法)方法。

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

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