Advertisement
TS10315016

practice extends

Apr 26th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. public class 練習繼承
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         Undergrad me = new Undergrad();
  6.         Student t1 = me;
  7.         Person t2 = me;
  8.         me.speak();
  9.         me.getCode(20);
  10.         me.getCode();
  11.         System.out.println(me.code);
  12.        
  13.         t1.speak();
  14.         t1.getCode(40);
  15.         t1.getCode();
  16.         System.out.println(t1.code);
  17.        
  18.         t2.speak();
  19.         t2.getCode(60);
  20.         t2.getCode();
  21.         System.out.print(t2.code);
  22.  
  23.     }
  24.  
  25. }
  26.  
  27.  
  28. class Person
  29. {
  30.     public String code = "Person Code";
  31.     protected double getCode(int c)
  32.     {
  33.         System.out.println(code+":"+c);
  34.         return c;
  35.     }
  36.     public void speak()
  37.     {
  38.         System.out.println("I'm a Person!");
  39.     }
  40. }
  41.  
  42. class Student extends Person
  43. {
  44.     Student()
  45.     {
  46.         System.out.println("I'm a Student!");
  47.     }
  48.     public String code = "Student code";
  49.     public double getCode(int c)
  50.     {
  51.         System.out.println(code+":"+c);
  52.         return c;
  53.     }
  54.    
  55.     public double getCode()
  56.     {
  57.         System.out.println("Student Code");
  58.         return 0.0;
  59.     }
  60. }
  61.  
  62. class Undergrad extends Student
  63. {
  64.     public double getCode()
  65.     {
  66.         System.out.println("Student Code");
  67.         return 0.0;
  68.     }
  69.    
  70.     public void speak()
  71.     {
  72.         System.out.println("I'm an Undergrad!");
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement