-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 コーディング標準を心に留めることから始めます。

もう 1 つの提案は、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 に答える