2016-08-23 53 views
0
package inheritencelatest; 
public class InheritenceLatest { 

    public static void main(String[] args) { 

     person p1=new person(12,"ekaf","atlanta","male"); 
     employe e1=new employe(23,"ekaf","atlanta","male",12000);  

     e1.ageCategory(); 
     e1.genderIdentity(); 
     e1.display(); 
     ((employe)p1).display(); 
     } 

} 

class person { 
     int age; 
     String name; 
     String Address; 
     String ageGroup; 
     protected String gender; 
     private String canNotBeAccesed; //cant be accessed by subclass  

     person(int age,String name,String Address,String gender) { 
      this.age=age; 
      this.name=name; 
      this.Address=Address; 
      this.gender=gender; 
     } 

     String ageCategory() { 
      if(age<10) 
      return("kid"); 
      else if(age<20 &&age>10) 
      return("Teen"); 
      else if(age<50&&age>20) 
      return("adult"); 
      else 
      return("senior"); 
     } 

    protected void genderIdentity() { 
     if("male".equals(gender)) 
     System.out.println("male"); 
     else if(gender=="female") 
     System.out.println("female"); 
     else 
     System.out.println("Special"); 
    } 

} 


class employe extends person { 
    double salary; 

    employe(int age,String name,String Address,String gender,int salary) { 
     super(age,name,Address,gender); 
     this.salary=salary; 
     this.name=name; 
    } 

    @Override 
    protected void genderIdentity() { 
     if(gender=="male") 
      salary=salary; 
     if(gender=="female") 
      salary=1.5*salary; 
     else 
      salary=2*salary;  
    } 

    void display() { 
     System.out.println(age +" " +name+" "+Address+" "+salary+" "+gender+ " "+ageGroup); 
    } 

}這個繼承的java代碼有什麼錯誤?

當我運行這段代碼我得到輸出:

男性
23 ekaf atlanat 24000.0男性空

異常在線程 「主要」 java.lang.ClassCastException: inheritencelatest.person無法轉換爲inheritencelatest.employe繼承權限.InheritenceLatest.main(InheritenceLatest.java:17)

我,懷疑是

如何薪水變成24000?爲什麼agecategory爲null,在我編寫e1.agecategory時沒有反對e1獲得ageGroup的值?爲什麼((employe)p1).display();正在引發異常?

可以使用什麼
person p2 = new employe(23,「ekaf」,「atlanat」,「male」,12000);?

+1

「p1」是一個「人」,而不是「僱主」。 – tkausl

+1

當您發佈代碼時,請更加努力地設置您的代碼格式。這一切都在這個時刻 - 這不像一個*最小*的例子來證明問題。 –

+0

你也應該閱讀閱讀http://stackoverflow.com/questions/513832 –

回答

2

工資如何變成24000?

在你的僱工類的genderIdentity()方法,您可以使用==比較gender"male"。這種情況是錯誤的,因爲gender"male"是不同的字符串對象(即使它們具有相同的內容/文本)。您應該使用.equals()來比較兩個字符串。現在,工資增加了一倍,因爲gender == "male"gender == "female"都不會返回true。

爲什麼agecategory爲null沒有對象e1在我編寫e1.agecategory時獲取年齡類別?

當您編寫e1.agecategory()時,您可以調用名爲agecategory()的方法。這個方法只返回一個String對象,但實際上並沒有做任何其他的事情。您沒有任何地方的任何代碼,賦值給變量agegroup(這將有看起來像agegroup = <something>;,所以變量保持的null它的默認值。

爲什麼((僱工)P1)。顯示();?是提高例外

這是因爲,與(employe)p1,您嘗試p1對象強制轉換爲employe型這不能做,因爲p1才被定義爲一個person,而不是employe。另一種方式可以正常工作,因爲通過寫作ng class employe extends person,每employe也被稱爲person

什麼可能是 的使用人p2 =新僱員(23,「ekaf」,「atlanat」,「男」,12000);

,因爲你正在創建一個變量p2可以引用任何類型的person,並且不侷限於僅參考employe對象這樣做可能是有用的。例如,在希望能夠處理任何種類的person的情況下,這非常有用。 Java仍然記得,這個特定的人是一名僱員,所以你可以稍後將它轉換爲更具體的employe類型,並調用employe特定的方法。

+0

非常感謝你的答案。它的工作和幫助了我很多:) – ekaf