【问题标题】:Why would java program compile but not run?为什么java程序会编译但不能运行?
【发布时间】:2012-02-12 20:23:38
【问题描述】:

我有一个名为 Employee 的类,它有 2 个 private 变量,还包含它所需的构造函数和其他必要的方法:

public class Employee {

    private String name;
    private int id;

    public Employee() {
        name = " No Name!";
        id = 00100;
    }

    public Employee(String n, int i) {
        name = n;
        id = i;
    }

    public Employee(Employee originalObject) {
        name = originalObject.name;
        id = originalObject.id;
    }

    public String getName() {
        return name;
    }

    public int getID() {
        return id;
    }

    public void setName(String newName) {
        if (newName == null) {
            System.out.println("Fatal Error setting employee name!");
            System.exit(0);
        } else {
            name = newName;
        }
    }

    public void setID(int newID) {
        id = newID;
    }

    public String toString() {
        return (name + " " + id);
    }

    public boolean equals(Employee otherEmployee) {
        return (name.equals(otherEmployee.name)
                && id == otherEmployee.id);
    }
}

这个Employee 类扩展了另一个名为HourlyEmployee 的类。扩展类如下:

public class HourlyEmployee extends Employee {

    private double wageRate;
    private double hours;

    public HourlyEmployee() {
        super();
        wageRate = 0;
        hours = 0;
    }

    public HourlyEmployee(String na, int di, double wR, double h) {
        super(na, di);
        if (wR >= 0 || h >= 0) {
            wageRate = wR;
            hours = h;
        } else {
            System.out.println("Fatal Error!: creating illegal hourly employee");
        }
        System.exit(0);
    }

    public HourlyEmployee(HourlyEmployee originalObject) {
        super(originalObject);
        wageRate = originalObject.wageRate;
        hours = originalObject.hours;
    }

    public double getRate() {
        return wageRate;
    }

    public double getHours() {
        return hours;
    }

    public double getPay() {
        return wageRate * hours;
    }

    public void setRate(double newWR) {
        if (newWR >= 0) {
            wageRate = newWR;
        } else {
            System.out.println("Fatal Error: negative hours worked!");
            System.exit(0);
        }
    }

    public String toString() {
        return (getName() + " " + getID() + "\n$" + wageRate + " per hour for " + hours + "hours");
    }
}

在我完成这个类的编码之后,我编写了 Demo 类来测试这些类以及它是如何工作的。

public class InheritanceDemo {

    public static void main(String[] args) {
        HourlyEmployee joe = new HourlyEmployee("Joe Worker", 281952, 50.50, 160);
        System.out.println("joe's longer name is " + joe.getName());
        System.out.println("Changing joe's name to joseph.");
        joe.setName("Joseph");
        System.out.println("joe's record is as follows: ");
        System.out.println(joe);
    }
}

最后,我尝试编译代码并且运行良好,但不幸的是,演示类没有运行,即使它在屏幕上显示“进程已完成”!你觉得这个程序有什么问题?

【问题讨论】:

  • 能不能把编译运行的命令也贴一下?
  • 在调试器中运行你的程序来跟踪它。
  • “没有运行”是什么样的?你是说消息没有写入控制台?
  • 我猜你有一个错误。
  • “甚至没有运行”是什么意思?你的意思是:“我没有看到我期望的输出”?

标签: java inheritance compilation


【解决方案1】:

问题在于HourlyEmployee 中的第一个System.exit(0)。由于在else之后没有{},所以无条件执行退出。

【讨论】:

    【解决方案2】:

    您的构造函数中不应包含 System.exit。

    您也不应该写出错误消息。抛出 IllegalArgumentException。

    你的 HourlyEmployee ctor 坏了。这就是你想要的:

    public class HourlyEmployee extends Employee {
    
        private double wageRate;
        private double hours;
    
        public HourlyEmployee() {
    
            super();
            wageRate = 0;
            hours = 0;
    
    
        }
    
        public HourlyEmployee(String na, int di, double wR, double h) {
    
            super(na, di);
            if (wR < 0) throw new IllegalArgumentException("wage rate cannot be negative");
            if (h < 0) throw new IllegalAccessError("hours cannot be negative");
            wageRate = wR;
            hours = h;
    
        }
    
        public HourlyEmployee(HourlyEmployee originalObject) {
    
            super(originalObject);
            wageRate = originalObject.wageRate;
            hours = originalObject.hours;
    
        }
    
        public double getRate() {
    
            return wageRate;
    
        }
    
        public double getHours() {
    
            return hours;
        }
    
        public double getPay() {
    
            return wageRate * hours;
    
        }
    
        public void setRate(double newWR) {
    
            if (newWR < 0) throw new IllegalArgumentException("wage rate cannot be negative");
                wageRate = newWR;
        }
    
        public String toString() {
    
            return (getName() + " " + getID() + "\n$" + wageRate + " per hour for " + hours + "hours");
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-27
      • 1970-01-01
      相关资源
      最近更新 更多