方法的调用
- 静态方法
- 非静态方法
- 形参和实参
- 值传递和引用传递
- this关键字(this.xxx表示该类中的xxx变量)
-
静态方法:
在定义方法时加static,可直接调用
-
非静态方法
在定义方法时不加static,不可直接调用,需要new一个对象
package oop.Demo02;
public class Student {
//非静态方法
public void Say(){
System.out.println("学生说话了!");
}
}
package oop.Demo02;
public class demo03 {
public static void main(String[] args) {
//非静态
demo03 demo03 = new demo03();
System.out.println(demo03.add1(10,20)); //30
//静态
System.out.println(add2(10,20)); //30
Student student = new Student();
student.Say(); //学生说话了!
}
//非静态
public int add1(int a,int b){
return a+b;
}
//静态
public static int add2(int a,int b){
return a+b;
}
}
值传递和引用传递
package oop.Demo02;
//值传递
public class demo01 {
public static void main(String[] args) {
int a = 1;
System.out.println(a); //1
change(a);
System.out.println(a); //1
}
public static void change(int a){
a=10;
return;
}
}
package oop.Demo02;
//引用传递
public class demo02 {
public static void main(String[] args) {
Person person = new Person();
System.out.println(person.name); //null
change(person);
System.out.println(person.name); //xiaoming
}
public static void change(Person person){
person.name="xiaoming";
}
}
class Person{
String name;
}