1

错误发生在和public class ModemTester下。我是编程新手和这本书,山姆在 24 小时内学习 Java,我正在经历这个练习,清单 10.2-5,我无法弄清楚。你们所有的专家都让我充满你的智慧^-^。谢谢你CableModemDslModem

public class Modem {
    int speed;

    public void displaySpeed() {
        System.out.println("Speed: " + speed);
   }
}

public class CableModem extends Modem {
    String method = "cable connection";

    public void connect() {
        System.out.println("Connecting to the Internet.....");
        System.out.println("Using a " + method);
    }
}

public class DslModem extends Modem {
    String method = "DSL phone connection";

    public void connect() {
        System.out.println("Connecting to the Internet.....");
        System.out.println("Using a " + method);
    }
}

public class ModemTester {
   public static void main(String[] args) {
        CableModem surfBoard = new CableModem();
        DslModem gateway = new DslModem();
        surfBoard.speed = 500000;
        gateway.speed = 400000;
        System.out.println("Trying the cable modem:");
        surfBoard.displaySpeed();
        surfBoard.connect();
        System.out.println("Trying the DSL modem:");
        gateway.displaySpeed();
        gateway.connect();
    }
}
4

2 回答 2

0

Java 不允许每个文件有一个以上的顶级公共类

于 2013-08-06T15:50:17.013 回答
0

这些类是否在同一个包/文件夹中?因为speed变量是包私有的,这意味着它只有ModemTester在与Modem.

您可以通过设置int speed来测试它public int speed,看看您是否仍然收到错误。

于 2013-08-06T15:55:03.860 回答