1 public class shuzu03{
2 public static void main(String[] args){
3
4
5 AA a = new AA();
6 int[] res = a.getSumAndSub(1,4);
7 System.out.println("和=" + res[0]);
8 System.out.println("差=" + res[1]);
9
10
11
12 //细节:调用带参数的方法时,一定对应着参数列表传入相同类型或兼容类型的参数
13 byte b1 = 1;
14 byte b2 = 2;
15 a.getSumAndSub(b1,b2);//byte->int
16 //a.getSumAndSub(1.1,1.8);//double ->int(x)
17 //细节:实参和形参的类型要一致或兼容、个数、顺序必须一致
18 //a.getSumAndSub(100)//x 个数不一致
19 a.f3("tom",10);//ok
20 //a.f3(100,"jack");//实际参数和形式参数顺序不对
21
22
23 }
24 }
25
26
27 class AA{
28 //细节:方法不能嵌套定义
29 public void f4(){
30 // 错误
31 // public void f5(){
32 //
33 // }
34 }
35
36 public void f3(String str,int n){
37
38 }
39
40 //1.一个方法最多有一个返回值【思考,如何返回多个结果 返回数组】
41 public int[] getSumAndSub(int n1,int n2){
42
43
44 int[] resArr = new int[2];//
45 resArr[0] = n1 + n2;
46 resArr[1] = n1 - n2;
47 return resArr;
48
49 }
50 //2.返回类型可以为任意类型,包含基本类型或引用类型(数组,对象)
51 //具体看 getSumAndSub
52 //
53 //3.如果方法要求有返回数据类型,则方法体中最后的执行语句必须为 return 值;
54 // 而且要求返回值类型必须和return的值类型一致或兼容
55 public double f1(){
56
57
58 double d1 = 1.1 * 3;
59 int n = 100;
60 return n;//int ->double
61 //return d1; //ok? double -> int
62 }
63 //如果方法时void,则方法体中可以没有return 语句,或者 只写 return;
64 //提示:在实际工作中,我们的方法都是为了完成某个功能,所以方法名要有一定含义
65 //,最好是见名知意
66
67 public void f2(){
68
69 System.out.println("hello1");
70 System.out.println("hello1");
71 System.out.println("hello1");
72 int n = 10;
73 //return;
74 }
75
76
77 }