程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

为什么java程序可以编译但不能运行?

发布于2021-09-09 13:31     阅读(1249)     评论(0)     点赞(20)     收藏(1)


我有一个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);
    }
}

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


解决方案


问题出System.exit(0)HourlyEmployee. 由于没有{}之后else,退出是无条件执行的。



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.javaheidong.com/blog/article/283612/233563d0c29fd7b0df7e/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

20 0
收藏该文
已收藏

评论内容:(最多支持255个字符)