day04--方法_调用的三种方式
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
package com.inmind.method01;
|
||||
/*
|
||||
方法的三种调用方式:
|
||||
1.直接调用 : 方法名(参数列表)
|
||||
|
||||
2.赋值调用:数据类型 变量名 = 方法名(参数列表)
|
||||
|
||||
3.打印输出调用: System.out.println(方法名(参数列表));
|
||||
|
||||
总结:直接调用可以调用任意方法,赋值和打印输出调用只能调用有返回值的方法。
|
||||
*/
|
||||
public class Demo04 {
|
||||
public static void main(String[] args) {
|
||||
//直接调用
|
||||
isEqual(1, 2);
|
||||
printN(3);
|
||||
|
||||
//赋值调用
|
||||
boolean result = isEqual(1, 2);
|
||||
//void v = printN(3);不能调用无返回值的方法
|
||||
|
||||
//打印输出调用
|
||||
System.out.println(isEqual(1, 2));
|
||||
//System.out.println(printN(3));不能打印无返回值的方法
|
||||
}
|
||||
|
||||
|
||||
//有返回值的方法
|
||||
public static boolean isEqual(int a,int b) {
|
||||
return a==b;
|
||||
}
|
||||
|
||||
//没有有返回值的方法
|
||||
public static void printN(int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
System.out.println("helloworld");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user