java方法的调用_java方法调用及传参

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

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

方法调用:

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

1)一个类:直接调用。

2)不同类/不同文件:

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

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

对象名. 属性/方法

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

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

对象名. 属性名/方法名

一非静态调用静态方法

二非静态调用非静态方法

1)同一类中:直接调用

2)不同类中:

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

b:实例化对象

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

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

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

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

public classDemo03{intage;public static voidmain(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 voidmethod(){

System.out.println("first method");

}

}

1 public classDemo04{2 static String name = "张三";3

4 public static voideat(){5 System.out.println("肉夹馍");6 }7 }

1 public classDemo05{2 static intage;3 String name;4 public static voidmain(String []args){5

6 Demo05 d1 = new Demo05();//静态调非静态 实例化

7 d1.method();8 }9

10 public voidmethod(){11 System.out.println(age); //非静态调静态

12 method1(); //非静态调静态

13 System.out.println(name);//非静态调非静态

14 method2(); //非静态调非静态

15 System.out.println("first method");16 }17 public static voidmethod1(){18 System.out.println("second method");19 }20 public voidmethod2(){21 System.out.println("third method");22 }23 }

1 public classDemo06{2

3 public static voidmain(String []args){4

5 Demo06 d1 = new Demo06(); //静态调非静态 实例化

6 d1.method();7 }8 public voidmethod(){9 System.out.println(Person.name); //非静态调静态

10 Person.method1(); //非静态调静态

11 Person p = new Person(); //非静态调非静态 实例化

12 p.method2();13 System.out.println("first method");14 }15 }16 classPerson{17 staticString name;18 intage;19 public static voidmethod1(){20 System.out.println("second method");21 }22 public voidmethod2(){23 System.out.println("third method");24 }25 }

1 public classDemo09{2 //实参到形参是单向的,所以在传递过程中形参值发生改变不会影响实参

3 public static voidmain(String []args){4 int i =1;5 String s = "ww";6 Demo09 d = newDemo09();7 d.method(i,s);8 System.out.println(i);9 System.out.println(s);10 }11 public void method(inti,String s){12 i = 100;13 s = "asd";14 }15 public voidmethod116 }

public classChuanDiZhi{int x = 3;public static voidmain(String args[]){

ChuanDiZhi p= newChuanDiZhi();

p.x= 9;

show(p);

System.out.println(p.x);

}public static voidshow(ChuanDiZhi p){

p.x= 4;

}

}

有无返回值

有void修饰,无返回值

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

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

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

返回类、集合、流等

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

方法传递参数

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

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

形参和实参的区别:

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

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

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

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

4实参到形参是单向的,

参数的传值方式

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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值