英文:
Call a different constructor depending on the static type of the instance created
问题
以下是翻译好的内容:
我有一个程序,其中一个类 Car 实现了两个接口:IRentable 和 IBuyable。
我的类大致如下所示:
class Car implements IRentable, IBuyable {
private String name;
private float rentValue;
private float sellValue;
// rentable constructor
public Car(String name, float rentValue) {
// ...
}
// buyable constructor
public Car(String name, float buyValue) {
// ...
}
// ...
}
对象的实例化大致如下所示:
IRentable c1 = new Car("name", 700f);
IBuyable c2 = new Car("name", 35_000f);
我想知道是否有一种方式可以根据对象的静态类型调用特定的构造函数。例如,如果我有 IRentable 类型:
IRentable c1 = new Car("name", 700f);
调用 rentable 构造函数。
如果我有 IBuyable 类型:
IBuyable c2 = new Car("name", 35_000f);
调用 buyable 构造函数。
英文:
I have a program where a class car implements two interfaces : rentable and buyable.
My class looks something like this:
Class Car implements IRentable, IBuyable{
private String name;
private float rentValue;
private float sellValue;
//rentable constructor
public Car(String name, float rentValue){
...
}
//buyable constructor
public Car(String name, float buyValue){
...
}
...
}
And the instantiation of the object would look something like this:
IRentable c1= new Car("name",700f);
IBuyable c2= New Car ("name",35_000f);
I was wondering if there is a way to call a specific constructor based on the static type of the object. For example, if I have the IRentable type:
IRentable c1= new Car("name",700f);
call the rentable constructor.
If I have the IBuyable type:
IBuyable c2= New Car ("name",35_000f);
call the buyable constructor.
答案1
得分: 0
这是不可能的,因为不同的构造函数需要不同的方法签名。
你可以有一个构造函数,带有一个标志作为第三个参数:
IRentable c1 = new Car("name", 700f, true);
IBuyable c2 = new Car("name", 35_000f, false);
英文:
This is not possible as different constructors need different method signatures.
You could have one constructur with a flag as third parameter:
IRentable c1= new Car("name",700f,true);
IBuyable c2= New Car ("name",35_000f,false);
答案2
得分: 0
我认为针对这种情况,你应该考虑建造者模式,或者如果你只需要这两个构造函数,那么你可以将全参数构造函数设置为私有,并创建两个静态方法来相应地创建对象。
英文:
I think for this case you should consider builder pattern or if you only need those two constructors then you could make the all args constructor private and create two static methods that would create you objects accordingly.
答案3
得分: 0
使用静态方法,可以命名为了明确区分。将构造函数设置为private
。
class Car implements IRentable, IBuyable {
private String name;
private float rentValue;
private float buyValue;
public static IRentable rent(String name, float rentValue) {
return new Car(name, rentValue, Float.NaN);
}
public static IBuyable buy(String name, float buyValue) {
return new Car(name, Float.NaN, buyValue);
}
private Car(String name, float rentValue, float buyValue) {
...
}
...
}
使用方法如下:
IRentable c1 = Car.rent("name", 700f);
IBuyable c2 = Car.buy("name", 35_000f);
英文:
Use static methods, which can be named to clarify the difference. Make the constructor private
.
class Car implements IRentable, IBuyable {
private String name;
private float rentValue;
private float buyValue;
public static IRentable rent(String name, float rentValue) {
return new Car(name, rentValue, Float.NaN);
}
public static IBuyable buy(String name, float buyValue) {
return new Car(name, Float.NaN, buyValue);
}
private Car(String name, float rentValue, float buyValue) {
...
}
...
}
They are used like this:
IRentable c1 = Car.rent("name", 700f);
IBuyable c2 = Car.buy("name", 35_000f);
答案4
得分: 0
你不能创建具有相同类型签名的超过1个构造函数。如果一辆汽车只能是可租赁(Rentable)或可购买(Buyable),你可以创建一个单一的接口,让上述两个接口都扩展自它,比如叫做 Costable(虽然名字不太好)。
interface Costable {
float amount();
}
然后将汽车(Car)组合成一个 Costable 对象。
class Car implements Costable {
private final Costable costable;
public Car(Costable costable) {
this.costable = costable;
}
@Override
public float amount() {
return costable.amount();
}
}
然后你可以基于 Rentable/Buyable 来扩展你的实现。这里是一个使用你上面已有的 IRentable 的示例。
class IRentable implements Costable {
private final float amount;
public IRentable(float amount) {
this.amount = amount;
}
@Override
public float amount() {
return amount;
}
}
Costable rentable = new Car(new IRentable(1_000));
英文:
You can't create more than 1 constructor with the same type signature(s). If a car is only suppose to be either Rentable or Buyable, you could create a single interface that both of the aforementioned interfaces extend like Costable (bad name i know).
interface Costable {
float amount();
}
Then compose the Car of a Costable object.
class Car implements Costable {
private final Costable;
public Car(Costable costable) {
this.costable = costable;
}
@Override
public float amount() {
return costable.amount();
}
}
Then you could extend Rentable/Buyable with your implementations. Here is an example with IRentable like you have above.
class IRentable implements Costable {
private final float amount;
public IRentable(float amount) {
this.amount = amount;
}
@Override
public float amount() {
return amount;
}
}
Costable rentable = new Car(new IRentable(1_000));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论