[英]Running java on online compiler jdoodle.com and having some issues
这是我正在为多级继承练习的代码,但是当我运行它时,在线编译器会给我以下错误:
public class MyClass {
^
/grandFather.java:26: error: non-static method talking() cannot be referenced from a static context
son.talking();
^
/grandFather.java:27: error: non-static method walking() cannot be referenced from a static context
son.walking();
^
/grandFather.java:28: error: non-static method running() cannot be referenced from a static context
son.running();
这是我正在运行的代码。 这是java中多级继承的练习代码。我创建了3个类(祖父,父亲和儿子):
public class grandFather{
int age;
double height;
public void talking(){
System.out.println("Grandfather can walk");
}
}
class father extends grandFather{
public void walking(){
System.out.println("Father can walk too");
}
}
class son extends father{
public void running(){
System.out.println("Son can talk, walk and also run");
}
}
public class MyClass {
public static void main(String args[]) {
son kamran = new son();
son.talking();
son.walking();
son.running();
}
}
基本上我在做:“son.talking()”而不是“kamran.talking”。
public class grandFather{
int age;
double height;
public void talking(){
System.out.println("Grandfather can walk");
}
public static void main(String args[]) {
son kamran = new son();
kamran.talking();
kamran.walking();
kamran.running();
}
}
class father extends grandFather{
public void walking(){
System.out.println("Father can walk too");
}
}
class son extends father{
public void running(){
System.out.println("Son can talk, walk and also run");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.