码迷,mamicode.com
首页 > 编程语言 > 详细

一个java覆盖的例子

时间:2016-08-10 19:20:40      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:

// 覆盖
class P{}
class Q extends P{}

class a{
static void m1(float x){ //静态方法不能被覆盖
System.out.println("father"+x);
}
void m2(float x){ //基类被子类覆盖
System.out.println("father"+x);
}

P m3(int x){
System.out.println("father");
return null;
}
}

class b extends a{
static void m1(float x){
System.out.println("son"+x);
}
void m2(float x){
System.out.println("son"+x+1);
}

Q m3(int x){ // 返回类型 是父类的子类的话 也实现覆盖 Q 是P的子类
System.out.println("son");
return null;
}
}


public class quest13{
public static void main(String[] args) {
a a1 =new b();
a1.m1(2);
a1.m2(3);
a1.m3(3);
}
}

 

输出结果:

father2.0
son3.01
son

一个java覆盖的例子

标签:

原文地址:http://www.cnblogs.com/jiangyi666/p/5757903.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!