Java方法
方法概述:
一些重复的代码,为了避免代码臃肿可以将代码放在{}中,并取名。
并按名字来调用这段代码。
定义格式:
修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2){
方法体;
return 返回值;
}
修饰符: 目前记住使用public static 即可;
返回值类型: 用于限定返回值的类型;
方法名: 方便我们调用方法;
参数类型: 用于限定调用方法传入数据的数据类型;
参数名: 用于接受变量是传入数据的变量;
方法体: 完成功能的代码;
返回值: 谁调用返回给谁接收
#方法与方法之间平级;
#方法取名第一个单词首字母小写,第二个单词首字母大写;
public class Test09 {
public static int sum(int a,int b){
int c = a + b;
return c;
}
public static void main(String[] args) {
int result = sum(10,20);
System.out.println(result);
}
}
方法练习1:键盘录入两个数据,返回两个数中的较大值。(录入放main)
public class Test09 {
public static int getMax(int a,int b){
if (a > b){
return a;
}
else {
return b;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 获取数据
System.out.println("请输入第一个数据");
int a = sc.nextInt();
System.out.println("请输入第二个数据");
int b = sc.nextInt();
int max = getMax(a,b);
System.out.println(max);
}
}
方法练习2:写一个方法,把所有的水仙花数打印在控制台
package com.scy;
import java.util.Random;
public class Test08 {
public static void printFlower (){
for (int i= 100;i<1000;i++){
int ge = i%10;
int shi = i/10%10;
int bai = i/100%10;
if (ge*ge*ge+bai*bai*bai+shi*shi*shi==i){
System.out.println(i);
}
}
}
public static void main(String[] args) {
// 写一个方法,把所有的水仙花数打印在控制台
printFlower();
}
}
方法练习3:写一个方法,打印1到n之间的数据(n是调用的时候传递过来的)
package com.scy;
import java.util.Random;
public class Test08 {
public static void getNum (int a){
for (int i=1;i<=a;i++){
System.out.println(i);
}
}
public static void main(String[] args) {
// 写一个方法,把所有的水仙花数打印在控制台
getNum(10);
}
}
Java方法的重载
方法重载:在同一个类中,出现方法名相同的情况。
特点:1.方法名相同
2.方法参数列表不同:
参数个数不同。
参数对应的数据类型不同。
3.方法重载与返回值无关。
注意:Java虚拟机会根据方法名及参数列表不同来区分方法。
方法重载练习1:
比较两个数据是否相等。
参数类型分别为两个byte类型,两个short类型,两个int类型,两个long类型。
方法参数是基本数据类型情况
public static boolean compare (byte a, byte b){
System.out.println("byte");
// if (a==b){
return true;
}
else {
return false;
}
return a==b;
}
public static boolean compare (short a, short b){
System.out.println("short");
return a==b;
}
public static boolean compare (int a, int b){
System.out.println("int");
return a==b;
}
public static boolean compare (long a, long b){
System.out.println("long");
return a==b;
}
public static void main(String[] args) {
// 写一个方法,把所有的水仙花数打印在控制台
boolean result1 = compare(20,30);
boolean result = compare((short)20,(short)30);
boolean result2 = compare(20L,30L);
}
}
方法参数是引用类型的情况
/*
* 基本类型作为方法的参数,形式参数的改变不影响实际参数。
*
* 形式参数:用于接收实际参数的变量
* 实际参数:实际参与运算的数据
*/
public class ArgsDemo {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println("a:" + a + ",b:" + b);//a:10,b:20
change(a, b);
System.out.println("a:" + a + ",b:" + b);//???
}
public static void change(int a, int b) {//a=10,b=20
System.out.println("a:" + a + ",b:" + b);//a:10,b:20
a = b;//a=20;
b = a + b;//b=40;
System.out.println("a:" + a + ",b:" + b);//a:20,b:40
}
}
基本数据类型作为参数传递,形式参数的改变不影响实际参数;
package com.itheima;
public class ArgsDemo2 {
public static void main(String[] args) {
int[] arr = { 1, 2, 3, 4, 5 };
for (int x = 0; x < arr.length; x++) {
System.out.println(arr[x]);//1,2,3,4,5
}
change(arr);
for (int x = 0; x < arr.length; x++) {
System.out.println(arr[x]);//1,4,3,8,5
}
}
public static void change(int[] arr) {
for (int x = 0; x < arr.length; x++) {
//如果数组元素是偶数,值就变成了以前的2倍
if (arr[x] % 2 == 0) {
arr[x] *= 2;
}
}
}
}
引用数据类型作为参数传递,形式参数的改变直接影响实际参数
方法练习2需求:把遍历数组改进为方法实现,并调用方法。
public class Test07 {
public static void getArray (int[] arr){
for (int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
}
public static void main(String[] args) {
int[] arr = {11,22,33,44,55};
getArray(arr);
}
}
public class Test07 { //改进打印结构为[11,22,33,44,55]
public static void getArray (int[] arr){
for (int i=0;i<arr.length;i++){
if (i == arr.length-1){
System.out.print(arr[i]);
}
else {
System.out.print(arr[i]);
System.out.print(", ");
}
}
}
public static void main(String[] args) {
int[] arr = {11,22,33,44,55};
System.out.print("[");
getArray(arr);
System.out.print("]");
}
}
方法练习33需求:把获取数组最值改进为方法实现,并调用
public class Test07 {
public static int getMax(int[] arr){
int max = arr[0];
for (int i=0; i<arr.length;i++){
if (max<arr[i]){
max = arr[i];
}
}
return max;
}
public static void main(String[] args) {
int[] arr = {11,22,33,44,55,66};
int max = getMax(arr);
System.out.println(max);
}
}