1. 程式人生 > >java中的繼承與呼叫繼承的方法

java中的繼承與呼叫繼承的方法

package test;


 class People{
int age,leg = 2,hand = 2;
protected void showPeopleMess() {
System.out.printf("%d歲,%d只腳,%d隻手\t",age,leg,hand);
}
}
 class Student extends People{
int number;
void tellNumber() {
System.out.printf("學號:%d\t",number);
}
int add(int x,int y) {
        return x+y;
 }
class UniverStudent extends Student{
int multi(int x,int y) {
return x*y;
}
}
public class Day04 {
   public void main(String[] args) {
Student zhang =new Student();
zhang.age =17;
zhang.number =100101;
zhang.showPeopleMess();
zhang.tellNumber();
int x=9,y=29;
System.out.print("會做方法:");
int result = zhang.add(x, y);
System.out.printf("%d+%d=%d\n",x,y,result);
UniverStudent geng = new UniverStudent();
geng.age = 21;
geng.number=6609;
geng.showPeopleMess();
geng.tellNumber();
System.out.print("會做方法:");
result =geng.add(x, y);
System.out.printf("%d+%d=%d\n",x,y,result);
System.out.print("會做乘法:");
result=geng.multi(x, y);
System.out.printf("%d*%d=%d\n",x,y,result);


}
}
 }