此错误是怎么来的? 哪位高手帮忙看看怎么回事?
//此程序报错说类CareAnimal是公共的,应在文件中声明等。
public interface CareAnimal {
public void play();
public void feed();
}
class Cadre implements CareAnimal {
void play() {
System.out.println("干部带它玩");
}
void feed() {
System.out.println("干部给它喂食");
}
void travel() {
System.out.println("干部带它旅游");
}
}
public class Test {
public static void main(String args[]) {
Cadre a = new Cadre();
a.travel();
}
}
[解决办法]
public interface CareAnimal
class Cadre implements CareAnimal
public 子类的范围不能比父类小。
[解决办法]
你的文件名是什么,java需要public的类与文件名一样。一个源文件里只能有一个public
楼主新建一个CareAnimal.java吧
[解决办法]
Cadre 类中实现的三个方法前加上public,不要用那个默认的
[解决办法]
public class TestCars {
public static void main (String [] args) {
Car a = new Car();
Car b = new Porsche(); //Car ref, but a Porsche object
a.drive(); // Runs the Car version of drive()
b.drive(); // Runs the Porsche version of drive()
}
}
class Car {
public void drive() {
System.out.println("Generic Car Driveing Generically");
}
}
class Porsche extends Car {
private void drive() { // whoa! - it's private!
System.out.println("Porsche driving Full Throttle");
}
}
If this code compiled (which it doesn't), the following would fail at runtime:
Car b = new Porsche(); // Car ref, but a Porsche
// object , so far so good
b.drive(); // Chaos at runtime!