回顾方法的定义 回顾方法的调用

回顾方法的定义

package oop;

public class Demo01 {
    //main方法
    public static void main(String[] args) {

    }


    /*
    修饰符 返回值类型 方法名(...){
    //方法体
    return 返回值;
    }
     */
public String sayHello(){
    return "hello,world";
}
public int max(int a ,int b){
    return a>b?a:b;//三 元运算 符
}
}

回顾方法的调用

 

package oop;

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;
    }
}
package oop;

public class Demo04 {
    public static void main(String[] args) {
     int a=1;
     System.out.println(a);//1

     Demo04.change(a);
      System.out.println(a);//1
    }
    //返回值为空
    public static void change(int a){
        a=10;
    }
}
package oop;

public class Demo05 {
    public static void main(String[] args) {
        Person person = new Person();
        System.out.println(person.name);//null
        Demo05.change(person);
        System.out.println(person.name);
    }
    public static void change(Person person){
        //person是一个对象:指向的--->Person person=new Person();这是一个具体的人,是可以改变属性的
        person.name="xc";
    }
}
//定义一个Person类,有一个属性:name
class Person{
    String name;

}

 

posted @ 2020-12-17 21:56  XC666  阅读(52)  评论(0)    收藏  举报