每天一事

来源:互联网 发布:305耳机在淘宝上买吗 编辑:程序博客网 时间:2024/04/27 14:15

方法举例:

public class TestMethod {

   

    public int m1(int a, int b, int c){

       int result = a + b + c;

       return result;

      

    }

   

    public void m2(int c){

       System.out.println(c);

    }

    //没有返回值的 所以要加void

    public static void main(String[] args) {

       TestMethod tm = new TestMethod();

       int i = tm.m1(2, 3, 4);

       System.out.println(i);

    }

}

 

输出结果:9

 

public class TestMethod {

   

    public int m1(int a, int b, int c){

       int result = a + b + c;

       return result;

      

    }

   

    public void m2(int c){

       System.out.println(c);

    }

   

    public static void main(String[] args) {

       TestMethod tm = new TestMethod();

       System.out.println(tm.m1(3, 4, 5));

    }

}

输出结果:12

public class TestMethod {

   

    public int m1(int a, int b, int c){

       int result = a + b + c;

       return result;

      

    }

   

    public void m2(int c){

       System.out.println(c);

    }

   

    public static void main(String[] args) {

       TestMethod tm = new TestMethod();

       int i = tm.m1(2, 3, 4);

       tm.m2(8);

    }

}

输出结果:8

 

public class TestMethod {

   

    public int m1(int a, int b, int c){

       int result = a + b + c;

       return result;

      

    }

   

    public void m2(int c){

       System.out.println(c);

    }

   

    public static void main(String[] args) {

       TestMethod tm = new TestMethod();

       int i = tm.m1(2, 3, 4);

//     System.out.println(i);

//     System.out.println(tm.m1(3, 4, 5));

       tm.m1(2, 3, 4);//合法 ,丢弃了返回结果 没有值输出

//     tm.m2(8);

    }

}

输出结果:没有输出结果

 

 

变量作用域举例:

public class TestVariable {

    public static void main(String[] args) {

       C c = new C();

       c.m1();

       int k = 4;

       c.m2(k);

    }

 

}

    class C {

          

       private int a = 1;

       private int b = 5;

      

       public void m1(){

           int i = 5, j = 5;

           a = i + j;

       }

      

       public void m2(int i){

           int j = 8;

           a = i + j;

           System.out.println(a);

       }

    }

结果是:12

 

值传递实例:

 

 

public class TestMyDate {

    public static void main(String[] args) {

     MyDate mydate = new MyDate();

     mydate.show();

    }

}

class MyDate {

    private int day = 1;

    private int month = 6;

    private int year = 1990;

    public int getDay() {

       return day;

    }

    public void setDay(int day) {

       this.day = day;

    }

    public int getMonth() {

       return month;

    }

    public void setMonth(int month) {

       this.month = month;

    }

    public int getYear() {

       return year;

    }

    public void setYear(int year) {

       this.year = year;

    }

 

    public void show(){

       System.out.println("year:"+year+",month:"+month+",day:"+day);

    }

}

输出结果:year:1990,month:6,day:1

 

 值传递实例:

public class Example {

    public static void main(String[] args) {

       Example ex = new Example();

       int date = 9;

       ex.change1(date);

      

       MyDate d1 = new MyDate();

       ex.change2(d1);

      

       MyDate d2 = new MyDate();

       ex.change3(d2);

      

       System.out.println(date);

       d1.display();

       d2.display();

    }

   

    public void change1(int i){//括号中为i的参数

       i = 12;

    }

   

    public void change2(MyDate b){

       b = new MyDate();

    }

   

    public void change3(MyDate b){

       b.setDay(22);

    }

}

public class MyDate {

    private int day = 1;

    private int month = 6;

    private int year = 1990;

    public int getDay() {

       return day;

    }

    public void setDay(int day) {

       this.day = day;

    }

    public int getMonth() {

       return month;

    }

    public void setMonth(int month) {

       this.month = month;

    }

    public int getYear() {

       return year;

    }

    public void setYear(int year) {

       this.year = year;

    }

 

    public void show(){

       System.out.println("year:"+year+",month:"+month+",day:"+day);

    }

    public void display() {

       // TODO Auto-generatedmethod stub

       System.out.println("year:"+year+",month:"+month+",day:"+day);

    }

}

结果为:

9

year:1990,month:6,day:1

year:1990,month:6,day:22

 


原创粉丝点击