java中静态方法和非静态方法覆盖的区别
来源:互联网 发布:sql查询某一天的数据 编辑:程序博客网 时间:2024/05/02 21:31
首先我们提供两个类,基类为Parent,派生类为Child。在Parent中我们提供两个方法,一个是静态方法staticMethod(),一个是非静态方法nonStaticMethod()。在Child类中我们覆盖着两个方法。
在Test类中我们分别使用Parent p1 = new Parent(),Parent p2 = new Child(),Child c = new Child() 得到三个实例,并分别调用静态方法和非静态方法,我们来看程序的运行结果在Test类中我们分别使用Parent p1 = new Parent(),Parent p2 = new Child(),Child c = new Child()得到三个实例,并分别调用静态方法和非静态方法,我们来看程序的运行结果
程序的运行结果为:
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 Calle
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 的非静态方法,为什么呢?原因是静态方法是在编译的时候把静态方法和类的引用类型进行匹配,而不是在运行的时候和类引用进行匹配。因此我们得出结论:当我们在子类中创建的静态方法,它并不会覆盖父类中相同名字的静态方法。
- java中静态方法和非静态方法覆盖的区别
- java中覆盖静态方法和非静态方法的区别
- java中静态方法和非静态方法覆盖的区别
- java中静态方法和非静态方法覆盖的区别
- java中静态方法和非静态方法覆盖的区别
- Java中静态方法和非静态方法的区别
- java中静态方法和非静态方法的区别
- java静态方法与非静态方法 覆盖的区别
- 实例说明java中覆盖静态方法和非静态方法的不同
- 实例说明java中覆盖静态方法和非静态方法的不同
- 实例说明java中覆盖静态方法和非静态方法的不同
- Java 中 静态方法与非静态方法的区别
- Java中静态方法与非静态方法的区别
- Java 中 静态方法与非静态方法的区别
- Java中静态方法与非静态方法的区别
- C#中静态方法和非静态方法的区别
- 覆盖静态方法与非静态方法的区别
- 覆盖静态方法与非静态方法的区别
- mysql 横表和纵表转换
- 20140905
- Android开发————与Service进行交互
- oracle pls-00201 问题
- 创建linux 内核函数man手册
- java中静态方法和非静态方法覆盖的区别
- 面向对象程序设计 第六次实验参考代码
- Ubuntu 14.04下Qt 5.3.1 使用fcitx 输入法输入中文的问题
- Android中尺寸单位
- POJ 2141 Message Decowding(简单题)
- android sdk--- 发送短信实例学习
- Linux内核编译
- x的值递增
- LeetCode---2.Evaluate Reverse Polish Notation