JAVA基础概念(三)

JAVA方法入参和返回类型

  • 方法入参

    • 基础数据类型

    • 引用数据类型

    修饰符 返回类型 方法名(参数类型 参数名,参数类型 参数名...){
    //方法体
    return
    }
  • 方法返回类型

    • return xxx 具体类型

    • 如果不用返回,则方法返回类型上写void

    修饰符 void ⽅方法名(参数类型 参数名,参数类型 参数名...){
    //⽅方法体
    }

例子

1 package study2day;public class Student1 {    
2     private int age;    
3     public void setAge(int age){        
4         this.age =age;    
5     }    
6     public int getAge(){        
7         return  age;    
8     }
9 }

注:第一块为引用代码

 1 package study2day;
 2  3 public class ParamTest {
 4     public static void main(String [] args){
 5         ParamTest.calculate("5000",2000);
 6         Student1 a = new Student1();
 7         a.setAge(10);
 8         System.out.println("操作前="+a.getAge());
 9         changeAge(a);
10         System.out.println("操作后="+a.getAge());
11     }
12 13     public static void calculate(String numbera,int numberb){
14         System.out.println("numbera ="+numbera);
15         System.out.println("numberb ="+numberb);
16     }
17     public static void changeAge(Student1 hx){
18         hx.setAge(27);
19     }
20 }

 

猜你喜欢

转载自www.cnblogs.com/jyuri/p/12078771.html
今日推荐