黑马程序员技术交流社区

标题: 出错了,少写了哪句 [打印本页]

作者: 赵嘉男    时间: 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
可能理解上有些问题  也有几句写错了  我帮你改了下  参考下吧
  1. class  PersonDemo
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 Person per = new Person("Zhangheng");
  6.     per.p();
  7.     Student stu = new Student();//既然调用了构造函数初始化,里面就不要放参数了
  8.     stu.study();
  9.         }
  10. }
  11. //既然是要继承Person的,那么Person里面就不要写主函数了,单独写个Person类,再用主函数调用Person的方法,这样才对!
  12. class Person
  13. {       
  14.         String name;

  15.         Person(String name)//构造函数初始化要这样写啊
  16.         {
  17.                 this.name =name;
  18.         }
  19.   
  20.   public void p()
  21.         {
  22.     System.out.println("The person's name is "+this.name);
  23.    }
  24. }

  25. class Student extends Person {
  26.   Student() {
  27.     super("Zhangyu");
  28.   }
  29.   public void study() {
  30.     System.out.println("The student is Studying");
  31.   }
  32. }
复制代码

作者: 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
Friends★ 发表于 2012-4-26 18:44
你的代码有点乱!
而且子父类中缺少构造方法!
代码修改如下:

因为宿舍临时掉网,所以不小心发了两遍,下次我会注意的!
作者: 杨志    时间: 2012-4-27 12:05
  1. test
复制代码

作者: 付左军    时间: 2012-4-27 18:58
初始化不成功





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2