静态方法隐藏、重写、继承
来源:互联网 发布:手机数据恢复破解版 编辑:程序博客网 时间:2024/05/15 23:47
静态方法可以通过二种形式来调用,一种是类名加方法名,另一种是类引用加方法名.
上述程序中通过第二种方式来调用静态方法,其实质是检查引用的类型来调用静态方法(即类名加方法名的方式).
静态成员(方法和属性)属于类而不是属于对象,静态方法,静态属性,动态属性早在编译期就已经确定(弱弱地说,相关地址数据存储在虚拟机的方法区类数据中).
-------------------------------------------------------------------------
1. 静态方法可以被继承.
程序一:
- /**
- * Copyright (c) 2011 Trusted Software and Mobile Computing(TSMC)
- * All rights reserved.
- * Author: Jarg Yee <yeshaoting@gmail.com>
- * http://jarg.iteye.com/
- */
- import static java.lang.System.out;
- /**
- * 静态方法可以被继承 测试程序 - 父类
- */
- class SuperClass
- {
- /** static method in the super class. */
- public static void display()
- {
- out.println("in the super class.");
- }
- }
- /*
- * 静态方法可以被继承 测试程序 - 子类
- */
- public class SubClass extends SuperClass
- {
- /** for debugging. */
- public static void main(String[] args)
- {
- SubClass sub = new SubClass();
- sub.display(); // 调用子类继承父类的静态方法
- }
- }
---------- 运行Java ----------
in the super class.
输出完成 (耗时 0 秒) - 正常终止
根据输出结果: in the super class.可知:
sub.display(); 调用子类继承了父类的静态方法.
由此可见: 子类可以继承父类的静态方法.
2. 静态方法被隐藏不能被重写.
程序二:
- /**
- * Copyright (c) 2011 Trusted Software and Mobile Computing(TSMC)
- * All rights reserved.
- * Author: Jarg Yee <yeshaoting@gmail.com>
- * http://jarg.iteye.com/
- */
- import static java.lang.System.out;
- /**
- * 静态方法被隐藏不能被重写 测试程序 - 父类
- */
- class SuperClass
- {
- /** static method in the super class. */
- public static void display()
- {
- out.println("in the super class.");
- }
- }
- /*
- * 静态方法被隐藏不能被重写 测试程序 - 子类
- */
- public class SubClass extends SuperClass
- {
- /** for debugging. */
- public static void main(String[] args)
- {
- SubClass sub = new SubClass(); // 上转型对象
- sub.display(); // 调用子类自己的静态方法
- SuperClass sup = new SubClass(); // 子类对象
- sup.display(); // 用被隐藏的父类静态方法
- }
- /** static method in the super class. */
- public static void display()
- {
- out.println("in the sub class.");
- }
- }
---------- 运行Java ----------
in the sub class.
in the super class.
输出完成 (耗时 0 秒) - 正常终止
对于动态方法,上转型对象调用的将会是子类中重写父类的动态方法.
而对于此处的静态方法,父类子类中都含有静态方法display()的情况,上转型对象却调用的父类的静态方法display(),而非子类中定义的静态方法display().
由此可见: 子类并没有重写父类的静态方法,而是将父类的静态方法隐藏,隐藏的父类方法可以通过父类类名在子类中被调用.
当子类中重写的方法也是静态的时候,其实质是将父类的静态方法隐藏,而并非是将其重写.(摘自: Java SE 6.0编程指南 P142)
隐藏和重写的区别在于:
隐藏是根据引用的类型来进行调用.
重写是根据对象的类型来进行调用.
(摘自: Java SE 6.0编程指南 P143)
3. 动态方法不能覆盖静态方法
上述程序二,如果去掉子类方法display()的static修饰,则会报如下错误:
---------- 编译Java ----------
C:\Documents and Settings\Administrator\桌面\test\SubClass.java:38: SubClass 中的 display() 无法覆盖 SuperClass 中的 display();被覆盖的方法为 static
public void display()
^
1 错误
输出完成 (耗时 1 秒) - 正常终止
说明: 子类中的动态方法display()无法覆盖父类中的静态方法display().
4. 静态方法不能覆盖动态方法
上述程序二,如果去掉父类方法display()的static修饰,则会报如下错误:
---------- 编译Java ----------
C:\Documents and Settings\Administrator\桌面\test\SubClass.java:38: SubClass 中的 display() 无法覆盖 SuperClass 中的 display();覆盖的方法为静态
public static void display()
^
1 错误
输出完成 (耗时 1 秒) - 正常终止
说明: 子类中的静态方法display()无法覆盖父类中的动态方法display().
疑惑:
动态方法不能覆盖静态方法.
这是否意味着加了static修饰的子类动态方法display()(即静态方法)是在对父类的静态方法display()进行覆盖呢?
难道说这里面的覆盖不是重写的意思,而是隐藏的意思?
(对于静态方法,能继承,但没有重写(覆盖)的概念,重写的概念是对于非static方法)
- 静态方法隐藏、重写、继承
- Java中static静态类和静态方法隐藏、重写、继承
- static的继承:静态方法只能隐藏(hidden),不能重写(override)
- 静态属性和静态方法可以被继承,但没有被重写(overwrite)而是被隐藏
- 静态(static)方法的继承和重写
- 静态(static)方法的继承和重写
- 静态(static)方法的继承和重写
- 静态(static)方法的继承和重写
- JAVA,假重写,方法假重写,继承父类时的静态方法假重写,静态方法假重写
- 静态方法只能被继承,而不能被重写
- 静态方法只能被继承不能被重写--详解
- 【转载】静态(static)方法的继承和重写
- java 静态(static)方法的继承和重写
- C#: 继承,重写与隐藏
- Java中为什么静态方法不能被重写?为什么静态方法不能隐藏实例方法?
- 笔记整理 静态与非静态 继承哪一些 继承链 重写 属性方法重载 PDO
- 继承&&方法重写
- 继承·方法重写
- poj 1258 Agri-Net 水题三连发。。。又是kruscal
- 多线程编程中的条件变量和虚假唤醒(spurious wakeup)
- Oracle JDBC内存管理--Oracle白皮书2009年8月 .
- Stringfomat
- Linux平台下java程序员的基本功(一)
- 静态方法隐藏、重写、继承
- Android设计模式系列(0)--开篇
- 搭建Linux0.11系统环境
- Android防止反编译
- linux网络编程之System V 消息队列(一):消息队列内核结构和msgget、msgctl 函数
- TypeError: 'dict_keys' object does not support indexing
- Android设计模式系列(1)--SDK源码之组合模式
- Android反编译拿来主义方法
- mysql批量插入数据方法