版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、thinking in java学习笔记 1 全面解 读方法重 载分类: java 2011-11-06 21:44 92 人阅读 评论 (1) 收藏 举报 之前学习C+时对于方法重载有了一点认识,最近看了 java编程思想一书,对书中方法重载 的章节进行了一番研 习发现之前对于方法重载的认识很浅薄很片面,所以在此 总结下最近关 于方法重载的学习结果,希望能比 较全面的解读下方法重载。一,方法重 载 的基本 认识 :学习之前我 们需要了解什么是方法重 载,方法名相同而参数不同的方法既是方法重 载, 简单 而言就是 让类 以统一的方式 处理不同 类型数据的一种方法。 举个例子就能直 观的看出来了
2、, 例如:void show() ; void show(String s) 例中定义了名字同 为show的两个方法,可以看出他 们的参数不同, 这即为方法重 载。那么方法重 载有什么作用呢?前面我 们已经解读过 构造器,有 时程序中需要用到参数不同而 方法名相同的构造器, 如果直接定 义那么就会 产生错误,此时方法重 载就发挥 了很大的作用, 计算机可以通 过方法重载中传递的参数找到适合的构造器 进行调用,从而 实现功能。下面给出个简单的例子加深下 认识。java view plaincopyprint?class Tree int height;Tree() prt("Plant
3、ing a seedling"); height = 0;Tree(int i) prt("Creating new Tree that is "+ i + " feet tall");height = i;void info() prt("Tree is " + height+ " feet tall");void info(String s) prt(s + ": Tree is "+ height + " feet tall");static void prt
4、(String s) System.out.println(s);public class Overloading public static void main(String args) for(int i = 0; i < 5; i+) Tree t = new Tree(i);(); ("overloaded method");/ Overloaded constructor:new Tree(); /*Output:Creating new Tree that is 0 feet tallTree is 0 feet tallOverl
5、oading method: Tree is 0 feet tall Creating new Tree that is 1 feet tall Tree is 1 feet tallOverloading method: Tree is 1 feet tall Creating new Tree that is 2 feet tall Tree is 2 feet tallOverloading method: Tree is 2 feet tall Creating new Tree that is 3 feet tall Tree is 3 feet tallOverloading me
6、thod: Tree is 3 feet tall Creating new Tree that is 4 feet tall Tree is 4 feet tallOverloading method: Tree is 4 feet tall Planting a seeding*/ class Tree int height;Tree() prt("Planting a seedling");height = 0;Tree(int i) prt("Creating new Tree that is "+ i + " feet tall&qu
7、ot;);height = i;void info() prt("Tree is " + height+ " feet tall");void info(String s) prt(s + ": Tree is "+ height + " feet tall");static void prt(String s) System.out.println(s);public class Overloading public static void main(String args) for(int i = 0; i &
8、lt; 5; i+) Tree t = new Tree(i);(); ("overloaded method");/ Overloaded constructor:new Tree(); /*Output:Creating new Tree that is 0 feet tallTree is 0 feet tallOverloading method: Tree is 0 feet tallCreating new Tree that is 1 feet tallTree is 1 feet tallOverloading method: Tre
9、e is 1 feet tallCreating new Tree that is 2 feet tallTree is 2 feet tallOverloading method: Tree is 2 feet tallCreating new Tree that is 3 feet tallTree is 3 feet tallOverloading method: Tree is 3 feet tallCreating new Tree that is 4 feet tallTree is 4 feet tallOverloading method: Tree is 4 feet tal
10、l Planting a seeding*/创建 Tree 对象的 时 候,既可以不含参数,也可以用 树的高度当参数。前者表示一 颗树苗,后 者表示已有一定高度的 树 木。要支持 这种 创建方式,得有一个默 认构造器和一个采用 现有高 度作 为参数的构造器。通 过上述方法 对于方法重 载就有了基本的了解。 现在便要知道 该如何 区分重 载 方法。二,区分重 载 方法:根据方法重 载的定 义可以看出,方法重 载最大的区 别在于参数不同,于是就有了以下几种区 分方法:1,参数 类 型不同:void show(String s) ;roshow(i nt a) 。2,参数数目不同:void show
11、(i nt a) ;void show(i nt a,i nt b) 。3,参数 顺 序不同:void show(int a,String s) ; void show(String a) 。这 个内容相比比 较简单 所以就不用我 苍 白的文字 赘 述 过多了。接下来将要分享下新学到的关于方法重 载的知 识点。三,基本 类 型的重 载 这是个比较特殊的重 载应用,我们通过例程分析: java view plaincopyprint?public class PrimitiveOverloading static void prt(String s) System.out.print
12、ln(s); void f1(char x) prt("f1(char)"); void f1(byte x) prt("f1(byte)"); void f1(short x) prt("f1(short)"); void f1(int x) prt("f1(int)"); void f1(long x) prt("f1(long)"); void f1(float x) prt("f1(float)"); void f1(double x) prt("f1(do
13、uble)"); void f2(byte x) prt("f2(byte)"); void f2(short x) prt("f2(short)"); void f2(int x) prt("f2(int)"); void f2(long x) prt("f2(long)"); void f2(float x) prt("f2(float)"); void f2(double x) prt("f2(double)"); void f3(short x) prt(&
14、quot;f3(short)"); void f3(int x) prt("f3(int)"); void f3(long x) prt("f3(long)"); void f3(float x) prt("f3(float)"); void f3(double x) prt("f3(double)"); void f4(int x) prt("f4(int)"); void f4(long x) prt("f4(long)"); void f4(float x)
15、prt("f4(float)"); void f4(double x) prt("f4(double)"); void f5(long x) prt("f5(long)"); void f5(float x) prt("f5(float)"); void f5(double x) prt("f5(double)"); void f6(float x) prt("f6(float)"); void f6(double x) prt("f6(double)")
16、; void f7(double x) prt("f7(double)"); void testConstVal() prt( “ 5:"); f1(5);f2(5);f3(5);f4(5);f5(5);f6(5);f7(5);void testChar() char x = 'x' prt("char:");f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); void testByte() byte x = 0; prt("byte:");f1(x);f2(x);f3(x)
17、;f4(x);f5(x);f6(x);f7(x); void testShort() short x = 0; prt("short:");f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); void testInt() int x = 0;prt("int:"); f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);void testLong() long x = 0; prt("long:");f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);
18、f7(x); void testFloat() float x = 0; prt("float:");f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); void testDouble() double x = 0;prt("double:");f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);public static void main(String args) PrimitiveOverloading p =new PrimitiveOverloading();p.testCons
19、tVal();p.testChar(); p.testByte();p.testShort();p.testInt();p.testLong(); p.testFloat();p.testDouble(); /*Output5:f1(int) f2(int) f3(int) f4(int) f5(long) f6(float) f7(double) char: f1(char) f2(int) f3(int) f4(int) f5(long) f6(float) f7(double) byte: f1(byte) f2(byte) f3(short) f4(int) f5(long) f6(f
20、loat) f7(double) short: f1(short) f2(short) f3(short) f4(int) f5(long) f6(float) f7(double) int: f1(int) f2(int) f3(int) f4(int) f5(long) f6(float) f7(double) long: f1(long) f2(long) f3(long) f4(long) f5(long) f6(float) f7(double) float: f1(float) f2(float) f3(float) f4(float) f5(float) f6(float) f7
21、(double) double: f1(double) f2(double) f3(double) f4(double) f5(double) f6(double) f7(double)*/public class PrimitiveOverloading static void prt(String s) System.out.println(s);void f1(char x) prt("f1(char)"); void f1(byte x) prt("f1(byte)"); void f1(short x) prt("f1(short)&
22、quot;); void f1(int x) prt("f1(int)"); void f1(long x) prt("f1(long)"); void f1(float x) prt("f1(float)"); void f1(double x) prt("f1(double)"); void f2(byte x) prt("f2(byte)"); void f2(short x) prt("f2(short)"); void f2(int x) prt("f2(
23、int)"); void f2(long x) prt("f2(long)"); void f2(float x) prt("f2(float)"); void f2(double x) prt("f2(double)"); void f3(short x) prt("f3(short)"); void f3(int x) prt("f3(int)"); void f3(long x) prt("f3(long)"); void f3(float x) prt(&q
24、uot;f3(float)"); void f3(double x) prt("f3(double)"); void f4(int x) prt("f4(int)"); void f4(long x) prt("f4(long)"); void f4(float x) prt("f4(float)"); void f4(double x) prt("f4(double)"); void f5(long x) prt("f5(long)"); void f5(floa
25、t x) prt("f5(float)"); void f5(double x) prt("f5(double)"); void f6(float x) prt("f6(float)"); void f6(double x) prt("f6(double)"); void f7(double x) prt("f7(double)"); void testConstVal() prt( “ 5:"); f1(5);f2(5);f3(5);f4(5);f5(5);f6(5);f7(5);
26、void testChar() char x = 'x' prt("char:"); f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);void testByte() byte x = 0; prt("byte:"); f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); void testShort() short x = 0;prt("short:"); f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);void t
27、estInt() int x = 0;prt("int:"); f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);void testLong() long x = 0; prt("long:"); f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);void testFloat() float x = 0; prt("float:"); f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);void testDouble() double
28、x = 0; prt("double:");f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);public static void main(String args) PrimitiveOverloading p = new PrimitiveOverloading(); p.testConstVal();p.testChar();p.testByte();p.testShort();p.testInt();p.testLong();p.testFloat();p.testDouble(); /*Output5:f1(int) f2(int
29、) f3(int) f4(int) f5(long) f6(float) f7(double)char: f1(char) f2(int) f3(int) f4(int) f5(long) f6(float) f7(double) byte: f1(byte) f2(byte) f3(short) f4(int) f5(long) f6(float) f7(double) short: f1(short) f2(short) f3(short) f4(int) f5(long) f6(float) f7(double) int: f1(int) f2(int) f3(int) f4(int)
30、f5(long) f6(float) f7(double) long: f1(long) f2(long) f3(long) f4(long) f5(long) f6(float) f7(double) float: f1(float) f2(float) f3(float) f4(float) f5(float) f6(float) f7(double) double: f1(double) f2(double) f3(double) f4(double) f5(double) f6(double) f7(double)*/观察这个程序的 输出,就会 发现 常数值 5 被当作一个 int 值
31、处理。所以如果有某个重 载方法 接受 int 型参数,他就会被 调用。再 观察别的类型可以看出如果 传入的数据 类型(实际参数 类 型)小于方法中声明的形式参数 类型,实际 数据类型就会被提升。但注意 char 型不同,如果 无法找到适合 char 参数的方法, 那么 char 的提升将跳 过 byte 和 short 型而直接提升成 为 int 型。这是传入参数小于形式参数的情况,那如果是 传入的 实际参数大于重 载方法声明的形式 参数将会是以下 这 种情况。java view plaincopyprint?public class Demotion static void prt(Stri
32、ng s) System.out.println(s);void f1(char x) prt("f1(char)"); void f1(byte x) prt("f1(byte)"); void f1(short x) prt("f1(short)"); void f1(int x) prt("f1(int)"); void f1(long x) prt("f1(long)"); void f1(float x) prt("f1(float)"); void f1(doub
33、le x) prt("f1(double)"); void f2(char x) prt("f2(char)"); void f2(byte x) prt("f2(byte)"); void f2(short x) prt("f2(short)"); void f2(int x) prt("f2(int)"); void f2(long x) prt("f2(long)"); void f2(float x) prt("f2(float)"); void
34、f3(char x) prt("f3(char)"); void f3(byte x) prt("f3(byte)"); 窄化 转换void f3(short x) prt("f3(short)"); void f3(int x) prt("f3(int)"); void f3(long x) prt("f3(long)"); void f4(char x) prt("f4(char)"); void f4(byte x) prt("f4(byte)");
35、 void f4(short x) prt("f4(short)"); void f4(int x) prt("f4(int)"); void f5(char x) prt("f5(char)"); void f5(byte x) prt("f5(byte)"); void f5(short x) prt("f5(short)"); void f6(char x) prt("f6(char)"); void f6(byte x) prt("f6(byte)"
36、;); void f7(char x) prt("f7(char)"); void testDouble() double x = 0; prt("double argument:"); f1(x);f2(float)x);f3(long)x);f4(int)x); f5(short)x);f6(byte)x);f7(char)x);/ public static void main(String args) Demotion p = new Demotion(); p.testDouble(); /*Output: double argument: f
37、1(double) f2(float) f3(long) f4(int) f5(short) f6(byte) f7(char)*/ public class Demotion static void prt(String s) System.out.println(s);void f1(char x) prt("f1(char)"); void f1(byte x) prt("f1(byte)"); void f1(short x) prt("f1(short)"); void f1(int x) prt("f1(int)
38、"); void f1(long x) prt("f1(long)"); void f1(float x) prt("f1(float)"); void f1(double x) prt("f1(double)"); void f2(char x) prt("f2(char)"); void f2(byte x) prt("f2(byte)"); void f2(short x) prt("f2(short)"); void f2(int x) prt("f2(int)"); void f2(long x) prt("f2(long)"); void f2(float x) prt("f2(float)"); void f3(char x) prt("f3(char)"); void f3(by
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年下半年2025广西百色市西林县公开招聘县属国企业管理人员1人易考易错模拟试题(共500题)试卷后附参考答案
- 2025山西省晋中市左权县开花调文化发展限公司招聘10人易考易错模拟试题(共500题)试卷后附参考答案
- 2025山东菏铁立喜商业运营管理限公司招聘31人易考易错模拟试题(共500题)试卷后附参考答案
- 2025山东滨州市沾化区事业单位招聘(综合类)拟聘用人员(第二批)易考易错模拟试题(共500题)试卷后附参考答案
- 2025山东兖矿能源集团股份限公司招聘业务人员21人易考易错模拟试题(共500题)试卷后附参考答案
- 2025届宁夏石化分公司高校毕业生春季招聘5人易考易错模拟试题(共500题)试卷后附参考答案
- 2025届中国船舶七一五所春季校园招聘易考易错模拟试题(共500题)试卷后附参考答案
- 2025宿州市萧县人民法院招考聘用制书记员聘用制司法警察易考易错模拟试题(共500题)试卷后附参考答案
- 2025安徽省引江济淮集团限公司下半年公开招聘专业技术人员26人易考易错模拟试题(共500题)试卷后附参考答案
- 2025学年浙江省衢州市柯城区新教师提前批招聘15人(浙师大专场)易考易错模拟试题(共500题)试卷后附参考答案
- 皮带廊清扫管理制度
- 种猪养殖场建设项目初步设计方案
- 浙江德斯泰新材料股份有限公司年产40000吨 PVB 功能膜项目环境影响登记表
- 初中地理学科核心素养培训讲座
- 数学职业生涯规划课件
- T/CADCC 003-2024汽车漆面保护膜施工技术规程
- 检测公司员工合同范本
- 基于学科核心素养的初中生物学作业设计与实施策略研究
- 2025年上饶经开区招才引资集团有限公司招聘笔试参考题库附带答案详解
- 村级组织2025年换届准备工作方案
- 传染病报告及处理规范服务培训
评论
0/150
提交评论