Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2a31bbd2c5 | ||
|
|
2fa1c45be1 |
@@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package com.inmind.method01;
|
||||||
|
/**
|
||||||
|
* 方法重载:指在同一个类中,允许存在一个以上的同名方法,只要它们的参数列表不同即可,与修饰符和返回值类型无关。
|
||||||
|
* 方法重载:两同一不同
|
||||||
|
* 1.同一类中
|
||||||
|
* 2.方法名相同
|
||||||
|
* 3.参数列表不同(参数列表的个数,数据类型,参数的顺序)
|
||||||
|
*
|
||||||
|
* 总结:方法重载的作用:只要记住一个方法名,就可以调用不同的功能
|
||||||
|
*/
|
||||||
|
public class OverLoadDemo5 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//定义2个整数相加之和,并返回结果的方法
|
||||||
|
getSum(1, 2);
|
||||||
|
//定义3个整数相加之和,并返回结果的方法
|
||||||
|
getSum(1, 2, 3);
|
||||||
|
//定义4个整数相加之和,并返回结果的方法
|
||||||
|
getSum(1, 2, 3, 4);
|
||||||
|
|
||||||
|
System.out.println(1);
|
||||||
|
System.out.println(1.1);
|
||||||
|
System.out.println(true);
|
||||||
|
System.out.println("true");
|
||||||
|
System.out.println(1.5F);
|
||||||
|
System.out.println(1L);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getSum(int a,int b) {
|
||||||
|
int sum = a+b;
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static int getSum(int a,int b,int c) {
|
||||||
|
int sum = a+b+c;
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getSum(int a,int b,int c,int d) {
|
||||||
|
int sum = a+b+c+d;
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user