2015-01-20 3 views
0

다음과 같이 하나의 메서드 만 포함하는 인터페이스를 구현하는 수퍼 클래스 (Employee)가 어디에 있는지 질문이 있습니다.하위 클래스의 메서드 호출

public interface Payable 
{  
    double getPaymentAmount(); // calculate payment; no implementation 
} 

나는 방법 실적을 포함, 각각의 직원 (예를 들어 SalariedEmployee, HourlyEmployee, CommissionEmployee)에서 상속 서브 클래스의 수를 가지고있다. 내가 지급 인터페이스를 구현 및 방법 실적을 호출하는 방법 getPaymentAmount을 선언합니다. 방법 getPaymentAmount가 다음 직원 계층 구조에서 하위 클래스에 의해 상속 될하기 위해 "급 직원을 수정할 수 있습니다하도록 요청했습니다

. getPaymentAmount은 특정 서브 클래스를 호출 할 때 객체를 사용하면 해당 하위 클래스에 대해 적절한 수익 방식을 다형성 방식으로 호출합니다. "

아무도 도와 줄 수 있습니까? 하위 클래스를 편집하지 않고 Employee 클래스 메소드 getPaymentAmount에서 관련 수익 계산 방법을 어떻게 호출합니까?

은 정말 모든 도움 감사 자바에 상대 안돼서

다음과 같이 Employee 클래스의 관련 부분은 다음과 같습니다

public abstract class Employee implements Payable 
{ 
    private String firstName; 
    private String lastName; 
    private String socialSecurityNumber; 

    // three-argument constructor 
    public Employee(String first, String last, String ssn) 
    { 
     firstName = first; 
     lastName = last; 
     socialSecurityNumber = ssn; 
    } // end three-argument Employee constructor 
    //getters, settters, toString override etc have been deleted. 
    public double getPaymentAmount() 
    { 
     ???? //This is what I need help with.   
    } 

} // end abstract class Employee 

과 서브 클래스의 1 예 복용 :

public class SalariedEmployee extends Employee 
{ 

    private double weeklySalary; 

    // four-argument constructor 
    public SalariedEmployee(String first, String last, String ssn, double salary) 
    { 
     super(first, last, ssn); // pass to Employee constructor 
     setWeeklySalary(salary); // validate and store salary 
    } // end four-argument SalariedEmployee constructor 

    @Override 
    public double earnings() 
    { 
     return getWeeklySalary(); 
    } // end method earnings 

} // end class SalariedEmployee 
+1

무엇을 도와 드릴까요? _specifically_? –

답변

0

어쩌면 이것이 당신이 원하는 것입니까? 다형성 (polymorphic) 동작을 구현하려면 다양한 Employee 클래스가 earnings()/getPaymentAmount()에 대해 다른 구현을 갖도록하십시오. 이렇게하면 메서드가 수퍼 클래스를 재정 의하여 다형성을 달성하게됩니다.

class Employee implements Payable 
{ 
    double getPaymentAmount(){ 
     return earnings(); 
    } 
    double earnings(){ 
     //Your other implementations 
     return xxx; 
    } 
} 

class SalariedEmployee extends Employee 
{ 
    double getPaymentAmount(){ 
     return earnings(); 
    } 
    double earnings(){ 
     //Different implementation for different employee tpye 
     return xxx; 
    } 
} 

는 "어떻게 내가 Employee 클래스 방법 getPaymentAmount 실적에 대한 관련 메소드를 호출합니까?"

걱정할 필요가 없습니다. 자바가 당신을 위해 그것을 돌볼 것입니다. 그것은 다형성의 기본입니다. 객체 클래스에 따라 각각의 메서드를 호출합니다. 그들이 알고 있기 때문에

당신이 abstract 방법은 earnings라고 선언 한 이후
abstract class Employee implements Payable 
{ 
    double getPaymentAmount(){ 
     return earnings(); 
    } 

    abstract double earnings(); 
} 

class SalariedEmployee extends Employee 
{ 

    double earnings(){ 
     //Different implementation for different employee tpye 
     return xxx; 
    } 
} 

abstract class의 다른 방법이 그 방법을 호출 할 수 있습니다

+0

안녕하세요, 도움 주셔서 감사합니다. 위의 내용에서 볼 수있는 유일한 문제는 하위 클래스 (예 : SalariedEmployee)를 편집하는 것입니다.이 하위 클래스는 우리가하지 말라고 명령 한 것입니다! 각 하위 클래스에는 이미 earnings() 메서드가 구현되어 있습니다. 예 : @Override 공개 이중 수입() { return getWeeklySalary(); } // end method earnings – user3600952

+0

@ user3600952 당신이 가지고있는 것의 모든 것을 게시하지 않는 한, 그게 내가 할 수있는 전부입니다. 나는 수입()이 처음에 어디에 위치해 있는지/그것이 어디에 있어야 하는지를 모른다. – user3437460

+0

안녕하세요, 도움을 주셔서 감사합니다. 위의 원래 게시물에 코드를 추가했습니다. – user3600952

-1
Employee.java 
======================================================================= 
interface Payable 
{ 
    double getPaymentAmount(); // calculate payment; no implementation 
} 

public abstract class Employee implements Payable{ 
    public double getPaymentAmount() 
    { 
     return 0.0; 
    } 

    public void printSalary() 
    { 

    } 
} 


Teacher.java 
======================================================================= 
public class Teachers extends Employee { 
    public double getPaymentAmount() 
    { 
     return 5; 
    } 

    public void printSalary() 
    { 
     System.out.println("Teachers current salary is: " + getPaymentAmount()); 
    } 

} 

SoftwareEngineer.java 
======================================================================= 
public class SoftwareEngineer extends Employee { 
    public double getPaymentAmount() 
    { 
     return 500; 
    } 

    public void printSalary() 
    { 
     System.out.println("Software Engineers current salary is: " + getPaymentAmount()); 
    } 

} 

TestEmployee.java 
======================================================================= 
public class TestEmployee { 
    public static void main(String s[]) 
    { 
     Employee e = new Teachers(); 
     e.printSalary(); 

     Employee e1 = new SoftwareEngineer(); 
     e1.printSalary(); 
    } 
} 
0

난 당신이 찾고있는 무슨 생각이 같은 것입니다 Employee의 인스턴스화 된 인스턴스에는 구현 된 earnings 메소드가 있어야합니다.

+0

그게 내가 찾고 있던 바로 그거야! 건배 다니엘! – user3600952

관련 문제