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);


}
}
 }

阅读更多 登录后自动展开
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页