|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我分了四个类
people是父类 有个run方法
student和teacher是子类 各自重写run方法、
test 创建两个对象 然后各自调run方法
提示错误
Syntax error on token "run", Identifier expected after this token
请问是哪里出问题了
People类
package itHeimaA2;
public class People {
public void run() {
System.out.println("People is running");
}
}
Teacher类
package itHeimaA2;
public class Teacher extends People{
public void run() {
System.out.println("Teacher is running~~~~")
}
}
Student类
package itHeimaA2;
public class Student extends People{
public void run() {
System.out.println("Student is running~~~");
}
}
Test类
package itHeimaA2;
public class Test {
People p1 = new Student();
p1.run();
People p2 = new Student();
p2.run();
}
运行结果如图
代码是直接抄的应该没有抄错 第一次运行成功了 但是不小心按的 ctrl shift f11 然后代码全变色了 关闭窗口 删除原项目 重写后 就这样报错了 请问是哪里出了问题 谢谢各位了
在Test类中,p1.run()和p2.run()应该在方法中执行,而不是在类中。正确的代码应该是这样的:
- package itHeimaA2;
- public class Test {
- public static void main(String[] args){
- People p1 = new Student();
- p1.run();
- People p2 = new Teacher();
- p2.run();
- }
- }
复制代码
此外,Teacher类中的run方法缺少一个分号,应该改为:
- package itHeimaA2;
- public class Teacher extends People{
- public void run() {
- System.out.println("Teacher is running~~~~");
- }
- }
复制代码
|
-
|