【原】Java学习笔记008 - 方法(函数)

 1 package cn.temptation;
 2
 3 public class Sample01 {
 4     public static void main(String[] args) {
 5         // 方法/函数 Method/Function
 6
 7         // 为什么会出现方法?
 8         // 原因1、在程序中多处出现相同的语句内容
 9         // 原因2、会发生变化
10
11         // 【只有变化是不变的】
12         // 回顾一下变量,为了应对变化,提出的新的机制
13         // 需要对变化进行封装处理,在这里就是要对一系列的语句进行封装,得到方法的概念
14         // 【方法的封装性】
15
16         // 方法的格式:
17         // 访问修饰符       返回类型      方法名(参数类型1   参数名1, 参数类型2   参数名2, ..., 参数类型n   参数名n) {
18         //        方法体内各种执行语句;
19         //        return  返回值;        // return语句用于有返回类型的场合
20         // }
21
22         // 1、访问修饰符:public static(公开的静态的)
23         // 2、返回类型:① 无返回的    void;② 有返回的
24         // 3、方法名:方法的名称,在方法被调用时使用
25         // 4、方法名后的括号里可以有参数列表(有参),也可以没有参数列表(无参)
26         // 5、参数列表:参数类型1   参数名1, 参数类型2   参数名2, ..., 参数类型n   参数名n
27
28         // 【方法的使用原则:不调用,不执行】
29
30         // 方法的调用形式:方法名(参数列表);
31
32         // 做输出的事情
33 //        System.out.println("天气不太好");
34 ////        System.out.println("网络不太好");
35 //        System.out.println("网络现在太好");
36         // 把对语句的使用,替换为对方法的调用
37         show();
38
39         // 做了一些其他的事情
40         // ...
41
42         // 做输出的事情
43 //        System.out.println("天气不太好");
44 ////        System.out.println("网络不太好");
45 //        System.out.println("网络现在太好");
46         // 把对语句的使用,替换为对方法的调用
47         show();
48
49         // 做了一些其他的事情
50         // ...
51
52         // 做输出的事情
53 //        System.out.println("天气不太好");
54 ////        System.out.println("网络不太好");
55 //        System.out.println("网络现在太好");
56         // 把对语句的使用,替换为对方法的调用
57         show();
58     }
59
60     // 在和主函数main同级的位置,制作方法
61     public static void show() {
62         // 做输出的事情
63         System.out.println("天气不太好");
64         // 当语句发生变化时,使用方法后,不需要对方法的调用发生修改,只需要修改一下方法中的语句即可
65 //        System.out.println("网络不太好");
66         System.out.println("网络现在太好");
67
68         // void无返回的方法中,写上return语句也可以,但是return无任何类型的返回值
69 //        return;
70         // 语法错误:Void methods cannot return a value,不能返回一个具体数据类型的结果
71 //        return true;
72     }
73 }
 1 package cn.temptation;
 2
 3 public class Sample02 {
 4     public static void main(String[] args) {
 5         // 1、方法在某一个类的内部的
 6         // 2、在称呼上,方法 和 函数 其实是一个意思
 7         // 3、做事情时,不要强求在一个方法内全部做完,不同的方法做不同的事情
 8
 9         // 方法对内封装语句,体现了它的【封装性】
10         // 方法对外到处被调用,体现了它的【复用性】
11     }
12
13     /**
14      * 方法名:显示方法的名称
15      * 方法用途:用于显示方法的用途
16      * 方法返回值:无
17      * 方法参数名:无
18      * 方法创建人:张三
19      * 方法创建时间:YYYY.MM.DD
20      * 方法修改人:李四
21      * 方法修改时间:YYYY.MM.DD
22      * 方法修改内容:.....
23      */
24     public static void show() {
25
26     }
27 }
28
29 // 语法错误:Syntax error on token "}", delete this token
30 // 语法错误:Syntax error, insert "}" to complete ClassBody
31 //public static void showOther() {
32 //
33 //}
 1 package cn.temptation;
 2
 3 public class Sample03 {
 4     public static void main(String[] args) {
 5         // 注意:
 6         // 1、方法是不能嵌套方法的,方法和方法之间是平级的
 7         // 2、方法名及参数列表均相同的方法是不能被定义的
 8
 9         // 语法错误:Illegal modifier for parameter show; only final is permitted
10 //        public static void show() {
11 //
12 //        }
13     }
14
15     public static void show() {
16
17     }
18
19     // 语法错误:Duplicate method show() in type
20 //    public static void show() {
21 //
22 //    }
23 }
 1 package cn.temptation;
 2
 3 public class Sample04 {
 4     public static void main(String[] args) {
 5         // 调用无返回值的方法
 6 //        add();
 7
 8         // 调用有返回值的方法
 9         // 注意:返回值是什么数据类型的值,在调用并接收的地方就要声明什么数据类型的变量来进行接收
10         // 定义局部变量result,在方法中定义的局部变量只能在该方法中使用,超出该方法无法访问。
11         // 所以主函数中定义的局部变量 和 同级方法中定义的局部变量,虽然同名同类型,但是互相不受影响
12         int result = add();
13         // 打印出结果
14         System.out.println(result);
15     }
16
17     // 需求:制作一个无返回值的方法,计算两个数的和
18 //    public static void add() {
19 //        int i = 2;
20 //        int j = 3;
21 //
22 //        int result = i + j;
23 //
24 //        // 直接打印出结果
25 //        System.out.println(result);
26 //    }
27
28     // 需求:制作一个有返回值的方法,计算两个数的和(考虑到计算归计算,打印归打印)
29     public static int add() {
30         int i = 2;
31         int j = 3;
32
33         // 定义局部变量result,在方法中定义的局部变量只能在该方法中使用,超出该方法无法访问
34         int result = i + j;
35
36         // 如果没有写return语句,会产生语法错误:This method must return a result of type int
37         return result;
38     }
39 }
 1 package cn.temptation;
 2
 3 public class Sample05 {
 4     public static void main(String[] args) {
 5         // 有参方法的调用
 6
 7         // 方法的参数:根据方法是被定义还是被调用,将方法的参数分为两种
 8         // 1、形参:形式参数,用于方法定义时,起站位表明参数类型的作用
 9         // 2、实参:实际参数,用于方法调用时,不用加上参数类型
10
11         // 注意:形参和实参的数据类型要一致
12
13         int result = add(2, 3);
14         // 实参不需要加上数据类型,加上会有语法错误
15 //        int result = add(int 2, int 3);
16         System.out.println(result);
17     }
18
19     // 定义方法
20     public static int add(int i, int j) {
21         // 方法内部就可以使用形参来做加法的操作
22         int result = i + j;
23
24         return result;
25     }
26 }
 1 package cn.temptation;
 2
 3 public class Sample06 {
 4     public static void main(String[] args) {
 5         // 使用方法时需要注意的问题:
 6         // 1、方法的定义
 7         //        ① 方法的返回值类型(输出)
 8         //        ② 方法的参数列表(输入)
 9         // 2、方法的调用
10
11         // 方法的调用形式
12         // 1、直接调用:一般用于无返回值的方法
13         // 2、赋值调用:在调用方法的地方,声明一个和方法返回值类型相同的变量,用以接收方法的返回值(一般用于有返回值的方法)
14         // 3、输出调用:本质上就是拿着方法的返回值作为输出语句的方法的实参使用
15
16         // 直接调用
17         test1();
18
19         // 赋值调用
20         int result = test2();
21         System.out.println(result);
22
23         // 输出调用
24         System.out.println(test2());
25     }
26
27     public static void test1() {
28         System.out.println("直接调用的方法");
29     }
30
31     public static int test2() {
32         System.out.println("有返回值的方法");
33         return 123;
34     }
35 }
 1 package cn.temptation;
 2
 3 import java.util.Scanner;
 4
 5 public class Sample07 {
 6     public static void main(String[] args) {
 7         // 需求:制作方法,接收用户键盘录入,判断输入的两个数字哪个大?
 8
 9         // 制作方法时,关注点在方法的返回值类型  和 参数列表上
10         // 好的方法都不是一蹴而就的,是不断提炼出来的
11
12         Scanner input = new Scanner(System.in);
13         System.out.println("输入一个数字:");
14         int i = input.nextInt();
15         System.out.println("再输入一个数字:");
16         int j = input.nextInt();
17         input.close();
18
19         // 调用方法,传递实参
20         int result = max(i, j);
21         System.out.println("两个数:" + i + "和" + j + "中较大的数为:" + result);
22     }
23
24     // 根据输入的数进行比较
25     public static int max(int i, int j) {
26         // 定义一个变量
27         int max = 0;
28
29         if (i > j) {
30             max = i;
31         } else {
32             max = j;
33         }
34
35         return max;
36     }
37 }
 1 package cn.temptation;
 2
 3 import java.util.Scanner;
 4
 5 public class Sample08 {
 6     public static void main(String[] args) {
 7         // 需求:制作一个方法,判断输入的两个数是否相等
 8         Scanner input = new Scanner(System.in);
 9         System.out.println("输入一个数字:");
10         int i = input.nextInt();
11         System.out.println("再输入一个数字:");
12         int j = input.nextInt();
13         input.close();
14
15         // 调用方法
16         if (compare(i, j)) {
17             System.out.println("两数相等");
18         } else {
19             System.out.println("两数不等");
20         }
21     }
22
23     // 制作方法,比较两个数是否相同
24     public static boolean compare(int i, int j) {
25         // 定义一个是否相同的标识
26 //        boolean flag = true;
27
28         // 写法1
29 //        if (i - j == 0) {
30 //            flag = true;
31 //        } else {
32 //            flag = false;
33 //        }
34
35         // 写法2
36 //        if (i == j) {
37 //            flag = true;
38 //        } else {
39 //            flag = false;
40 //        }
41
42         // 写法3
43 //        if (i - j != 0) {
44 //            flag = false;
45 //        }
46
47         // 写法4
48 //        if (i != j) {
49 //            flag = false;
50 //        }
51 //
52 //        return flag;
53
54         // 写法5
55 //        flag = (i == j) ? true : false;
56 //        return flag;
57
58         // 写法6
59         return i == j;
60     }
61 }
 1 package cn.temptation;
 2
 3 import java.util.Scanner;
 4
 5 public class Sample09 {
 6     public static void main(String[] args) {
 7         // 需求:制作方法,获取三个数中最大的一个
 8         Scanner input = new Scanner(System.in);
 9         System.out.println("输入第一个数字:");
10         int i = input.nextInt();
11         System.out.println("输入第二个数字:");
12         int j = input.nextInt();
13         System.out.println("输入第三个数字:");
14         int k = input.nextInt();
15         input.close();
16
17         // 调用方法
18         int result = max(i, j, k);
19         System.out.println("最大的数为:" + result);
20     }
21
22     // 比较三个数中最大的一个
23     public static int max(int i, int j, int k) {
24          // 定义一个变量max
25         int max = 0;
26
27         // 写法1
28 //        if (i > j) {
29 //            if (i > k) {
30 //                max = i;
31 //            } else {
32 //                max = k;
33 //            }
34 //        } else {
35 //            if (j > k) {
36 //                max = j;
37 //            } else {
38 //                max = k;
39 //            }
40 //        }
41
42         // 写法2
43         int tempMax = (i > j) ? i : j;
44         max = (tempMax > k) ? tempMax : k;
45
46         return max;
47     }
48 }
 1 package cn.temptation;
 2
 3 public class Sample10 {
 4     public static void main(String[] args) {
 5         // 方法的重载(英文:Overload)
 6         // 定义:方法名相同、参数列表不同的一系列方法称为方法的重载
 7         System.out.println(add(2, 3));
 8         System.out.println(add(4, 5, 6));
 9     }
10
11     // 需求:制作一个方法,操作两个数相加
12     public static int add1(int i, int j) {
13         int result = i + j;
14         return result;
15     }
16
17     // 需求:制作一个方法,操作三个数相加
18     public static int add2(int i, int j, int k) {
19         int result = i + j + k;
20         return result;
21     }
22
23     // 需求:制作一个方法,操作四个数相加...
24
25     // 既然这些方法做的都是数字相加的事情,考虑使用相同的方法名add
26
27     // 需求:制作一个方法,操作两个数相加
28     public static int add(int i, int j) {
29         int result = i + j;
30         return result;
31     }
32
33     // 需求:制作一个方法,操作三个数相加
34     public static int add(int i, int j, int k) {
35         int result = i + j + k;
36         return result;
37     }
38 }
 1 package cn.temptation;
 2
 3 public class Sample11 {
 4     public static void main(String[] args) {
 5         // 方法重载的注意点:
 6         // 1、方法名需要相同
 7         // 2、方法的参数列表必须不同,包括参数的类型或个数,以此区分不同的方法
 8         //        ① 如果参数个数不同,不用考虑参数类型
 9         //        ②如果参数个数相同,需要考虑参数的类型 或 参数的顺序必须不同
10         // 3、方法的返回值类型、访问修饰符可以相同,也可以不相同
11     }
12
13     public static void show(int i, int j) {
14
15     }
16
17     public static void show(int i, double j) {
18
19     }
20
21     // 参数个数相同,参数类型相同,但是参数名不同的方法不是重载方法
22     // 语法错误:Duplicate method show(int, int) in type
23 //    public static void show(int a, int b) {
24 //
25 //    }
26
27     public static void show(int i, int j, int k) {
28
29     }
30
31     // 和第二个方法参数个数相同、类型相同,但是顺序不同,也是重载方法
32     public static void show(double j, int i) {
33
34     }
35
36     // 和第一个方法参数列表相同,但是无法区分参数顺序是否不同,所以产生语法错误
37     // 语法错误:Duplicate method show(int, int) in type
38 //    public static void show(int j, int i) {
39 //
40 //    }
41
42     // 虽然返回值类型和其他重载方法不同,但是并不影响是重载方法
43     public static boolean show(int i) {
44         return true;
45     }
46
47     // 和第一个方法参数列表相同,但是返回值类型不同,这也无法区分,有语法错误
48     // 语法错误:Duplicate method show(int, int) in type
49 //    public static boolean show(int i, int j) {
50 //        return false;
51 //    }
52 }
 1 package cn.temptation;
 2
 3 public class Sample12 {
 4     public static void main(String[] args) {
 5         // 方法调用时,匹配参数的数据类型,优先选择参数类型完全匹配的重载方法
 6         System.out.println(test(3, 4));
 7 //        System.out.println(test(3.4, 4.5));
 8
 9         // 注释掉重载方法1、5,保留重载方法2、3、4时有语法错误
10         // The method test(double, double) is ambiguous for the type
11
12         // 注释掉重载方法1、2、5,保留重载方法3、4时有语法错误
13         // The method test(int, double) is ambiguous for the type
14
15         // 注释掉重载方法1、2、3、4,保留重载方法5时有语法错误
16         // The method test() in the type Sample12 is not applicable for the arguments (int, int)
17
18         // 调用方法重载时,参数匹配规则:
19         // 1、如果有参数类型完全匹配的重载方法,会自动选择该重载方法
20         // 2、如果没有参数类型完全匹配的重载方法,首先会去匹配部分匹配的重载方法
21         //        注意:如果有多个部分匹配程度一致的重载方法,会ambiguous for the type语法错误,因为无法选择
22         // 3、如果只有一个参数类型不匹配的方法,这时也就不用谈什么重载了,观察形参的数据类型和实参的数据类型,如果形参的数据类型可以包含实参的数据类型就不会出错
23     }
24
25     // 重载方法1
26     public static int test(int i, int j) {
27         return 1;
28     }
29
30     // 重载方法2
31 //    public static int test(double i, double j) {
32 //        return 2;
33 //    }
34
35     // 重载方法3
36 //    public static int test(int i, double j) {
37 //        return 3;
38 //    }
39
40     // 重载方法4
41 //    public static int test(double i, int j) {
42 //        return 4;
43 //    }
44
45     // 重载方法5
46 //    public static int test() {
47 //        return 5;
48 //    }
49
50     // 重载方法6
51     public static int test(long a, long b) {
52         return 6;
53     }
54 }
时间: 2024-08-24 16:19:57

【原】Java学习笔记008 - 方法(函数)的相关文章

Java学习笔记之方法重载,动态方法调度和抽象类

一.方法重载 如果子类中的方法与它的超类中的方法有相同的方法名,则称子类中的方法重载超类中的方法,特别是当超类和子类中的方法名和参数类型都相同时,在子类中调用该方法时,超类中的方法会被隐藏.考虑下面程序: 1 class A 2 { 3 int i, j; 4 A(int a, int b) 5 { 6 i = a; 7 j = b; 8 } 9 10 // display i and j 11 void show() 12 { 13 System.out.println("i and j: &

Java学习笔记之方法重载

被重载的方法必须具有不同的参数列表.不能基于不同修饰符或返回值类型来重载方法. package welcome; public class TestMethodOverloading { public static void main(String[] args) { System.out.println("The maximum between 3 and 4 is " + max(3, 4)); // 调用max(int, int)方法 System.out.println(&qu

java学习笔记14--多线程编程基础1

本文地址:http://www.cnblogs.com/archimedes/p/java-study-note14.html,转载请注明源地址. 多线程编程基础 多进程 一个独立程序的每一次运行称为一个进程,例如:用字处理软件编辑文稿时,同时打开mp3播放程序听音乐,这两个独立的程序在同时运行,称为两个进程 进程要占用相当一部分处理器时间和内存资源 进程具有独立的内存空间 通信很不方便,编程模型比较复杂 多线程 一个程序中多段代码同时并发执行,称为多线程,线程比进程开销小,协作和数据交换容易

非专业码农 JAVA学习笔记 6java工具类和算法-string

续<非专业码农 JAVA学习笔记 5 java工具类和算法> 五.字符串string 字符串和字符的差别:字符串双引号括起来”n”,字符用单引号括起来,表示一种符号’\n’ 1.string的主要方法和属性 类 方法或者属性 备注 定义string Stirng s=new string(“值”),string s=”值” 属性 string.length:string的长度为字节 方法startswith,endswith s.startwith(“值”)-以值为开头,s.endswith(

Java学习笔记之接口

一.接口的概念与定义 首先考虑一个简单的接口的定义: public interface Output { int MAX_LINE = 40; void out(); void getData(String msg); } 定义接口使用关键字interface 修饰符interface前面的public可以省略,如果省略,则采用默认访问控制,即只有在相同包结构的代码才可以访问此接口 接口不可以有构造方法(区别于类中的构造方法) 接口里面的所有成员,包括常量.方法等都是public访问权限,所以在

java学习笔记16--异常

java学习笔记16--异常 异常 异常时导致程序中断运行的一种指令流,如果不对异常进行正确的处理,则可能导致程序的中断执行,造成不必要的损失, 所以在程序的设计中必须要考虑各种异常的发生,并正确的做好相应的处理,这样才能保证程序正常的执行. 异常类的继承结构 在整个java的异常结构中,实际上有以下两个最常用的类:Exception.Error,这两个类全都是Throwable的子类 Exception:一般表示的是程序中出现的问题,可以直接使用try...catch处理. Error:一般指

Swift学习笔记(14)--方法

1.分类 方法分为实例方法和类型方法 实例方法(Instance Methods):与java中的类似,略 类型方法(Type Methods):与java.oc中的类方法类似.声明类的类型方法,在方法的func关键字之前加上关键字class:声明结构体和枚举的类型方法,在方法的func关键字之前加上关键字static. 方法的参数名参见学习笔记的函数章节 2.在实例方法中修改值类型和self 结构体和枚举是值类型.一般情况下,值类型的属性不能在它的实例方法中被修改. 但是,如果你确实需要在某个

Java学习笔记&lt;3&gt;面向对象相关

面向对象的基本思想 从现实世界客观存在的事务出发来构造软件系统,并在系统的构造中尽可能运用人类的自然思维方式,如抽象.分类 继承.聚合.多态等. 类和对象的关系 对象中存储了类规定的数据类型,并且对象可以调用类的方法. java面向对象 <1>对象是java程序的核心,一切皆对象. <2>对象可以看成静态属性(成员变量)和动态属性(方法)的封装体. <3>类是创新同一类型对象的模版,定义了该类型对象应具有的成员变量及方法. 类的定义 成员变量可以用java语言的任何一种

Java学习笔记_23_List接口实现类

23.List接口实现类: List接口继承了Collection接口,它是一个允许存在重复项的有序集合. 1>实现类ArrayList: ArrayList类支持可随需要而增长的动态数组.数组列表以一个原大小被创建,当超过了它的大小, 类集自动增大,当对象被删除后,数组就可以缩小. 优点:ArrayList类对于使用索引取出元素用较高的效率,他可以用索引快速定位对象. 缺点:ArrayList类对于元素的删除或插入速度较慢. 构造方法: · ArrayList(): 构造一个初始容量为10的空