所以我对实验后的接口有一个疑问:可以将特定信息放入一个吗?
这是我的界面:
public interface Weapon{
public int damage();
public int range();
public String name();
public int rateOfFire();
public String[] applicableOperators();
public void getSpecs(); //this is the method in which I have a question
}
接下来,我将它实现到给定的枪中:R4C。
public class R4C implements Weapon{
@Override
public int damage(){
return 34;
}
@Override
public int range(){
return 20; //meters
}
@Override
public String name(){
return "R4-C";
}
@Override
public int rateOfFire(){
return 850; //rounds per minute
}
public String[] applicableOperators(){
String[] appOps = new String[1];
appOps[0] = "Ash";
return appOps;
}
public void getSpecs(){
System.out.printf("Name: %s\nDamage: %d\n Rate of Fire: %d\n"
+ "Range: %d\n", name(), damage(), rateOfFire(), range());
System.out.println("\nApplicable Operators:");
for(int arrayCount = 0; arrayCount < applicableOperators().length; arrayCount++){
System.out.printf("%s\n", applicableOperators()[arrayCount]);
}
}
}
所以我希望“getSpecs”方法始终具有相同的输出。无论放入哪种枪,我总是想要这种格式:
名称:( WeaponName)
伤害:(伤害)
火速:(RoF)
范围:(范围)
适用的运营商:
灰
有没有办法在界面内执行此操作?或者我只需要制作一个单独的方法用于其他地方的输出?我知道这可能是一个基本问题,但是因为到目前为止我对接口知之甚少,所以任何输入都可以提供帮助。 谢谢!
答案 0 :(得分:2)
在java&gt; = 8中,您可以在界面中定义默认方法:
public interface Weapon{
public int damage();
public int range();
public String name();
public int rateOfFire();
public String[] applicableOperators();
default public void getSpecs() {
//this is the method in which I have a question
System.out.printf("Name: %s\nDamage: %d\n Rate of Fire: %d\n"
+ "Range: %d\n", name(), damage(), rateOfFire(), range());
for(int arrayCount = 0; arrayCount < applicableOperators().length; arrayCount++){
System.out.printf("%s\n", applicableOperators()[arrayCount]);
}
}
}
Java 8引入了“默认方法”或(Defender方法)新功能, 它允许开发人员在没有的情况下向接口添加新方法 打破了这些接口的现有实现。它提供 允许接口定义实现的灵活性将用作 默认情况下,具体类无法提供 该方法的实施。
答案 1 :(得分:1)
也许您可以使用用户界面默认方法。您可以阅读有关此here的更多信息。
答案 2 :(得分:1)
如果您要归档此类功能,我建议使用abstract class
代替interface
public abstract class Weapon {
protected int damage;
protected int range;
protected int rateOfFire;
protected String name;
abstract public int damage();
abstract public int range();
abstract public String name();
abstract public int rateOfFire();
abstract public String[] applicableOperators();
abstract public void getSpecs(); //this is the method in which I have a question
}
public class R4C extends Weapon {
@Override
public int damage() {
return damage;
}
public void setDamage(int damage) {
this.damage = damage;
}
@Override
public int range() {
return range;
}
public void setRange(int range) {
this.range = range;
}
@Override
public int rateOfFire() {
return rateOfFire;
}
public void setRateOfFire(int rateOfFire) {
this.rateOfFire = rateOfFire;
}
@Override
public String name() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] applicableOperators() {
String[] appOps = new String[1];
appOps[0] = "Ash";
return appOps;
}
public void getSpecs() {
System.out.printf("Name: %s\nDamage: %d\n Rate of Fire: %d\n"
+ "Range: %d\n", name(), damage(), rateOfFire(), range());
System.out.println("\nApplicable Operators:");
for (int arrayCount = 0; arrayCount < applicableOperators().length; arrayCount++) {
System.out.printf("%s\n", applicableOperators()[arrayCount]);
}
}
}
现在,您可以使用setXXX
方法