Python 官方文档:入门教程 => 点击学习
目录一、方法(Method)概念二、可变参数(Variable)三、方法的参数传递问题1. 基本数据类型2. 引用数据类型四、方法签名(Method Signature)五、方法的重
下面是方法体代码案例:
public class MethodBody {
// 1.代码块
{
System.out.println("【{}】是方法体");
}
// 2.静态代码块
static {
System.out.println("【static {}】是方法体");
}
// 3.方法
public void run(int age) {
System.out.println("方法的花括号中是方法体");
// 4.if
if (age == 18) {
System.out.println("if 语句的花括号中是方法体");
}
// 5.for
for (int i = 0; i < age; i++) {
System.out.println("for 循环的花括号中是方法体");
}
// 6.while
while (age > 50) {
System.out.println("while 循环的花括号中是方法体");
}
// 7.switch-case
switch (age) {
// 错误:在该区域写代码是错误的(该区域不是方法体)
// System.out.println(age); // ERROR
case 1: {
System.out.println("switch 语句的 case 语句块是方法体");
}
}
// 8.do-while
do {
System.out.println("do-while 循环的花括号中是方法体");
} while (age < 5);
}
}
其实可以理解为只有三个地方是代码块:
① 代码块
② 静态代码块
③ 方法中
但是,当初老师教的时候把 if、while、for 等也归纳为方法体
补充:定义方法可能还会有其他修饰符(eg:static、final、abstract),后面还会详细介绍
仔细看下面的代码, 学会定义方法:
public class CreateMethodDemo {
public static void main(String[] args) {
int sum1 = CreateMethodDemo.sumOne2Hundred(1, 100);
// sum1 = 5050
System.out.println("sum1 = " + sum1);
int sum2 = CreateMethodDemo.sumOne2Hundred(1, 1000);
// sum2 = 500500
System.out.println("sum2 = " + sum2);
int sum3 = CreateMethodDemo.sumOne2Hundred(1, 10000);
// sum3 = 50005000
System.out.println("sum3 = " + sum3);
}
private static int sumOne2Hundred(int start, int end) {
int sum = 0;
for (int i = start; i <= end; i++) {
sum += i;
}
return sum;
}
}
思考:编写程序计算多个整数的和。eg:计算【2, 5, 6, 7, 66, 53】的和
public class VariableParameter {
public static void main(String[] args) {
int[] arr = {2, 5, 6, 7, 66, 53};
VariableParameter vp = new VariableParameter();
// sumByArr = 139
System.out.println(vp.sumByArr(arr));
}
private String sumByArr(int[] arr) {
if (arr == null || arr.length < 1) return "arr 数组为 null, 为数组元素为 0";
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return "sumByArr = " + sum;
}
}
思路1:
?可把需要进行求和的整数放入一个整型数组中,并把整型数组作为参数传给 sumByArr 方法
?sumByArr 方法接收一个 int 类型的数组作为参数,在 sumByArr 的方法体中通过 for 循环遍历数组中的数字,并进行求和
思路2:
?使用可变参数替换 arr 数组
public class VariableParameter {
public static void main(String[] args) {
VariableParameter vp = new VariableParameter();
// 当 sumByVariable1Parameter 的参数列表中一个【值】都没有
// 的时候, 返回值是可变参数类型的默认值
int sum = vp.sumByVariable1Parameter(2, 5, 6, 7, 66, 53);
// sumByVariable1Parameter = 139
System.out.println("sumByVariable1Parameter = " + sum);
}
private int sumByVariable1Parameter(int... nums) {
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
}
return sum;
}
}
String 类有静态方法 format 可用于拼接字符,它的底层就用到了【可变参数】
public class VariableParameter {
public static void main(String[] args) {
String info = String.fORMat("name: %s; age: %d; money: %f",
"庆医", 10, 895863.99);
// info = name: 庆医; age: 10; money: 895863.990000
System.out.println("info = " + info);
}
}
Passing Primitive Data Type Arguments 传递原始数据类型参数
Primitive arguments, such as an int or a double, are passed into methods by value. This means that any changes to the values of the parameters exist only within the scope of the method. When the method returns, the parameters are Gone and any changes to them are lost.
原始参数(eg:int 或 double)通过 value 传递给方法。这意味着对参数值的任何更改仅存在于该方法的作用域内。当方法返回后,栈帧销毁后,参数消失后,对它们的任何更改都将无效。
public class ArgumentsPassingTest {
public static void main(String[] args) {
int n = 10;
test(n); // 值传递(v 和 n 没有关系)
// n = 10
System.out.println("n = " + n);
}
private static void test(int v) { // v = 10
v = 20;
}
}
基本类型作为返回值,返回的是值本身:
public class ArgumentsPassingTest {
public static void main(String[] args) {
int test = test(66);
// test = 88
System.out.println("test = " + test);
}
private static int test(int param) {
param = 88;
return param;
}
}
Passing Reference Data Type Arguments(传递引用数据类型参数)
Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object’s fields can be changed in the method, if they have the proper access level.
引用数据类型参数(例如对象)也按值传递给方法。这意味着当方法返回时,传入的引用仍然引用着与之前相同的对象。但是,如果对象字段的值具有适当的访问级别,则可以在方法中更改它们。
public class ArgumentsPassingTest {
public static void main(String[] args) {
int[] nums = {1, 2, 3};
test(nums);
// nums = [1, 66, 3]
System.out.println("nums = " + Arrays.toString(nums));
}
private static void test(int[] param) {
param[1] = 66;
}
}
引用类型作为返回值是引用传递(地址传递):
public class ArgumentsPassingTest {
public static void main(String[] args) {
int[] test = test();
// test = [1314, 520, 666]
System.out.println("test = " + Arrays.toString(test));
}
private static int[] test() {
int[] ints = {1314, 520, 666};
return ints;
}
}
栈帧销毁销毁的是局部变量信息,堆空间的对象不会被回收的。
方法签名只由2部分组成:方法名、参数类型
private static void test(double pai, String name, int age) {
return null;
}
官方教程:
Overloaded(重载) methods are differentiated by the number and the type of the arguments passed into the method. For example: run(String s) and run(int i) are distinct and unique methods because they require different argument types.
重载的方法通过传递给方法的参数的数量和类型来区分。
例如:run(String s)
和 run(int i)
是不同且独特的方法,因为它们拥有不同的参数类型。
重载:
The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.
编译器在区分方法时不考虑返回类型,因此即使它们具有不同的返回类型,也不能声明具有相同签名的两个方法。
You cannot declare more than one method with the same name and the same number and type of arguments, because the compiler cannot tell them apart.
您不能声明多个具有相同名称和相同数量和类型的参数的方法,因为编译器无法区分它们。
下面的两个方法构成方法重载:
private static int[] test(double weight, String name, int age) {
return null;
}
private static int[] test(int age, double weight, String name) {
return null;
}
到此这篇关于Java 方法(方法的定义,可变参数,参数的传递问题,方法重载,方法签名)的文章就介绍到这了,更多相关Java方法定义内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Java 方法(方法的定义,可变参数,参数的传递问题,方法重载,方法签名)
本文链接: https://www.lsjlt.com/news/167281.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0