方法的调用:递归
方法的调用:递归
静态方法和非静态方法
package Demo1;public class Demo03 {public static void main(String[] args) {//静态方法 staticStudent.a();//非静态方法//无法像静态方法直接调用,// 需要实例化这个类,使用new关键字//可以通过这样写new Student().b();// 但是一般使用更为规范的写法如下Student student = new Student();student.b();}//当方法都是静态或者非静态时可以相互调用//但是当有一方时静态方法时,静态方法的那一方无法调用另一方//这是因为静态方法是和类同时加载的,而方法此时还没加载所以不能调用public static void a(){// b();}public void b(){a();}}
//另外一个类Student
/*** public class Student {*** //静态方法* public static void a(){* System.out.println("静态方法");* }* //* public void b(){* System.out.println("非静态方法");* }** }*/
形参和实参
package Demo1;public class Demo04 {public static void main(String[] args) {//实际参数和形式参数是得同一种类型System.out.println(add(1,2));}public static int add(int a,int b){return a+b;}
}
值传递和引用传递
package Demo1;public class Demo05 {public static void main(String[] args) {//值传递int a=1;System.out.println(a);Demo05.ab(1);System.out.println(a);}//返回值为空,这边a只是实际参数,并没有返回改变a的值public static void ab(int a){a=10;}
}
输出结果为两个1
package Demo1;public class Demo06 {//引用传递public static void main(String[] args) {Person person = new Person();//类的实例化System.out.println(person.name);change(person);System.out.println(person.name);}public static void change(Person person){//person是一个对象改变的是Person类中的值person.name="111";}}
class Person{String name;
}
输出结果为:
null
111
this关键字
这边仅仅提到,this只是当作这个类名来调用,例如在Demo01中调用方法可以使用this.add( );