Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inheritance #17

Open
8igMac opened this issue Oct 28, 2021 · 2 comments
Open

Inheritance #17

8igMac opened this issue Oct 28, 2021 · 2 comments

Comments

@8igMac
Copy link
Owner

8igMac commented Oct 28, 2021

Requirement

在物件導向程式設計裡,我們可以用繼承 (inheritance) 來減少重複的代碼(在 Dart 語言裡頭,繼承是用 extends )。

如果我們對 super class 得實作不滿意,可以在我們想複寫的 method 上加上 @override 以可以客製化我們想要的行為。

如果你只要 super class 定義好的 method 介面,但所有實作都想自己來,可以把 extends 換成 implements

mixin 是 Dart 裡頭的另一個功能,因為 Dart 規定 sub class 最多只能繼承一個 super class,所以當我們有很多 class
他們的 super class 都不同,但我們想對這群 class 實作一個共同的行為,我們只能一個一個手動實作。mixin 提供了另一個
選擇,可以根據行為來擴充我們的繼承,而且這些擴充是線性疊加上去的(如果有重複 method 後面的會覆蓋前面的)。

可以理解一下下面的 code

class SuperHero {
  String name = '';
  void mySuperPower() {
    print("I'm $name, My super power is ...");
  }
}

// Regulate this mixin to be used only in `SuperHero`'s
// subclass. Because we use instance variable `name` that
// is only define in `SuperHero`.
mixin Fly on SuperHero {
  void fly() => print('My name is $name, I can fly.');
}

mixin RaserEyes {
  void raserEyes() => print('I have raser eyes.');
}

mixin Strength {
  void strength() => print('I have super strength.');
}

// Pure inheritance.
class Batman extends SuperHero {
  String name = 'Batman';
}

// Re`implements` all the method of `SuperHero`.
class Flash implements SuperHero {
  String name = 'Flash';

  // Don't need `@override`.
  void mySuperPower() {
    print("I'm $name. I'm fast.");
  }
}

class WonderWoman extends SuperHero with Strength {
  String name = 'Wonder Woman';

  @override
  void mySuperPower() {
    print("I'm $name, ");
    strength();
  }
}

class Superman extends SuperHero with Fly, RaserEyes, Strength {
  String name = 'Superman';

  @override
  void mySuperPower() {
    fly();
    raserEyes();
    strength();
  }
}

void main() {
  var batman = Batman();
  var superman = Superman();
  var wonderWoman = WonderWoman();
  var flash = Flash();

  batman.mySuperPower();
  superman.mySuperPower();
  wonderWoman.mySuperPower();
  flash.mySuperPower();
}

輸出

I'm Batman, My super power is ...
My name is Superman, I can fly.
I have raser eyes.
I have super strength.
I'm Wonder Woman,
I have super strength.
I'm Flash. I'm fast.

發揮你們的創意,在日常生活中找找類似的關係並實作成一個簡單的繼承架構,並練習以下的概念

  • extends
  • @override
  • mixin
  • implements (optional)

這次不用寫測試。

Reference

@dodo5487
Copy link
Collaborator

想問 你寫 mixin RaserEyes 的用意 而不寫 class RaserEyes 是為了想讓人更清楚的知道 他是mixin 的用途 還是有其他想法呢

@8igMac
Copy link
Owner Author

8igMac commented Oct 31, 2021

"..Unless you want your mixin to be usable as a regular class, use the mixin keyword instead of class...."
官方文件的說法,感覺是加上mixin keyword可以避免用來生成物件,是一個保護作用。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants