标题: 出错了,少写了哪句 [打印本页] 作者: 赵嘉男 时间: 2012-4-26 17:39 标题: 出错了,少写了哪句 public class Person {
String name;
public void p(){
System.out.println("The person's name is Zhangheng");
}
public static void main() {
Person per = new Person("Zhangheng");
per.p();
Student stu = new Student("Zhangyu");
stu.study();
}
}
class Student extends Person {
Student() {
super("Zhangyu");
}
public void study() {
System.out.println("The student is Studying");
}
}
错误在哪呢,是不是少写语句了。
题目:编写学生类,继承人类,要求父类的构造函数为(preson(string name),子类的构造函数要调用父类的构造函数.p()方法用于输出该对象的信息(如The person's name is Zhangheng),学生类有study()方法,并分别创建一个对象并调用各个方法作者: 施俊 时间: 2012-4-26 17:46
Person类里面没有定义有参数的构造方法,你创建对象的时候传了个参数进去。作者: 翟友伟 时间: 2012-4-26 17:46
super("Zhangyu");
你 Person类的构造函数在哪里作者: 罗旭维 时间: 2012-4-26 17:46
两个类都没有定义有参构造方法而 new的时候调用的是有参构造方法,作者: 邓斌 时间: 2012-4-26 17:47
有点看不懂你代码。Person没有定义参数的构造函数,也没有定义构造函数,那默认为空参数
而Student构造函数也无参数,函数中却调用父类的构造函数传入参数,作者: 光sail 时间: 2012-4-26 17:59
public class Person {
String name;
public void p(){
System.out.println("The person's name is Zhangheng");
}
public static void main() {
Person per = new Person("Zhangheng"); //person类没有构造方法,这里就无法调用Person的有参数的构造方法
per.p();
Student stu = new Student("Zhangyu"); //Student类没有构造方法,这里就无法调用Student的有参数的构造方法
stu.study();
}
}
class Student extends Person {
Student() {
super("Zhangyu"); //Student的父类Person类没有构造方法,这里就无法调用Student父类Person的有参数的构造方法
}
public void study() {
System.out.println("The student is Studying");
}
} 作者: 贾联国 时间: 2012-4-26 18:03
可能理解上有些问题 也有几句写错了 我帮你改了下 参考下吧
class PersonDemo
{
public static void main(String[] args)
{
Person per = new Person("Zhangheng");
per.p();
Student stu = new Student();//既然调用了构造函数初始化,里面就不要放参数了
System.out.println("The person's name is "+this.name);
}
}
class Student extends Person {
Student() {
super("Zhangyu");
}
public void study() {
System.out.println("The student is Studying");
}
}
复制代码
作者: Friends★ 时间: 2012-4-26 18:11
你的代码有点乱!
而且子父类中缺少构造方法!
代码修改如下:
class Test9{
public static void main(String[] args) {
Person p=new Person("zhangheng");
p.show();
Student stu = new Student("Zhangyu");
stu.study();
}
}
class Person{
String name;
Person(String name){
this .name=name;
}
public void show(){
System.out.println("The person's name is "+name);
}
}
class Student extends Person{
Student(String name){
super(name);
}
public void study() {
System.out.println(name+ " "+"The student is Studying");
}
}
运行结果
The person's name is zhangheng
Zhangyu The student is Studying作者: Friends★ 时间: 2012-4-26 18:44
你的代码有点乱!
而且子父类中缺少构造方法!
代码修改如下:
class Test9{
public static void main(String[] args) {
Person p=new Person("zhangheng");
p.show();
Student stu = new Student("Zhangyu");
stu.study();
}
}
class Person{
String name;
Person(String name){
this .name=name;
}
public void show(){
System.out.println("The person's name is "+name);
}
}
class Student extends Person{
Student(String name){
super(name);
}
public void study() {
System.out.println(name+ " "+"The student is Studying");
}
}
运行结果
The person's name is zhangheng
Zhangyu The student is Studying作者: 邵中国 时间: 2012-4-26 22:36
带参的构造函数呢?作者: 李培辉 时间: 2012-4-26 23:59
首先,每个类都会有一个构造方法,没有写的话就会有一个默认的参数为空的构造函数。子类在继承父类时,子类的构造方法默认会有一句super(),也可指定带参数的super(),或者在第一行去调用自身其他的构造函数,其他的构造函数第一行必定会有一个super()。此时,父类如果有带参数的构造函数,而没有不带参数的构造函数的话,系统不会再默认提供一个空参的,就需要自己编写,否则编译就会报错。自己怎么讲也讲不清,你还是自己亲自动手多试几次,多错几次就能理解了。作者: Friends★ 时间: 2012-4-27 08:08