回顾方法和加深
-
方法的定义
-
修饰符
-
返回类型
-
break:跳出switch,结束循环 和 return的区别
-
方法名:注意规范,见名知意
-
参数列表:(参数类型,参数名)…
-
异常抛出:
//Demo01 类 public class Demo01 { //main 方法 public static void main(String[] args) { } /* 修饰符 返回值类型 方法名(...){ //方法体 return 返回值 } */ public String sayHello(){ return "hello,world"; } public void print(){ return; } public int max(int a,int b){ return a>b ? a : b; //三元运算符 } //数组下标越界:Arrayindexoutofbounds public void readFile(String file) throws IOException{ } }
-
-
方法的调用:递归
-
静态方法
-
非静态方法
public class Demo02 { public static void main(String[] args) { //静态方法 static Student.say(); //实例化这个类 new //对象类型 对象名 = 对象值; Student student = new Student(); student.say1(); } //非静态方法 } -
形参和实参
public class Demo03 { public static void main(String[] args) { //实际参数和形式参数的类型要对应 int add = Demo03.add(1, 2); System.out.println(add); } public static int add(int a,int b){ return a+b; } } -
值传递和引用传递
//值传递 public class Demo04 { public static void main(String[] args) { int a = 1; System.out.println(a); Demo04.change(a); System.out.println(a); //1 } //返回值为空 public static void change(int a){ a = 10; } }//引用传递:对象,本质还是值传递 public class Demo05 { public static void main(String[] args) { Perosn perosn = new Perosn(); System.out.println(perosn.name); Demo05.change(perosn); System.out.println(perosn.name); } public static void change(Perosn perosn){ //perosn是一个对象:指向的---> Perosn perosn = new Perosn();可以改变属性 perosn.name = "Devil"; } } //定义了一个perosn类,有一个属性:name class Perosn{ String name; } -
this关键字
//java--->class public class Person { //一个类即使什么都不写,它也会存在一个方法 //显示的定义构造器 String name; //实例化初始值 //1,使用new关键字,本质是在调用构造器 //2,用来初始化值 public Person(){ this.name = "李四"; } //有参构造:一旦定义了有参构造,无参构造就必须显示定义 public Person(String name){ this.name = name; } //alt + insert 生成构造器 } /* public static void main(String[] args) { //new 实例化了一个对象 Person person = new Person(); System.out.println(person.name); } 构造器 1,和类名相同 2,没有返回值 作用: 1,new本质在调用构造方法 2,初始化对象的值 注意点: 1,定义有参构造之后,如果想使用无参构造,显示的定义一个无参的构造 alt + Insert 生成构造器 this.类名 = */
-
1万+

被折叠的 条评论
为什么被折叠?



