我正在阅读this Oracle Java interface tutorial。
如果您不想解决我所做的具体编码错误,我正在寻找比教程提供的更多信息:如何获得实际使用ACMEBicycle中定义的方法的接口示例部分,如打印方法?我似乎无法复制粘贴过去的示例,也不能以编译器接受的方式修改它们。 (或者更确切地说,运行代码的东西?)
那里的代码似乎并不完整,似乎依赖于读者进行实验以找到正确的语法。我没有成功。首先,我尝试更改"良好编码实践中的代码,关于界面与类和超级类#34;使用接口来定义渗透及其3个站点。我意识到这给了我太多不受控制的变量,因为它是我自己的代码,所以我决定使用提供的自行车示例。为了实际使用我设置的内容,我尝试了与the bicycledemo example under the class tutorial类似的内容。这是代码:
package javaapplication5;
/** * * @author Jeff */
interface Bicycle {
// wheel revolutions per minute
void changeCadence(int newValue);
void changeGear(int newValue);
void speedUp(int increment);
void applyBrakes(int decrement);
}
class ACMEBicycle implements Bicycle {
int cadence = 0;
int speed = 0;
int gear = 1;
// The compiler will now require that methods
// changeCadence, changeGear, speedUp, and applyBrakes
// all be implemented. Compilation will fail if those
// methods are missing from this class.
void changeCadence(int newValue) {
cadence = newValue;
}
void changeGear(int newValue) {
gear = newValue;
}
void speedUp(int increment) {
speed = speed + increment;
}
void applyBrakes(int decrement) {
speed = speed - decrement;
}
void printStates() {
System.out.println("cadence:" +
cadence + " speed:" +
speed + " gear:" + gear);
}
}
class ACMEBycicle{ ACMEBycicle.printStates() }
public class JavaApplication5 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
}
}
当前版本显示我只是尝试使用一个简单的 print 命令,该命令已经定义为interface Bicycle
的特定实现 ACMEBicycle 的一部分。我还尝试复制 bicycledemo 示例而不是编写我自己的代码,除了我觉得需要用 ACMEBicycle 替换自行车导致< / p>
package javaapplication5;
/** * * @author Jeff */
interface Bicycle {
//wheel revolutions per minute
void changeCadence(int newValue);
void changeGear(int newValue);
void speedUp(int increment);
void applyBrakes(int decrement);
}
class ACMEBicycle implements Bicycle {
int cadence = 0;
int speed = 0;
int gear = 1;
// The compiler will now require that methods
// changeCadence, changeGear, speedUp, and applyBrakes
// all be implemented. Compilation will fail if those
// methods are missing from this class.
void changeCadence(int newValue) {
cadence = newValue;
}
void changeGear(int newValue) {
gear = newValue;
}
void speedUp(int increment) {
speed = speed + increment;
}
void applyBrakes(int decrement) {
speed = speed - decrement;
}
void printStates() {
System.out.println("cadence:" +
cadence + " speed:" +
speed + " gear:" + gear);
}
}
class BicycleDemo {
public static void main(String[] args) {
// Create two different
// Bicycle objects
ACMEBicycle bike1 = new ACMEBicycle();
ACMEBicycle bike2 = new ACMEBicycle();
// Invoke methods on
// those objects
bike1.changeCadence(50);
bike1.speedUp(10);
bike1.changeGear(2);
bike1.printStates();
bike2.changeCadence(50);
bike2.speedUp(10);
bike2.changeGear(2);
bike2.changeCadence(40);
bike2.speedUp(10);
bike2.changeGear(3);
bike2.printStates();
}
}
public class JavaApplication5 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
}
}
答案 0 :(得分:0)
你已经在bike2.printStates()之后关闭了一次课程。 删除第二个}
答案 1 :(得分:0)
每个样本都有几个问题。一个在您引用的教程页面上解决:
注意:要实际编译ACMEBicycle类,您需要将public关键字添加到已实现的接口方法的开头。