错误:课堂上的私人访问

时间:2015-02-13 06:17:55

标签: java class

public class ProductionWorker extends Employee
{
    private int shift;
    private double rateOfPay;
    private double hoursWorked;

    ProductionWorker(String name, int id, int shift, double rateOfPay, double hoursWorked)
    {
        super(name, id);
        this.shift = shift;
        this.rateOfPay = rateOfPay;
        this.hoursWorked = hoursWorked;
    }



public class TeamLeader extends ProductionWorker
{
    private double monthlyBonus;

    TeamLeader(String name,int id, int shift, double rateOfPay, double hoursWorked, double monthlyBonus)
    {
        super(name, id , shift, rateOfPay, hoursWorked);
        this.monthlyBonus = monthlyBonus;

    }

    public double calcPay()
    {
        double pay = 0;
        //night shift
        if (shift == 2)
        {
            pay = ((hoursWorked + hoursWorked / 2) * rateOfPay) + monthlyBonus;
        }
        else
        {
            pay = (hoursWorked * rateOfPay) + monthlyBonus;
        }
        return pay;
    }
}

我将类ProductionWorker扩展到TeamLeader类,然后在类TeamLeader上创建方法calcPay()我的问题是我需要从类ProductionWorker获取变量shift,hoursOfWorked和rateOfPay的值,所以我可以使用方法calcPay(我的主人。可以帮助我,提前谢谢你。

2 个答案:

答案 0 :(得分:0)

要访问父类(ProductionWorker)的变量,您需要将变量范围从private更改为protected。

私有变量仅在类中可见。 受保护的变量在子类和包中的所有类中都可见

您可以参考以下链接:http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

如果您不希望将它们设置为受保护,您也可以为变量创建getter和setter,但这不是首选方法。

例如getter和setter:

private int shift;

protected int getShift() {
    return this.shift;
}

protected void setShift(int shift) {
     this.shift = shift;
}

答案 1 :(得分:0)

因为您的字段是私有的,这意味着除非您创建一个方法来返回实例化Object时要查找的特定值,否则您的其他类或其方法都不能访问该数据,因此它是私有的。

你需要做的是创造" getter" ProductionWorker课程中允许您在TeamLeader课程中说出类似内容的方法:

shift = ProductionWorker.getShift();

这些方法是专门设计的,因此如果您需要从不同的类中查看Object的字段,您只需创建对象并使用其方法返回值。以下是这些方法的样子:

public int getShift()
{
    return shift;
}

public double getHoursWorked()
{
    return hoursWorked;
}

public double getRateOfPay()
{
    return rateOfPay;
}

通过将这些公共方法放在您的超类中,您将能够在TeamLeader子类中创建ProductionWorker并使用其方法来访问实例变量。

更新
以下是一些可能适用于您的事业的示例代码:

public class ProductionWorker extends Employee
{
    private int shift;
    private double rateOfPay;
    private double hoursWorked;

    ProductionWorker(String name, int id, int shift, double rateOfPay, double hoursWorked)
    {
        super(name, id);
        this.shift = shift;
        this.rateOfPay = rateOfPay;
        this.hoursWorked = hoursWorked;
    }

    // Now we can add the "getters"
    public int getShift()
    {
        return shift;
    }

    public double getHoursWorked()
    {
        return hoursWorked;
    }

    public double getRateOfPay()
    {
        return rateOfPay;
    }



public class TeamLeader extends ProductionWorker
{
    ProductionWorker prodWorker = new ProductionWorker(Jared, 10046, 2, 8.50, 12.6); // Creates a production worker you can reference

    private int shift = prodWorker.getShift();
    private double rateOfPay = prodWorker.getRateOfPay();
    private double hoursWorked = prodWorker.getRateOfPay();
    // These three statements demonstrate the getters and how they can be implemented

    private double monthlyBonus;


    TeamLeader(String name,int id, int shift, double rateOfPay, double hoursWorked, double monthlyBonus)
    {
        super(name, id , shift, rateOfPay, hoursWorked);
        this.monthlyBonus = monthlyBonus;

    }

    public double calcPay()
    {
        double pay = 0;
        //night shift
        if (shift == 2)
        {
            pay = ((hoursWorked + hoursWorked / 2) * rateOfPay) + monthlyBonus;
        }
        else
        {
            pay = (hoursWorked * rateOfPay) + monthlyBonus;
        }
    return pay;
    }
}

请记住,这可能不是整个项目的完美解决方案,因为我无法看到您正在扩展的其他超类或客户端应该使用此代码执行的操作。此示例代码旨在让您了解getter如何在创建对象的类中工作并演示如何使用它们。我肯定会保留我为你设定的吸气剂。

另外
确保对于Object的每个实例变量,每个变量都应该有一个getter。您会发现可能需要不时访问该数据以在客户端进行计算。