JAVA进阶5

间歇性混吃等死,持续性踌躇满志系列-------------第5天

1、IDEA常用快捷键

2、简单方法的使用

 1 package cn.intcast.day05.demo01;
 2 
 3 public class demo01 {
 4     public static void main(String[] args) {
 5         //单独调用
 6         sum(3, 4);
 7         //打印调用
 8         System.out.println(sum(5, 9));
 9         //赋值调用
10         int num = sum(100, 12);
11         System.out.println("赋值后变量的值:" + num);
12     }
13 
14     public static int sum(int a, int b) {
15         int re = a + b;
16         return re;
17     }
18 }
方法的三种调用格式

运行结果图

3、方法中有参数和无参数的对比

 1 package cn.intcast.day05.demo01;
 2 
 3 public class demo01 {
 4     public static void main(String[] args) {
 5         me(12,12);
 6         we();
 7     }
 8 
 9     //有参数
10     public static void me(int a, int b) {
11         int re = a * b;
12         System.out.println("两数的积" + re);
13     }
14     //无参数
15     public static void we(){
16         for (int i = 0; i < 5; i++) {
17             System.out.println("你好啊!");
18         }
19     }
20 }
View Code

运行结果图

4、方法练习:比较两个数字是否相等

 1 package cn.intcast.day05.demo01;
 2 
 3 //判断两个数是否相等
 4 public class demo01 {
 5     public static void main(String[] args) {
 6         System.out.println(isSame(10, 15));  //false
 7         System.out.println(isSame(10, 10));  //true
 8     }
 9 
10     /*三要素
11     返回值:boolean
12     方法名称:isSame
13     参数列表:int a ,int b
14     */
15     public static boolean isSame(int a, int b) {
16         /*boolean same;
17         if(a==b){
18             same =true;
19         }else {
20             same =false;
21         }
22         return same;*/
23 
24         /*boolean same = a==b? true:false;
25         return same;*/
26         return a == b;
27     }
28 }
View Code

运行结果图

5、使用方法的注意事项

6、方法的重载

 1 package cn.intcast.day05.demo01;
 2 
 3 //判断两个数是否相等
 4 public class demo01 {
 5     public static void main(String[] args) {
 6         byte a = 10;
 7         byte b = 10;
 8         System.out.println(su(a, b));
 9         System.out.println(su((short) 11, (short) 12));
10         System.out.println(su(12, 1));
11         System.out.println(su(1l, 2L));
12     }
13 
14     public static boolean su(byte a, byte b) {
15         System.out.println("两个byte参数的执行!!!");
16         return a == b;
17     }
18 
19     public static boolean su(short a, short b) {
20         System.out.println("两个short参数的执行!!!");
21         if (a == b) {
22             return true;
23         } else {
24             return false;
25         }
26     }
27 
28     public static boolean su(int a, int b) {
29         System.out.println("两个int参数的执行!!!");
30         boolean s;
31         if (a == b) {
32             s = true;
33         } else {
34             s = false;
35         }
36         return s;
37     }
38 
39     public static boolean su(long a, long b) {
40         System.out.println("两个long参数的执行!!!");
41         boolean s = a == b ? true : false;
42         return s;
43     }
44 }
重载方法练习

运行结果图

posted @ 2019-03-16 22:31  贫血的吸血鬼  阅读(154)  评论(0编辑  收藏  举报