以下代码运行输出是0public class Personprivate String name=Personint age=0;public class Child extends Personfpublic String grade;public static void mainStringl argsPerson p = new Child0;Systemoutprintlnpname;3编
编译错误,代码中有多处语法错误:
- 在Person类的定义中,缺少了构造函数的括号。
- 在Person类中,name变量的赋值语句中使用了不合法的引号字符。
- 在Child类的定义中,缺少了继承父类的关键字extends。
- 在Child类的定义中,缺少了构造函数的括号。
- 在main方法中,args参数的类型声明错误,应为String[]而不是Stringl]。
- 在main方法中,创建Child对象时缺少了括号。
- 在main方法中,输出语句的括号中缺少了分号。
修改后的代码如下:
public class Person {
private String name = "Person";
private int age = 0;
public class Child extends Person {
public String grade;
}
public static void main(String[] args) {
Person p = new Person().new Child();
System.out.println(p.name);
}
}
输出结果为:
Person
``
原文地址: http://www.cveoy.top/t/topic/iTWJ 著作权归作者所有。请勿转载和采集!