面向對象--簡單多態例子

package polymorphic;

public class lzi {

    public static void main(String[] args) {
        
        peration f=new peration();//操作學生類對象
        f.OperatingStudents(new Lee("李四","18","男"));
        f.OperatingStudents(new wolf("張三","18","男"));
    }    
/*
 描述計算機14應用技術504班的同學個人信息外   還有其他人的愛好
 他們除了學習專業知識 外,還有其他同學的愛好不同\籃球\足球\打遊戲\....
1. 李氏
2.張三
3.過一段時間 來了一位新同學
 
 */    

}
class application{//14應用技術504班
    
    String name;//姓名
    String Age;//年齡
    String  Gender;//性別
public application(String name,String Age,String  Gender){
        this.name=name;
        this.Age=Age;
        this.Gender=Gender;
    }
public final  void Study(){
//final 強制這個函數不能複寫 因爲是:所有學生都學習知識都同樣 固定的 !不能改了
System.out.println("姓名:"+name+"  "+"年齡:"+Age+"  "+"性別:"+ Gender+"  "+"學習專業知識");
 }

public void Basketbal() {
    System.out.println("愛好  打籃球");
}

public    void  PlayGames(){
    System.out.println("愛好  打遊戲");
}

public    void  FootbaLL(){
    System.out.println("愛好  踢足球");
}
}
//------------------------------------------


class peration{//操作學生的工具類
public void OperatingStudents(application f){
    f.Study();
    if(f  instanceof Lee){
        f.PlayGames();//愛好打遊戲
    }else if(f  instanceof wolf){
        f.FootbaLL();//愛好踢足球
        
    }        
    }
}

class Lee  extends application implements Game{
//李氏類繼承 14應用技術504班這個類
    
public Lee(String name, String Age, String Gender) {
super(name, Age, Gender);//訪問父類
}  
  }
 
class  wolf  extends application  implements Football{
//張三類繼承 14應用技術504班這個類
    public wolf(String name, String Age, String Gender) {
        super(name, Age, Gender);//訪問父類的變量
        
    }
    
}
class NewStudents{
    
}
//------------愛好  接口擴展學生信息-------------------
interface  Basketball{//愛好打籃球
    
    public     abstract void Basketbal();
}

interface Game{//愛好打遊戲
    public  abstract void  PlayGames();
}

interface Football{//愛好是足球
    public abstract    void  FootbaLL();
}
//------------結束--------------------------------

/*

結果輸出;

姓名:李四  年齡:18  性別:男  學習專業知識
愛好  打遊戲
姓名:張三  年齡:18  性別:男  學習專業知識
愛好  踢足球


*/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章