超类变量不通过子类打印

时间:2012-07-04 15:06:45

标签: java inheritance

你能告诉我为什么这段代码没有在控制台上显示任何结果。

class employee {
    protected String name;
    protected double salary;
    protected String dob;

    public employee(String name, double salary, String dob) {
        this.name = name;
        this.salary = salary;
        this.dob = dob;
    }

    public employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }
}

public class Manage extends employee {
    String dept1;

    public Manage(String name, double salary, String dob, String dept1) {
        super(name, salary, dob);
        this.dept1 = dept1;
    }

    public Manage(String name, double salary, String dept1) {
        super(name, salary);
        this.dept1 = dept1;
    }

    public static void main(String args[]) {
        employee e = new employee("Vikas", 122345);
        employee e2 = new employee("Vikas", 122345, "12-2-1991");
        Manage m = (Manage) new Manage("Vikas", 122345, "Sales");
        Manage m2 = new Manage("Vikas", 122345, "12-2-1991", "sales");
        m.display();
        m2.display();
    }

    public void display() {
        System.out.println("Name " + name);
        System.out.println("Salary " + salary);
        System.out.println("Birth " + dob);
        System.out.println("Department " + dept1);
    }
}

2 个答案:

答案 0 :(得分:2)

你有一些拼写错误的拼写错误。我修好了。可怕的代码,但它的工作原理。首先考虑Sun / Oracle Java编码标准。

另一个建议是编写toString()覆盖而不是display()

您有一个员工构造函数,将出生日期初始化为null。不要那样做;喜欢这样的东西:

public employee(String name, double salary) {
    this(name, salary, new Date());
}

这是我在控制台中看到的内容。你的问题是什么?

"C:\Program Files\Java\jdk1.7.0_02\bin\java" -Didea.launcher.port=7534 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 117.499\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_02\jre\lib\alt-rt.jar;C:\Program Files\Java\jdk1.7.0_02\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_02\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_02\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_02\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_02\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_02\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_02\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_02\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_02\jre\lib\rt.jar;C:\Program Files\Java\jdk1.7.0_02\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_02\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_02\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_02\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_02\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_02\jre\lib\ext\zipfs.jar;F:\Projects\Java\stack-overflow\out\production\Stack-overflow;F:\Projects\Java\stack-overflow\test-lib\junit-4.10.jar;F:\Projects\Java\stack-overflow\test-lib\org.springframework.test-3.1.0.RC2.jar;F:\Projects\Java\stack-overflow\lib\aopalliance-1.0.jar;F:\Projects\Java\stack-overflow\lib\commons-math-2.1-javadoc.jar;F:\Projects\Java\stack-overflow\lib\commons-math-2.1-sources.jar;F:\Projects\Java\stack-overflow\lib\commons-math-2.1.jar;F:\Projects\Java\stack-overflow\lib\dom4j-1.6.1.jar;F:\Projects\Java\stack-overflow\lib\jackson-all-1.6.2.jar;F:\Projects\Java\stack-overflow\lib\jackson-core-asl-1.6.2.jar;F:\Projects\Java\stack-overflow\lib\jackson-core-lgpl-1.6.2.jar;F:\Projects\Java\stack-overflow\lib\jackson-jaxrs-1.6.2.jar;F:\Projects\Java\stack-overflow\lib\jackson-mapper-asl-1.6.2.jar;F:\Projects\Java\stack-overflow\lib\jackson-mapper-lgpl-1.6.2.jar;F:\Projects\Java\stack-overflow\lib\jackson-mrbean-1.6.2.jar;F:\Projects\Java\stack-overflow\lib\jackson-smile-1.6.2.jar;F:\Projects\Java\stack-overflow\lib\jackson-xc-1.6.2.jar;F:\Projects\Java\stack-overflow\lib\Jama-1.0.2.jar;F:\Projects\Java\stack-overflow\lib\jdom.jar;F:\Projects\Java\stack-overflow\lib\jxl.jar;F:\Projects\Java\stack-overflow\lib\org.springframework.aop-3.1.0.RC2.jar;F:\Projects\Java\stack-overflow\lib\org.springframework.asm-3.1.0.RC2.jar;F:\Projects\Java\stack-overflow\lib\org.springframework.aspects-3.1.0.RC2.jar;F:\Projects\Java\stack-overflow\lib\org.springframework.beans-3.1.0.RC2.jar;F:\Projects\Java\stack-overflow\lib\org.springframework.context-3.1.0.RC2.jar;F:\Projects\Java\stack-overflow\lib\org.springframework.context.support-3.1.0.RC2.jar;F:\Projects\Java\stack-overflow\lib\org.springframework.core-3.1.0.RC2.jar;F:\Projects\Java\stack-overflow\lib\org.springframework.expression-3.1.0.RC2.jar;F:\Projects\Java\stack-overflow\lib\org.springframework.instrument-3.1.0.RC2.jar;F:\Projects\Java\stack-overflow\lib\org.springframework.instrument.tomcat-3.1.0.RC2.jar;F:\Projects\Java\stack-overflow\lib\org.springframework.jdbc-3.1.0.RC2.jar;F:\Projects\Java\stack-overflow\lib\org.springframework.jms-3.1.0.RC2.jar;F:\Projects\Java\stack-overflow\lib\org.springframework.orm-3.1.0.RC2.jar;F:\Projects\Java\stack-overflow\lib\org.springframework.oxm-3.1.0.RC2.jar;F:\Projects\Java\stack-overflow\lib\org.springframework.transaction-3.1.0.RC2.jar;F:\Projects\Java\stack-overflow\lib\org.springframework.web-3.1.0.RC2.jar;F:\Projects\Java\stack-overflow\lib\org.springframework.web.portlet-3.1.0.RC2.jar;F:\Projects\Java\stack-overflow\lib\org.springframework.web.servlet-3.1.0.RC2.jar;F:\Projects\Java\stack-overflow\lib\org.springframework.web.struts-3.1.0.RC2.jar;F:\Projects\Java\stack-overflow\lib\postgresql-8.1-405.jdbc3.jar;F:\Projects\Java\stack-overflow\lib\sqlitejdbc-v056.jar;F:\Projects\Java\stack-overflow\lib\StackWrap4J-1.0.1.jar;F:\Tools\apache-tomcat-6.0.35\lib\annotations-api.jar;F:\Tools\apache-tomcat-6.0.35\lib\catalina-ant.jar;F:\Tools\apache-tomcat-6.0.35\lib\catalina-ha.jar;F:\Tools\apache-tomcat-6.0.35\lib\catalina-tribes.jar;F:\Tools\apache-tomcat-6.0.35\lib\catalina.jar;F:\Tools\apache-tomcat-6.0.35\lib\ecj-3.7.jar;F:\Tools\apache-tomcat-6.0.35\lib\el-api.jar;F:\Tools\apache-tomcat-6.0.35\lib\jasper-el.jar;F:\Tools\apache-tomcat-6.0.35\lib\jasper.jar;F:\Tools\apache-tomcat-6.0.35\lib\jsp-api.jar;F:\Tools\apache-tomcat-6.0.35\lib\servlet-api.jar;F:\Tools\apache-tomcat-6.0.35\lib\tomcat-coyote.jar;F:\Tools\apache-tomcat-6.0.35\lib\tomcat-dbcp.jar;F:\Tools\apache-tomcat-6.0.35\lib\tomcat-i18n-es.jar;F:\Tools\apache-tomcat-6.0.35\lib\tomcat-i18n-fr.jar;F:\Tools\apache-tomcat-6.0.35\lib\tomcat-i18n-ja.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 117.499\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain cruft.Manage
Name Vikas
Salary 122345.0
Birth null
Department Sales
Name Vikas
Salary 122345.0
Birth 12-2-1991
Department sales

Process finished with exit code 0

答案 1 :(得分:1)

您的代码错误,请使用带编译器的IDE。这是您想要的代码:

public static void main(String args[]) {
    employee e = new employee("Vikas", 122345);
    employee e2 = new employee("Vikas", 122345, "12-2-1991");
    Manage m = new Manage("Vikas", 122345, "Sales");
    Manage m2 = new Manage("Vikas", 122345, "12-2-1991", "sales");
    m.display();
    m2.display();
}

static class employee {
    protected String name;
    protected double salary;
    protected String dob;

    public employee(String name, double salary, String dob) {
        this.name = name;
        this.salary = salary;
        this.dob = dob;
    }

    public employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }
}

static public class Manage extends employee {
    String dept1;

    public Manage(String name, double salary, String dob, String dept1) {
        super(name, salary, dob);
        this.dept1 = dept1;
    }

    public Manage(String name, double salary, String dept1) {
        super(name, salary);
        this.dept1 = dept1;
    }

    public void display() {
        System.out.println("Name " + name);
        System.out.println("Salary " + salary);
        System.out.println("Birth " + dob);
        System.out.println("Department " + dept1);
    }
}