java面向对象03-方法调用的回顾


方法类

public class Demo01 {
//方法的调用
//静态方法
public static void method(){
System.out.println("我是静态方法");
}

public void methodTwo(){
System.out.println("我是非静态方法");
}

public static int sum(int a ,int b){
return a+b;
}


}

main方法类

public class Application {
public static void main(String[] args) {
Demo01.method();
Demo01 demo01 = new Demo01();
demo01.methodTwo();


Application application = new Application();
//形式参数和实际参数类型要一致
Demo01.sum(1,2);
//静态方法可以通过类名直接调用
Application.print();
//非静态方法只有在类实例化之后才能被调用
application.printTwo();

//java里面所有的都是值传递,引用传递的实质也是值传递

Person person = new Person();
System.out.println(person.name);
Application.change(person);
System.out.println(person.name);

}

public static void change(Person person){
person.name="奥利给";
}

public static void print(){
System.out.println("我和类一起加载");
}
public void printTwo(){
System.out.println("类实例过后我才加载");
}
}

class Person{
String name;

}
控制台打印

我是静态方法
我是非静态方法
我和类一起加载
类实例过后我才加载
null
奥利给



posted on 2021-06-24 14:01  谢波认真学java  阅读(48)  评论(0)    收藏  举报

导航