回顾方法
回顾方法及加深
-
方法的定义
-
修饰符
-
返回值类型
-
break:跳出switch,结束循环 和return :方法结束;的区别
-
方法名:注意规范,见名知意
-
参数列表:(参数类型,参数名)…
-
异常抛出:疑问
package oop; import com.sun.corba.se.impl.logging.IORSystemException; import java.io.IOException; //Dome01 类 public class Dome01 { //main 方法(用于启动程序) public static void main(String[] args) { }/* 修饰符 返回值类型 方法名(方法的参数){ 方法体 return 返回值;} */ //return 结束方法,返回一个结果! public String sayHello(){ return "hellow,world"; } //没有返回值void public void hellow(){ return; } public int max(int a,int b){ return a>b ? a : b;//三元运算符 } //数组下标越界:Arrayindexoutofbounds public void readFile(String file) throws IOException{ } }
-
-
方法的调用:递归
-
静态方法
-
非静态方法
package oop; //学生类 public class Student { //静态方法 public static void say(){ System.out.println("学生说话了"); } //非静态方法 public void talk(){ System.out.println("讲故事"); } }
package oop; public class Dome02 { //静态方法 static //非静态方法 public static void main(String[] args) { //要调用静态(static)方法: 类名.方法名 Student.say(); //调用非静态方法,实例化这个类 new //对象的类 对象名=对象值 Student student = new Student();//等同于new Student().talk(); student.talk(); } //static静态方法和类一起加载 public static void a(){ //b();报错,调用的时候要注意 } //非静态方法,类实例化之后才存在的 public void b(){ } }
-
形参和实参
package oop; public class Dome03 { public static void main(String[] args) { //实际参数的和形式参数的类型要对应 //new Dome03().add(1,2); int add = Dome03.add(1, 2); System.out.println(add); }//main 方法,方便测试 public static int add(int a,int b){ return a+b; } }
-
值传递和引用传递
package oop; //值传递 public class Dome04 { public static void main(String[] args) { int a = 1; System.out.println(a); Dome04.change(a); System.out.println(a); } //返回值为空 public static void change(int a){ a = 10; } }
1 1 Process finished with exit code 0
package oop; public class Dome05 { public static void main(String[] args) { Person person = new Person(); System.out.println(person.name);//null Dome05.change(person); System.out.println(person.name); } public static void change(Person person){ //person是一个对象--->Person person =new person();这是一个具体的人,可以改变属性 person.name = "秦疆"; } } //定义了一个类person类,有一个属性:name class Person{ String name;//null }
null 秦疆 Process finished with exit code 0
-
this关键字
-