当前位置: 代码迷 >> Java相关 >> super问题。
  详细解决方案

super问题。

热度:354   发布时间:2009-09-19 18:46:28.0
super问题。

  下面不知道什么问题,老实在super那里出错,帮忙找找大家。
、============================

class Person {
    String name;
    int id;
   
    Person (String name, int id) {
        this.name = name;
        this.id = id;
    }
   
    public String info() {
        return "name:" + name + "\n" + "ID:" + id;
    }
}

class Teacher {
    String prof;
   
    Teacher (String n, int i, String prof){
        super(n,i);
        this.prof = prof;
    }
   
    public String info(){
        return "name:" + name + "\n" + "ID:" + id +"\n" +"Prof" + prof;
    }
}

public class Test1 {
    public static void main(String args[]) {
        Person r = new Person("zhangsan",23);
        Teacher t = new Teacher("laozhang",35);
        System.out.println(r.info());
        System.out.println(t.info());
    }
}

++++++++++++++++++++++

搜索更多相关的解决方案: super  

----------------解决方案--------------------------------------------------------
class Person {
    String name;
    int id;
     
    Person (String name, int id) {
        this.name = name;
        this.id = id;
    }
     
    public String info() {
        return "name:" + name + "\n" + "ID:" + id;
    }
}

class Teacher extends Person{
    String prof;
     
    Teacher (String n, int i, String prof){
        super(n,i);
        this.prof = prof;
    }
     
    public String info(){
        return "name:" + name + "\n" + "ID:" + id +"\n" +"Prof" + prof;
    }
}

public class Test1 {
    public static void main(String args[]) {
        Person r = new Person("zhangsan",23);
        Teacher t = new Teacher("laozhang",35);
        System.out.println(r.info());
        System.out.println(t.info());
    }
}

----------------解决方案--------------------------------------------------------
extends是继承父类

----------------解决方案--------------------------------------------------------
是楼主少写了extends吧!!这么重要的东西!
再问一下:
class Teacher extends Person{  
    String prof;  
      
    Teacher (String n, int i, String prof){  
        super(n,i);  //把这里的换成this.(n,i)可以不

        this.prof = prof;
     }  

----------------解决方案--------------------------------------------------------
是楼主少写了extends吧!!这么重要的东西!
再问一下:
class Teacher extends Person{  
    String prof;  
      
    Teacher (String n, int i, String prof){  
        super(n,i);  //把这里的换成this.(n,i)可以不

        this.prof = prof;
     }  

----------------解决方案--------------------------------------------------------
回复 5楼 sonwill
不可以,this指的是对象对象的引用。而super指的的基类的引用。
----------------解决方案--------------------------------------------------------
回复 6楼 vecomwa
谢谢!前几天去笔试遇到了!多选,我也选进去了!惨了!
不过也学习了。
----------------解决方案--------------------------------------------------------
以下是引用sonwill在2009-9-20 13:49的发言:

是楼主少写了extends吧!!这么重要的东西!
再问一下:
class Teacher extends Person{  
    String prof;  
      
    Teacher (String n, int i, String prof){  
        super(n,i);  //把这里的换成this.(n, ...
不行的 因为在使用子类的构造方法时没有现式的调用父类的构造方法则系统会默认调用父类的无参构造方法,可是这题的父类中并没有定义无参构造方法,所以会出现错误 如果父类有定义无参构造方法 就是可以的。
----------------解决方案--------------------------------------------------------
  相关解决方案