이것은 간단한 해결책이 있지만 확실하지는 않지만 Java를 처음 사용하기 때문에 해결할 수 없습니다.재정의 된 Subclass 메서드에서 Superclass 메서드를 호출하십시오.
나는 슈퍼 클래스 Pay를 확장하는 서브 클래스 Payroll을 가지고 있는데, 'calc_payroll'이라는 재정의 된 메소드를 포함하고있다. 이 메서드에서 같은 이름의 수퍼 클래스 메서드를 호출하고 우선 순위가 높은 메서드의 변수에 출력을 할당하려고합니다. 다음은
public class Payroll extends Pay
{
public double calc_Payroll()
{
double grossPay = super.calc_Payroll();
double taxAmt = tax(grossPay);
double netPay = grossPay - taxAmt;
System.out.println(grossPay);
return netPay;
}
}
계산하고 다른 서브 클래스에서 호출 할 때 총 급여를 반환하는 문제가없는 슈퍼 클래스
public double calc_Payroll()
{
double otRate = rate * 1.77;
double otHours = ttlHours - stHours;
if(stHours == 0)
{
grossPay = otHours * rate;
}
else
{
grossPay = ((stHours * rate) + (otHours * otRate));
}
System.out.println(stHours + "//" + otHours + "//" + rate);//for testing
return grossPay;
}
슈퍼 클래스 메소드의 기능에 calc_payroll 방법의 코드는 아래에있는 내 코드는하지만, 동일한 이름의 메소드에서 호출 할 때 위의 코드 (테스트를 위해 레이블을 지정 함)의 인쇄 줄은 모든 변수에 대해 0을 표시합니다.
전체 'Pay'클래스의 코드는 요청시 아래와 같습니다.
public class Pay
{
private double ttlHours;
private int stHours;
private double rate;
double grossPay = 0.0;
final double TAXL = 0.07;
final double TAXM = 0.1;
final double TAXH = 0.16;
public void SetHours(double a)
{
ttlHours = a;
}
public void SetHoursStr(int a)
{
stHours = a;
}
public void SetRate(double a)
{
rate = a;
}
public double GetHours()
{
return ttlHours;
}
public int GetStHours()
{
return stHours;
}
public double GetRate()
{
return rate;
}
public double taxRate()
{
double taxRate = 0.0;
if(grossPay <= 399.99)
{
taxRate = TAXL;
}
else if(grossPay <= 899.99)
{
taxRate = TAXM;
}
else
{
taxRate = TAXH;
}
return taxRate;
}
public double tax(double grossPay)
{
double ttlTax = 0.0;
if(grossPay < 400.00)
{
ttlTax += (grossPay * TAXL);
}
else if(grossPay < 900.00)
{
ttlTax += (grossPay * TAXM);
}
else
{
ttlTax += (grossPay * TAXH);
}
return ttlTax;
}
public double calc_Payroll()
{
double otRate = rate * 1.77;
double otHours = ttlHours - stHours;
if(stHours == 0)
{
grossPay = otHours * rate;
}
else
{
grossPay = ((stHours * rate) + (otHours * otRate));
}
System.out.println(stHours + "//" + otHours + "//" + rate);//for testing
return grossPay;
}
}
서브 클래스의 급여는 아래 초기화 변수
public class CalPayroll extends Pay
{
Payroll nPay = new Payroll();
Accept Read = new Accept();
public void AcceptPay()
{
char select = '0';
while(select != 'e' && select != 'E')
{
System.out.println("Payroll Computation \n");
System.out.print("Enter number of hours worked (00.0) <0 for Quick exit>: ");
SetHours(Read.AcceptInputDouble());
System.out.print("Enter first number of hours straight (integer or 0 to disable): ");
SetHoursStr(Read.AcceptInputInt());
System.out.print("Enter hourly rate of worker (00.00): ");
SetRate(Read.AcceptInputDouble());
Screen.ScrollScreen('=', 66, 1);
Screen.ScrollScreen(1);
displayInfo();
System.out.println("e to exit, any other letter + <Enter> to continue");
select = Read.AcceptInputChar();
}
}
public void displayInfo()
{
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
System.out.println("Gross pay is :" + currency.format(calc_Payroll()));
System.out.println("Tax is :" + percent.format(taxRate()));
System.out.println("Net pay is :" + currency.format(nPay.calc_Payroll()));
Screen.ScrollScreen(1);
}
}
내가 혼란 스러워요에 값을 할당하기 위해 사용자 입력을 허용하는 코드가 다른 코드
를 포함하지 않는다!
,로
while
루프를 교체하는 다음, 당신은 클래스의 인스턴스를 만들 때 그 값을 초기화하는 없습니다 수 있습니다. 기본적으로 모든 int는'0','float','double'은'0.0'을 얻습니다. –"같은 이름의 메서드에서 호출 할 때"확장 할 수 있습니까? –
@OusmaneMahyDiaw OP는이 메서드를 재정의하는 메서드 내에서 메서드를 호출하려고합니다. –