SCJP认证 第二章 面向对象 2.4.2重载方法(1)
2011-03-20 15:41
204 查看
重载规则:
重载方法必须改变变元列表。
重载方法可以改变返回类型。
重载方法可以改变访问修饰符。
重载方法可以声明信的更广的检验的异常。
方法能够在同一个类或者一个子类中被重载。
由于被重写的doStuff()方法没有声明异常,当IOException对编译器而言是个检验异常你可能被误导,认为IOException就是问题。但是doStuff()方法根本没有被重写!子类Bar通过修改变元列表重载了doStuff()方法,因此IOException是不存在问题的。
合法的重载
让我们来看一个要重载方法:
调用重载方法
在上面的TestAdder代码中,第一调用a.addThem(b, c) 时向该方法传递两个int,因此,调用addThem()的第一个版本——带两个int变元的版本。第二次调用a.addThem(22.5, 9.3)时,向该方法传递两个double,因此调用addThem()的两个版本——带两个double变元的重载版本。
假如有这样一个重载方法,其一个版本带Animal,另一个版本带Horse(Animal的子类) 。如果在该方法调用中传递一个Horse对象,将调用带Horse的重载版本。先来看一下以下代码:
输出结果:
in the Animal version
in the Horse version
但是,如果使用对Horse对象的Animal引用,情况如何?
代码会输出:
in the Animal version
即使在运行时实际对象是Horse而不是Animal,但是选择调用哪个重载方法并不是在运行时动态决定的。只需记住,引用类型(不是对象类型) 决定了调用哪个重载方法!
重载方法必须改变变元列表。
重载方法可以改变返回类型。
重载方法可以改变访问修饰符。
重载方法可以声明信的更广的检验的异常。
方法能够在同一个类或者一个子类中被重载。
public class Foo { public void doStuff(int y, String s){ } public void moreThings(int x){ } } class Bar extends Foo{ public void doStuff(int y, long s) throws IOException{ } }
由于被重写的doStuff()方法没有声明异常,当IOException对编译器而言是个检验异常你可能被误导,认为IOException就是问题。但是doStuff()方法根本没有被重写!子类Bar通过修改变元列表重载了doStuff()方法,因此IOException是不存在问题的。
合法的重载
让我们来看一个要重载方法:
public void changeSize(int size, String name, float pattern){} // 以下方法是changeSize()方法的合法重载: public void changeSize(int size, String name){} public int changeSize(int size, pattern){} public void changeSize(float pattern, String name) throws IOException{}
调用重载方法
class Adder{ public int addThem(int x, int y){ return x+y; } //Overload the addThem method to add doubles instead of ints public double addThem(double x, double y){ return x + y; } } public class TestAdder{ public static void main(String[] args){ Adder a = new Adder(); int b = 27; int c = 3; int result = a.addThem(b, c); // Which addThem is invoked? double doubleresult = a.addThem(22.5, 9.3);//Which addThem? } }
在上面的TestAdder代码中,第一调用a.addThem(b, c) 时向该方法传递两个int,因此,调用addThem()的第一个版本——带两个int变元的版本。第二次调用a.addThem(22.5, 9.3)时,向该方法传递两个double,因此调用addThem()的两个版本——带两个double变元的重载版本。
假如有这样一个重载方法,其一个版本带Animal,另一个版本带Horse(Animal的子类) 。如果在该方法调用中传递一个Horse对象,将调用带Horse的重载版本。先来看一下以下代码:
class Animal {} class Horse extends Animal{} class UseAnimal{ public void doStuff(Animal a){ System.out.println("In the Animal version"); } public void doStuff(Horse h){ System.out.println("In the Horse version"); } public static void main(String[] args){ UseAnimal ua = new UseAnimal(); Animal animalObj = new Animal(); Horse horseObj = new Horse(); ua.doStuff(animalObj); ua.doStuff(horseObj); } }
输出结果:
in the Animal version
in the Horse version
但是,如果使用对Horse对象的Animal引用,情况如何?
Animal animalRefToHorse = new Horse(); ua.doStuff(animalRefToHorse);
代码会输出:
in the Animal version
即使在运行时实际对象是Horse而不是Animal,但是选择调用哪个重载方法并不是在运行时动态决定的。只需记住,引用类型(不是对象类型) 决定了调用哪个重载方法!
相关文章推荐
- SCJP认证 第二章 面向对象 2.4.2 重载方法(2)
- SCJP认证 第二章 面向对象 2.1 封装
- SCJP认证 第二章 面向对象 2.2.2 HAS-A关系
- SCJP认证 第二章2.2继承、IS-A、HAS (2.1.0 继承)
- 面向对象的学习之-----魔术六个方法
- 【IOS 开发】Object - C 面向对象 - 类 , 对象 , 成员变量 , 成员方法
- 面向对象三种方法实现继承的优缺点,及改进
- C++面向对象的方法
- C语言程序设计-现代方法----示例及练习题----第二章--C语言基本概念
- Nginx用户认证配置方法详解(域名/目录)
- js面向对象,多种创建对象方法!
- PHP核心技术与最佳实践 读书笔记 第二章 面向对象的设计原则
- Java编程那些事儿61—面向对象设计方法和面向对象特性(一)
- PHP面向对象(OOP)编程完全教程:8.构造方法__construct()与析构方法__destruct()
- SCJP认证复习~1
- php面向对象与面向过程两种方法给图片添加文字水印
- SUN公司SCJP认证学习之旅
- 面向对象的开发方法(Object Oriented,OO)
- python开发学习-day07(面向对象之多态、类的方法、反射、新式类and旧式类、socket编程)
- 你必须要知道的架构知识~第二章 代码是否面向对象,要看你的继承怎么用