私はsomtimes 'next generation'や「機能」として記述されている次のスタイルに私の不変データオブジェクトのほとんどを書いている:インタフェースと一緒に「次世代」Javaデータオブジェクトスタイルを使用するにはどうすればよいですか?
public class Point {
public final int x;
public final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
私はインタフェースによって指定されたデータオブジェクトに対して同じスタイルを使用したいと思います:
public interface Point {
public final int x;
public final int y;
}
public class MyPoint {
public MyPoint(int x, int y) {
this.x = x;
this.y = y;
}
}
public class Origin {
public Origin() {
this.x = 0;
this.y = 0;
}
}
これは、Javaでは許可されていません。これは、インターフェイスコードと実装にエラーが発生します。
私は
public interface Point {
public int x();
public int y();
}
public class MyPoint {
private int mx, my;
pulic MyPoint(int x, int y) {
mx = x;
my = y;
}
public int x() {return mx;}
public int y() {return my;}
}
public class Origin {
public int x() {return 0;}
public int y() {return 0;}
}
に私のコードを変更することができます。しかし、それはより多くのコードである、と私はそれがAPIで簡単のほぼ同じ感じを与えるとは思いません。
私のジレンマからパスが見えますか?それとも、あなたは個人的にもっとシンプルなスタイルを使用しますか?
(私は可変/不変、getterSetter /新しいスタイルまたは公共/民間分野の議論で、本当に興味がありません。)
「次世代」は単に「不変型」ですか? –
ええ、それはおそらく公正だと言います。 –