Guest User

Untitled

a guest
Sep 19th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. //基底類別(父類別)
  2. public class Otaku
  3. {
  4. //欄位
  5. public int weight, height;
  6. //建構子
  7. public Point(int weight, int height)
  8. {
  9. this.weight = x;
  10. this.height = y;
  11. }
  12. }
  13.  
  14. //衍生類別(子類別)
  15. public class IdolOtaku : Otaku{
  16. //欄位
  17. public string oshiMem;
  18. //建構子
  19. public IdolOtaku(int weight,int height,string oshiMem) :
  20. //繼承自父類別的建構子並將所需要的引數傳遞過去
  21. base(weight,height)
  22. {
  23. this.oshiMem = oshiMem;
  24. }
  25.  
  26. }
  27.  
  28. //宣告三個實體
  29. Otaku achan = new Otaku(100, 200);
  30. IdolOtaku asurin = new IdolOtaku(10, 20, "yukka");
  31.  
  32. //可以用基底類別的名稱來宣告變數
  33. //但new 後面必須使用衍生類別的名稱
  34. Otaku momoko = new IdolOtaku(10, 20, "nanase");
Add Comment
Please, Sign In to add comment