java中静态方法和非静态方法覆盖的区别
2011-09-18 09:12
489 查看
昨天晚上看书的时候,发现静态方法与非静态方法还是没弄清楚。
早上起来上网查了下资料。以下是自己转载了网络资料,当中代码部分是自己再次测试的。在这里与大家一起分享。
----------------------------分割线------------------------------
首先我们提供两个类,基类为Parent,派生类为Child。在Parent中我们提供两个方法,一个是静态方法staticMethod(),一个是非静态方法nonStaticMethod()。在Child类中我们覆盖着两个方法。
java 代码:
-----------------------------分割线-------------------------------
在Tes1t类中我们分别使用Parent p1 = new Parent(),Parent p2 = new Child(),Child c = new Child()得到三个实例,并分别调用静态方法和非静态方法,我们来看程序的运行结果 。
java 代码:
程序的运行结果为:
E:\Stu\java\myjava>java Test1
Parent.static:Parent's static Method is Called
p1.static:Parent's static Method is Called
p2.static:Parent's static Method is Called
========================
p1.nonStatic:Parent's Non-Static Method is Called
p2.nonStatic:Child's non-static method is called
Child.static:Child's static method is called
c.static:Child's static method is called
c.nonStatic:Child's non-static method is called
值得注意的是p2实际上是一个Child的类型的引用,然而在调用静态方法的时候,它执行的却是父类的静态方法,而不是Child的静态方法,而调用 p2的非静态方法的时候执行的是Child的非静态方法,为什么呢?原因是静态方法是在编译的时候把静态方法和类的引用类型进行匹配,而不是在运行的时候和类引用进行匹配。因此我们得出结论:当我们在子类中创建的静态方法,它并不会覆盖父类中相同名字的静态方法。
Test1类中我们分别使用Parent p1 = new Parent(),Parent p2 = new Child(),Child c = new Child()得到三个实例,并分别调用静态方法和非静态方法,我们来看程序的运行结果。
早上起来上网查了下资料。以下是自己转载了网络资料,当中代码部分是自己再次测试的。在这里与大家一起分享。
----------------------------分割线------------------------------
首先我们提供两个类,基类为Parent,派生类为Child。在Parent中我们提供两个方法,一个是静态方法staticMethod(),一个是非静态方法nonStaticMethod()。在Child类中我们覆盖着两个方法。
java 代码:
class Parent { public void nonStaticMethod() { System.out.println("Parent's Non-Static Method is Called"); } public static void staticMethod() { System.out.println("Parent's static Method is Called"); } } class Child extends Parent { public void nonStaticMethod() { System.out.println("Child's non-static method is called"); } public static void staticMethod() { System.out.println("Child's static method is called"); } }
-----------------------------分割线-------------------------------
在Tes1t类中我们分别使用Parent p1 = new Parent(),Parent p2 = new Child(),Child c = new Child()得到三个实例,并分别调用静态方法和非静态方法,我们来看程序的运行结果 。
java 代码:
public class Test1 { public static void main(String[] args) { Parent p1 = new Parent(); Parent p2 = new Child(); Child c = new Child(); //使用父类Parent调用staticMethod()方法。 //打印输出父类StaticMethod()方法内容 System.out.print("Parent.static:"); Parent.staticMethod(); //使用父类Parent new 出来的对象p1调用staticMethod()方法。 //打印输出父类StaticMethod()方法内容 System.out.print("p1.static:"); p1.staticMethod(); //p2 为父类引用指向子类对象,理应打印出子类重写后的方法。 //但是,由于子类定义的static方法,所以依旧打印出父类的方法内容。 System.out.print("p2.static:"); p2.staticMethod(); System.out.println("========================"); //p1调用父类中的nonStaticMethod() System.out.print("p1.nonStatic:"); p1.nonStaticMethod(); //p2 为父类引用指向子类对象,由于子类重写的方法不是静态方法。 //所以,在这里打印出的是,子类重写父类方法后的内容。 System.out.print("p2.nonStatic:"); p2.nonStaticMethod(); //利用Child父类调用子类的构造方法,staticMethod()静态方法 System.out.print("Child.static:"); Child.staticMethod(); //利用父类对象c,调用staticMethod()静态方法 System.out.print("c.static:"); c.staticMethod(); //利用父类对象c,调用nonStaticMethod()非静态方法 System.out.print("c.nonStatic:"); c.nonStaticMethod(); } }
程序的运行结果为:
E:\Stu\java\myjava>java Test1
Parent.static:Parent's static Method is Called
p1.static:Parent's static Method is Called
p2.static:Parent's static Method is Called
========================
p1.nonStatic:Parent's Non-Static Method is Called
p2.nonStatic:Child's non-static method is called
Child.static:Child's static method is called
c.static:Child's static method is called
c.nonStatic:Child's non-static method is called
值得注意的是p2实际上是一个Child的类型的引用,然而在调用静态方法的时候,它执行的却是父类的静态方法,而不是Child的静态方法,而调用 p2的非静态方法的时候执行的是Child的非静态方法,为什么呢?原因是静态方法是在编译的时候把静态方法和类的引用类型进行匹配,而不是在运行的时候和类引用进行匹配。因此我们得出结论:当我们在子类中创建的静态方法,它并不会覆盖父类中相同名字的静态方法。
Test1类中我们分别使用Parent p1 = new Parent(),Parent p2 = new Child(),Child c = new Child()得到三个实例,并分别调用静态方法和非静态方法,我们来看程序的运行结果。
相关文章推荐
- java中静态方法和非静态方法覆盖的区别
- java中静态方法和非静态方法覆盖的区别
- java中覆盖静态方法和非静态方法的区别
- java中静态方法和非静态方法的区别
- 实例说明java中覆盖静态方法和非静态方法的不同
- 实例说明java中覆盖静态方法和非静态方法的不同
- Java中synchronized用在静态方法和非静态方法上面的区别
- 【java】synchronized修饰静态方法和非静态方法的区别
- 实例说明java中覆盖静态方法和非静态方法的不同
- Java实例说明 静态方法和非静态方法的区别
- Java中静态方法和非静态方法的区别
- C#和Java 方法重写和覆盖隐藏的区别
- java学习——静态方法和非静态方法
- 浅析C#中静态方法和非静态方法的区别
- 静态方法和非静态方法的区别
- java中静态方法和非静态方法调用的一点小困扰,已解决。
- 【转】C#中静态方法和非静态方法的区别
- java java面向对象-方法重载和方法覆盖的区别
- 【转】java中静态方法和非静态方法的存储
- java中静态方法和非静态方法的存储