方法调用及传参

静态方法:有static修饰的方法。

非静态方法:没有static修饰的方法。

  1. 方法调用:

一静态方法调用 静态方法/属性

1)一个类:直接调用。 

2)不同类/不同文件:

a: 类名.属性名/方法名

b:实例化对象。  类名 对象名 = new类名();

                                                          对象名. 属性/方法

           二静态调用 非静态方法/属性

都先实例化对象。     类名 对象名 = new类名();

                                                                 对象名. 属性名/方法名

           一非静态调用静态方法

           二非静态调用非静态方法

            1)同一类中:直接调用

            2)不同类中:

                                a: 类名 . 方法(只能是静态属性)

b:实例化对象

总结:可直接调用的三种情况

1.一个类中 静态调静态 。

2. 一个类中 非静态调用 静态/非静态。

3. 静态     类名.静态属性/静态方法。

public class Demo03{
    int age;
    public static void main(String []args){
        System.out.println(Demo04.name);//静态调用静态1
        Demo04.eat();

        Demo04 d = new Demo04();//静态调用静态2
        System.out.println(d.name);
        d.eat();

        Demo03 d1 = new Demo03();//静态调用非静态
        d1.method();
        System.out.println(d1.age);
    }
    public void method(){
        System.out.println("first method");
    }

}
public class Demo04{
    static String name = "张三";

    public static void eat(){
        System.out.println("肉夹馍");
    }
}
public class Demo05{
    static int age;
    String name;
    public static void main(String []args){

        Demo05 d1 = new Demo05();//静态调非静态  实例化
        d1.method();
    }

    public void method(){
        System.out.println(age); //非静态调静态    
        method1();                 //非静态调静态    
        System.out.println(name);//非静态调非静态    
        method2();                 //非静态调非静态    
        System.out.println("first method");
    }
    public static void method1(){
        System.out.println("second method");
    }
    public void method2(){
            System.out.println("third method");
    }
}
public class Demo06{

    public static void main(String []args){

        Demo06 d1 = new Demo06();   //静态调非静态  实例化
        d1.method();
    }
    public void method(){
        System.out.println(Person.name);   //非静态调静态
        Person.method1();                  //非静态调静态
        Person p = new Person();           //非静态调非静态  实例化
        p.method2();
        System.out.println("first method");
    }
}
class Person{
    static String name;
    int age;
    public static void method1(){
        System.out.println("second method");
    }
    public void method2(){
            System.out.println("third method");
    }
}
public class Demo09{
    //实参到形参是单向的,所以在传递过程中形参值发生改变不会影响实参
    public static void main(String []args){
        int i =1;
        String s = "ww";
        Demo09 d = new Demo09();
        d.method(i,s);
        System.out.println(i);
        System.out.println(s);
    }
    public void method(int i,String s){
        i = 100;
        s = "asd";
    }
        public void method1
}
public class ChuanDiZhi{
    int x = 3;
    public static void main(String args[]){
        ChuanDiZhi p = new ChuanDiZhi();
        p.x = 9;
        show(p);
        System.out.println(p.x);
    }
    public static void show(ChuanDiZhi p){
        p.x = 4;
    }        
}

有无返回值

有void修饰,无返回值

int------------------>return  int  类型的值

string-------------->return String类型的值

数据类型-------->return 当前数据类型值

返回类、集合、流等

方法的返回值类型必须和return之后的值数据类型一样

方法传递参数

形参:形式参数,在定义方法时所携带的参数。

实参:实际参数,方法调用时所传入的参数。

形参和实参的区别:

         1形参只有在被调用的时候,才分配内存单元,在调用结束时立即释放内存单元

                   形参只在当前方法内部有效

2实参可以是常量、变量、表达式、方法 等。但是在进行方法调用前,必须要有确定的值。

3形参和实参在顺序、类型、长度、必须一致。

4实参到形参是单向的,

参数的传值方式

1值传递:在运行函数时,形参和实参在不同的内存位置之中,形参将实参的值复制一份。在函数运行结束时,形参被释放,实参的值不会发生改变。

2地址传递:在运行函数时,传给形参的是实参的地址,那么形参被修改时,实参也会发生改变。

非静态方法:没有static修饰的方法。

  1. 方法调用:

一静态方法调用 静态方法/属性

1)一个类:直接调用。 

2)不同类/不同文件:

a: 类名.属性名/方法名

b:实例化对象。  类名 对象名 = new类名();

                                                          对象名. 属性/方法

           二静态调用 非静态方法/属性

都先实例化对象。     类名 对象名 = new类名();

                                                                 对象名. 属性名/方法名

           一非静态调用静态方法

           二非静态调用非静态方法

            1)同一类中:直接调用

            2)不同类中:

                                a: 类名 . 方法(只能是静态属性)

b:实例化对象

总结:可直接调用的三种情况

1.一个类中 静态调静态 。

2. 一个类中 非静态调用 静态/非静态。

3. 静态     类名.静态属性/静态方法。

public class Demo03{
    int age;
    public static void main(String []args){
        System.out.println(Demo04.name);//静态调用静态1
        Demo04.eat();

        Demo04 d = new Demo04();//静态调用静态2
        System.out.println(d.name);
        d.eat();

        Demo03 d1 = new Demo03();//静态调用非静态
        d1.method();
        System.out.println(d1.age);
    }
    public void method(){
        System.out.println("first method");
    }

}
public class Demo04{
    static String name = "张三";

    public static void eat(){
        System.out.println("肉夹馍");
    }
}
public class Demo05{
    static int age;
    String name;
    public static void main(String []args){

        Demo05 d1 = new Demo05();//静态调非静态  实例化
        d1.method();
    }

    public void method(){
        System.out.println(age); //非静态调静态    
        method1();                 //非静态调静态    
        System.out.println(name);//非静态调非静态    
        method2();                 //非静态调非静态    
        System.out.println("first method");
    }
    public static void method1(){
        System.out.println("second method");
    }
    public void method2(){
            System.out.println("third method");
    }
}
public class Demo06{

    public static void main(String []args){

        Demo06 d1 = new Demo06();   //静态调非静态  实例化
        d1.method();
    }
    public void method(){
        System.out.println(Person.name);   //非静态调静态
        Person.method1();                  //非静态调静态
        Person p = new Person();           //非静态调非静态  实例化
        p.method2();
        System.out.println("first method");
    }
}
class Person{
    static String name;
    int age;
    public static void method1(){
        System.out.println("second method");
    }
    public void method2(){
            System.out.println("third method");
    }
}
public class Demo09{
    //实参到形参是单向的,所以在传递过程中形参值发生改变不会影响实参
    public static void main(String []args){
        int i =1;
        String s = "ww";
        Demo09 d = new Demo09();
        d.method(i,s);
        System.out.println(i);
        System.out.println(s);
    }
    public void method(int i,String s){
        i = 100;
        s = "asd";
    }
        public void method1
}
public class ChuanDiZhi{
    int x = 3;
    public static void main(String args[]){
        ChuanDiZhi p = new ChuanDiZhi();
        p.x = 9;
        show(p);
        System.out.println(p.x);
    }
    public static void show(ChuanDiZhi p){
        p.x = 4;
    }        
}

有无返回值

有void修饰,无返回值

int------------------>return  int  类型的值

string-------------->return String类型的值

数据类型-------->return 当前数据类型值

返回类、集合、流等

方法的返回值类型必须和return之后的值数据类型一样

方法传递参数

形参:形式参数,在定义方法时所携带的参数。

实参:实际参数,方法调用时所传入的参数。

形参和实参的区别:

         1形参只有在被调用的时候,才分配内存单元,在调用结束时立即释放内存单元

                   形参只在当前方法内部有效

2实参可以是常量、变量、表达式、方法 等。但是在进行方法调用前,必须要有确定的值。

3形参和实参在顺序、类型、长度、必须一致。

4实参到形参是单向的,

参数的传值方式

1值传递:在运行函数时,形参和实参在不同的内存位置之中,形参将实参的值复制一份。在函数运行结束时,形参被释放,实参的值不会发生改变。

2地址传递:在运行函数时,传给形参的是实参的地址,那么形参被修改时,实参也会发生改变。

猜你喜欢

转载自www.cnblogs.com/yueluoshuxiang/p/11367091.html