-9

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

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);
    }
}
4

2 回答 2

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
于 2012-07-04T15:15:20.643 回答
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);
    }
}
于 2012-07-04T15:10:57.603 回答