Created
March 10, 2016 00:01
-
-
Save yuba/ce0e32dd029220c9f813 to your computer and use it in GitHub Desktop.
継承関係があるのはわかる、で、どちらが親でどちらが子? ref: http://qiita.com/yuba/items/baceffbdbcfdc03f7945
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Real { | |
double r; | |
} | |
class Complex { | |
double r; | |
double i; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Real { | |
double r; | |
} | |
class Complex extends Real { | |
double i; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Complex { | |
double r; | |
double i; | |
} | |
class Real extends Complex { | |
public Complex(double r) { | |
this.r = r; | |
this.i = 0; | |
// iが常に0であるようにコードで保つ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Complex { | |
double r { get; set; } | |
double i { get; set; } | |
} | |
class Real: Complex { | |
double i { | |
get { return 0; } | |
set { } | |
// そもそもミュータブルにするかってのはあるがとりあえず虚部の代入は無視。 | |
// 例外飛ばす方がより厳密ですよね。 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface IComplex { | |
double getR(); | |
double getI(); | |
// せっかくだから今回はイミュータブルにしてみましたよ! | |
} | |
class Real implements IComplex{ | |
double r; | |
double getR() { return r; } | |
double getI() { return 0; } | |
} | |
class Complex implements IComplex { | |
double r; | |
double i; | |
double getR() { return r; } | |
double getI() { return i; } | |
// いつも冗長でうんざりさせられるgetter/setterもこういう状況では役に立つというもの。 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment