我有一个关于接口的相当基本的问题,我也很新。我通常会使用重载的构造函数来实例化我的类。我现在正在尝试使用接口并想知道如何填充我的构造函数。我会在我的界面中使用类似 setSomeMethod(arguement1,arguement2) 的东西来填充我的属性吗?
我还想指出我正在使用带有服务注入的“Tapestry5”框架。例子
public class Main {
@Inject
private Bicycle bicycle;
public Main() {
//Not sure how to pass constructor variables in
this.bicycle();
}
}
界面
public interface bicycle {
public void someMethod(String arg1, String arg2);
}
实现类
public class MountainBike implements bicycle {
private String arg1;
private String arg2;
public MountainBike() {
//Assuming there is no way to overload this constructor
}
public void someMethod(String arg1, String2 arg2) {
this.arg1 = arg1;
this.arg2 = arg2;
}
}
那么你如何处理扩展类呢?我不确定如何填充扩展类构造函数。
public class MountainBike extends BicycleParts implements bicycle {
private String arg1;
private String arg2;
public MountainBike() {
//Assuming there is no way to overload this constructor
//Not sure where to put super either, but clearly won't work here.
//super(arg1);
}
public void someMethod(String arg1, String2 arg2) {
this.arg1 = arg1;
this.arg2 = arg2;
//Assuming it doesn't work outside of a constructor, so shouldn't work
//here either.
//super(arg1);
}
}
public class BicycleParts {
private String arg1;
public void BicycleParts(String arg1) {
this.arg1 = arg1;
}
}
提前致谢。