File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -956,6 +956,35 @@ head:
956956- スーパークラスで private 宣言されているメソッドと同じ名前のメソッドをサブクラスで定義しない
957957 スーパークラスにある private メソッドと同じ名前のメソッドをサブクラスで定義しないこと。private メソッドはオーバーライドされず全く別のメソッドとして扱われ、他の人の混乱を招き、バグにつながる恐れがある。
958958
959+ - サブクラスのコンストラクタで`super ()`呼び出し前の処理を利用する
960+ スーパークラスのコンストラクタに渡す引数の検証や構築が必要な場合、`super ()`呼び出しの前にロジックを記述することを推奨する。
961+ これにより、従来行われていたprivate コンストラクタへの委譲などの迂回策を避け、より自然なコードを記述が可能。
962+ ただし、`super ()`呼び出しの前にインスタンスフィールドへアクセスするとコンパイルエラーになる点に注意すること。
963+
964+ 悪い例:
965+
966+ ```java
967+ class Bar extends Foo {
968+ private Bar(List<String > l) {
969+ super (l, l);
970+ }
971+ Bar() {
972+ this (List . of(" abc" , " def" ));
973+ }
974+ }
975+ ```
976+
977+ 良い例:
978+
979+ ```java
980+ class Bar extends Foo {
981+ Bar () {
982+ var param = List . of(" abc" , " def" ); // super() の前にロジックを記述
983+ super (param, param);
984+ }
985+ }
986+ ```
987+
959988## インナークラス
960989
961990- 原則としてインナークラスは利用しない
You can’t perform that action at this time.
0 commit comments