TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
一、基本概念:
① 静态多态性(编译时的特性),java中的静态多态性实现手段-----重载函数。其调用规则是依据对象在定义时的类型相应地调用对应类中的重载函数
② 动态多态性(运行时的特性),Java中的动态多态性实现手段---覆盖(替换)基类中的同名成员函数(函数原型一致)。 其调用规则是依据对象在实例化时而非定义时的类型相应地调用对应类中的同名成员函数。
③ 父类与子类对象编程规则(赋值兼容原则):子类的对象可当着父类的对象来使用。
二、应用实例:
- class Base{
- public void fun(){
- System.out.println("Base Class fun()");
- }
- public void fun(int X){
- System.out.println("Base Class fun(int x)");
- }
- }
- public class Derived extends Base{
- public void fun(int x,int y){
- System.out.println("Derived Class fun(int x,int y)");
- }
- public void fun(){
- System.out.println("Derived Class fun()");
- }
- public static void main(String[] args){
- Base obj=new Base();
- obj.fun(); //调用基类中的fun()
- // obj.fun(1,2); 错误
- obj=new Derived();
- obj.fun(); //调用派生类中的fun()
- obj.fun(1); //调用基类中的fun(int x)
- // obj.fun(1,2); 错误
- Derived ObjD=new Derived();
- ObjD.fun(1,2); //调用派生类中的fun(int x,int y)
- ObjD.fun(); //调用派生类中的fun()
- ObjD.fun(1); //因派生类中未定义出,则调用从基类中继承类来的
- }
- }
- 三、继承与派生时的方法重写权限要求:重写的方法的访问权限不能有比基类更严格
- 的访问权限和定义出更多的例外。
- 例如: class Base{
- public void fun() throws IOException{
- }
- }
复制代码
class Derived extends Base{
//错误! 重写的方法的访问权限不能有比基类更严格的访问权限和更多的例外定义
private void fun() throws IOException,InterruptedException{
}
}
- 考考你(10):
- What is the output of the following program?
- 1. class Base {
- 2. int x=3;
- 3. public Base() {}
- 4. public void show() {
- 5. System.out.print(" The value is " +x);
- 6. }
- 7. }
- 8. class Derived extends Base {
- 9. int x=2;
- 10. public Derived() {}
- 11. public void show() {
- 12. System.out.print(" The value is " +x);
- 13. }
- 14. }
- 15. public class Test {
- 16. public static void main(String args[]) {
- 17. Base b = new Derived();
- 18. b.show();
- 19. System.out.println("The value is "+b.x);
- 20. }
- 21. } // end of class Test
- a. The value is 3 The value is 2
- b. The value is 2 The value is 3
- c. The value is 3 The value is 3
- d.The value is 2 The value is 2
- [url=../pr/answer.jsp]点击观看答案[/url]
复制代码
function TempSave(ElementID)
{
CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value);
CommentsPersistDiv.save("CommentXMLStore");
}
function Restore(ElementID)
{
CommentsPersistDiv.load("CommentXMLStore");
document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent");
} |
|