黑马程序员技术交流社区
标题:
关于方法覆盖的疑问
[打印本页]
作者:
马林康
时间:
2012-6-17 18:07
标题:
关于方法覆盖的疑问
本帖最后由 马林康 于 2012-6-19 16:29 编辑
毕老师视频中讲到 静态只能覆盖静态 我试了一下貌似不能覆盖i,不知是不是我理解错了 ?
1.jpg
(122.05 KB, 下载次数: 66)
下载附件
2012-6-17 18:06 上传
/*
方法重写(override)
1.子类覆盖父类,必须保证子类权限大于等同于父类权限,才可以覆盖。
*/
class ExtendsDemo
{
public static void main(String[] args)
{
Parent p1= new Parent();
p1.show();//父类5
Child c1=new Child();
c1.show();//子类4
Parent p2= new Child();
p2.show();//子类4
p2.method();//输出Parent
System.out.println(p2.a);//5
}
}
class Parent
{
public int a=5;
void show()
{
System.out.println("父类"+a);
}
static void method()
{
System.out.println("Parent");
}
}
class Child extends Parent
{
public int a =4;
void show()
{
System.out.println("子类"+a);
}
static void method()
{
System.out.println("Child");
}
}
复制代码
作者:
淡然
时间:
2012-6-17 18:10
Mark~~~~~~
作者:
曾祥彬
时间:
2012-6-17 18:17
static方法属性类级别的方法,是不可以被覆盖的。只有可能被重载。
如下:
public class Human {
public static void p(){
System.out.println("human");
}
}
复制代码
public class Person extends Human {
// @Override // 这里加上表示覆盖的注解,编译器是会报错的
public static void p(){
System.out.println("human");
}
}
复制代码
还要理解的是,覆盖的作用就是为了多态,static不属于对象所有,这样你用什么类调用具体的调用就是你所用的类,跟具体对象没关系。从这点上也说明static方法是不可以被覆盖的。
作者:
葛奎
时间:
2012-6-17 18:17
确实已经覆盖了父类的方法
写成parent s=new parent();就可以调用子类中的覆盖的方法了
Parent s=new Child();是多态
多态的特点:父类的引用指向子类的对象
静态函数以及成员变量具有其特殊性,因为它们都是在类加载的时候,就已经加载到内存里了.
Parent s=new Child();父类引用在使用方法或成员变量时,应该遵循下面步骤:
1,在父类中是不是存在该方法或者成员变量.
2,如果存在,编译通过
3,如果调用的是成员变量,只看父类中的成员变量.
4,调用的方法是静态的,即使子类重写了该方法,jvm也会优先选择调用父类的
5,一般函数子类如果有覆盖,就调用子类的
作者:
龙秋地
时间:
2012-6-17 18:19
static 的属性和方法都是不能覆盖的,因为static修饰的部分都是与对象无关的,只与类有关.
两个类的同名同参数的静态方法是分属于两个类的,各不相干,只是名字相同而已.
所以你用父类调用的,输出的就是父类的方法.
如果你仔细看一下老师的视频老师强调的是非静态绝对不能覆盖静态,而且静态覆盖静态没有意义.
有可能是老师的小mistake.或者你会意错了.
作者:
王杰
时间:
2012-6-17 18:28
本帖最后由 王杰 于 2012-6-17 18:53 编辑
首先,毕老师没有说错,
Parent p2= new Child();
看这里,注意这里是多态了。
p2.show();//子类4
这里运行的是子类的show方法;
p2.method();//输出Parent
你的p2是什么类型的?是父类的吧?!这里和没有new对象就是直接调用父类的静态方法不是一样吗?
记住一句话,编译看左边,运行看右边,句话只是适用于一般方法的情况,而对于静态方法,你就得看他到底是什么类型的!!!。
如果你要验证毕老师是不是说错了,你可以这样:
new Child().method();
也就是创建一个子类,调用覆盖了的静态方法。也可以不创建子类的对象直接用类名调用也可以。
System.out.println(p2.a);//5
class Parent
{
static int a = 1;
int b = 2;
void show()
{
System.out.println("Parent show run."+"a="+a+",b+"+b);
}
static void method()
{
System.out.println("Parent static menthod run."+"a="+a);
}
}
class Child extends Parent
{
static int a = 3;
int b = 4;
void show()
{
System.out.println("Child show run."+"a="+a+",b+"+b);
}
static void method()
{
System.out.println("Child static menthod run."+"a="+a);
}
}
public class TestClass
{
public static void main(String[] args){
Parent p = new Parent();
Child c = new Child();
Parent pc = new Child();
System.out.println("a="+p.a+",b="+p.b);
p.show();
p.method();
System.out.println("----------------------");
System.out.println("a="+c.a+",b="+c.b);
c.show();
c.method();
System.out.println("----------------------");
System.out.println("a="+pc.a+",b="+pc.b);
pc.show();
pc.method();
}
}
复制代码
运行结果:
a=1,b=2
Parent show run.a=1,b+2
Parent static menthod run.a=1
----------------------
a=3,b=4
Child show run.a=3,b+4
Child static menthod run.a=3
----------------------
a=1,b=2
Child show run.a=3,b+4
Parent static menthod run.a=1
作者:
王璐
时间:
2012-6-18 09:48
大家都知道非静态方法的继承覆盖的原则,即向上转型时从他的实例中可而已看出来,但是静态方法却不是像非静态方法那样工作,虽然静态方法可以被继承但是为什么不能覆盖呢?
原因是这样的,静态方法跟其他非静态方法不一样的区别在于静态方法不用NEW一个实例就可以调用的,即它的调用是根据他的类型来判断的,所以我总结一句话就是:静态方法的继承和覆盖是跟着引用类型的,而非静态方法是跟着实例本身的。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2