Super class 没有传递值

Super class is not passing along values

Super class 没有运行,因为当 Steelfactory class 尝试从 super class 获取数据时,输出显示那里没有注释。 寻求解决为什么会发生这种情况。 自 1998 年以来,名为 null 的工厂以 0 的速度生产汽车 我想知道这是否是因为我的 super class 错误,但我在编写它时没有遇到任何错误。或者在代码逻辑方面是否存在其他问题? 代码:

package finalsproject;

static abstract class Factory {
    //Atributes: 
    String factoryName; //The name of the factory
    int employeeAmount; // number of workers in the factory
    int producedAmount; // number of products made
    // Constructor: 
    public Factory (String ifactoryName,int iemployeeAmount,int iproducedAmount) {// Prameterized consrtuctor
        factoryName = ifactoryName;
        employeeAmount = iemployeeAmount;
        producedAmount = iproducedAmount;
    }
    //Methods: 
    public abstract String getFactoryName();
    public abstract int getEmployeeAmount();
    public abstract int getProducedAmount();
        
}
       
//The class SteelFactory must have the following specifications
//-It must implement the abstract class Factory
//Make these two classes implement the abstract class above
    
static class SteelFactory extends Factory {
    //Attributes:
    String factoryName; // Name of the factory
    int employeeAmount; // Number of workers
    int producedAmount; // number of products
    int yearCreated; // the year the factory was made
    
    //Constructor: 
    public SteelFactory (String ifactoryName,int iemployeeAmount,int iproducedAmount,int iyearCreated) {
        super ( ifactoryName, iemployeeAmount,iproducedAmount);
        yearCreated = iyearCreated;
    }
    // Methods
    public String getFactoryName() {
        return (factoryName);
    }
    public int getEmployeeAmount() {
        return (employeeAmount);
        
    }
    public int getProducedAmount() {
        return (producedAmount);
    }
    public String toString () {
        return ("The Factory called " + factoryName + " with " + employeeAmount + "makes cars at a rate of " + producedAmount + "since the year "+ yearCreated);
    }
    
}   

您只需从 SteelFactory class 中删除重复的变量 factoryNameemployeeAmountproducedAmount 即可解决此问题,否则 class 将使用从未初始化的局部变量,而不是来自超级 class 的正确变量。扩展 class 的一个重要原因是我们不必 re-use/re-type 多个 class 中的相同变量和方法。

此外,不要忘记使用 @Override 注释,它可以帮助您跟踪扩展了哪些方法和 helps prevent common mistakes.

工作代码如下:

    static class SteelFactory extends Factory {
        //Attributes:
        int yearCreated; // the year the factory was made
        
        //Constructor: 
        public SteelFactory (String ifactoryName,int iemployeeAmount,int iproducedAmount,int iyearCreated) {
            super ( ifactoryName, iemployeeAmount,iproducedAmount);
            yearCreated = iyearCreated;
        }
        // Methods
        @Override
        public String getFactoryName() {
            return (factoryName);
        }
        @Override
        public int getEmployeeAmount() {
            return (employeeAmount);
        }
        @Override
        public int getProducedAmount() {
            return (producedAmount);
        }
        @Override
        public String toString () {
            return ("The Factory called " + factoryName + " with " + employeeAmount + "makes cars at a rate of " + producedAmount + "since the year "+ yearCreated);
        }
    }   

然后只需使用:

SteelFactory test = new SteelFactory("Test", 78, 26, 2022);
System.out.println(test.toString());

将正确打印以下结果(您需要修正格式以包含空格):

The Factory called Test with 78makes cars at a rate of 26since the year 2022