方法的定义
修饰符
返回类型
break :跳出switch,结束循环 和 return 的区别
参数列表:(参数类型,参数名)…
异常抛出
1 package oop.Demo01;
2 ?
3 import java.io.IOException;
4 ?
5 //Demo01 类
6 public class Demo01 {
7 ?
8 //main 方法
9 public static void main(String[] args) {
10 ?
11 }
12 ?
13 /*
14 修饰符 返回值类型 方法名(…){
15 //方法体
16 return 返回值;
17 }
18 */
19 public String sayHello(){
20 return "hello,world";
21 }
22 ?
23 public void hello(){
24 return;//返回空
25 }
26 ?
27 public int max(int a,int b){
28 return a>b ? a : b;//三元运算符
29 }
30
31
32 //数组下标越界 Arrayindexoutofbounds
33 public void readFile(String file) throws IOException{
34
35 }
36 ?
37 }
?
方法的调用(递归:难点)
静态方法
非静态方法
1 package oop.Demo01;
2 ?
3 public class Demo02 {
4 ?
5 public static void main(String[] args) {
6 //Student.say();(为静态方法时的调用)
7 ?
8 //非静态方法时
9 //实体化这个new
10 //对象类型 对象名 = 对象值;
11 ?
12 Student student = new Student();
13 ?
14 student.say();
15 }
16 }
1 package oop.Demo01;
2 ?
3 //学生类
4 public class Student {
5 ?
6 //静态方法
7 /*public static void say(){
8 System.out.println("学生说话了");
9 } */
10 ?
11 //非静态方法
12 public void say(){
13 System.out.println("学生说话了");
14 }
15 }
形参和实参
1 package oop.Demo01;
2 ?
3 public class Demo03 {
4 ?
5 public static void main(String[] args) {
6 //实际参数和形式参数的类型要对应
7 int add = Demo03.add(1,2);
8 System.out.println(add);
9 }
10 ?
11 public static int add(int a, int b){
12 return a+b;
13 }
14 }
值传递和引用传递
1 package oop.Demo01;
2 ?
3 //值传递
4 public class Demo04 {
5 public static void main(String[] args) {
6 int a = 1;
7 System.out.println(a);//1
8 ?
9 Demo04.change(a);
10 System.out.println(a);//1
11 }
12 ?
13 //返回值为空
14 public static void change(int a){
15 a = 10;
16 }
17 }
1 package oop.Demo01;
2 ?
3 //值传递
4 public class Demo04 {
5 public static void main(String[] args) {
6 int a = 1;
7 System.out.println(a);//1
8 ?
9 Demo04.change(a);
10 System.out.println(a);//1
11 }
12 ?
13 //返回值为空
14 public static void change(int a){
15 a = 10;
16 }
17 }
原文:https://www.cnblogs.com/carlos4study/p/15018486.html