更新时间:2023-11-30 21:54:10
没有内置的方法可以做到这一点.您当然可以编写一个方法,该方法将采用 Foo 并创建一个已使用相关属性初始化的 Bar.
There's no built-in way to do this. You can certainly write a method that will take a Foo and create a Bar that's been initialized with the relevant properties.
public Bar getBar() {
Bar bar = new Bar();
bar.setPropOne(this.getPropOne());
bar.setPropTwo(this.getPropTwo());
return bar;
}
另一方面,继承意味着Foo 是 Bar,所以你可以这样做
On the other hand, what inheritance means is that a Foo is a Bar, so you could just do
public Bar getBar() {
return this;
}