赞
踩
目录
方法是将具有独立功能的代码块组织成为一个整体,使其具有特殊功能的代码集。
注意:
1、方法必须先创建才能使用,该过程称为方法定义
2、方法创建后并不是直接运行,需要手动使用后才执行,该过程称方法调用。
格式:
- public static void 方法名(){
- //方法体
- }
范例
- public static void isEvenNumber(){
- //方法体
- }
格式
方法名();
范例
isEvenNumber();
- public class MethodDemo {
- public static void main(String[] args){
- //调用方法
- isEvenNumber();
- }
-
- //定义方法
- public static void isEvenNumber(){
- int number=10;
- if(number%2==0){
- System.out.println(true);
- }else {
- System.out.println(false);
- }
- }
- }

方法必须先定义后调用,否则程序会报错
设计一个方法用于打印两个数中较大数
- public class MethodText {
- public static void main(String[] args){
- getMax();
- }
-
- public static void getMax(){
- int a=10;
- int b=20;
- if(a>b){
- System.out.println(a);
- }else{
- System.out.println(b);
- }
- }
- }
格式:
public static void 方法名(参数){... ...}
格式(单个参数)
public static void 方法名(数据类型 变量名){... ...}
范例(单个参数):
public static void isEvenNumber(int number){... ...}
格式(多个参数)
public static void 方法名(数据类型 变量名1,数据类型 变量名2,.....){......}
范例(多个参数)
public static void getMax(int number,int numder){......}
注意:
方法定义时,参数中的数据类型与变量都不能缺少,缺少任意一个程序将报错
方法定义时,多个参数之间使用逗号(,)分隔
格式:
方法名(参数);
格式(单个参数)
方法名(变量名/常量值);
范例(单个参数)
isEvenNunber(5);
格式(多个参数)
方法名(变量名1/常量值1,变量名2/常量值2);
范例(多个参数)
getMax(5,6);
注意:
方法调用时,参数的数量与类型必须与方法定义中的设置相匹配,否则程序将报错
- public class MethodText {
- public static void main(String[] args){
- //带参数调用(常量)
- isEvenNumber(10);
- //带参调用变量
- int i=12;
- isEvenNumber(i);
- }
- //带参数的定义
- public static void isEvenNumber(int Nunber){
- if(Nunber%2==0){
- System.out.println(true);
- }else {
- System.out.println(false);
- }
- }
- }

形参:方法定义中的参数,等同于变量的定义格式,例如:int number
- public class MethondDemo02{
-
- public static void main(String[] agrs){
- isEvenNumber(10);
- int number=10;
- isEvenNumber(number);
- }
-
- public static void isEvenNumber(int number){
- if(number % 2==0){
- System.out.println(true);
- }else{
- System.out.println(false);
- }
- }
-
- }

实参:方法调用中的参数,等同于使用变量或常量,例如 10 number
设计一个方法用于打印两个数中较大的数,数据来自参数
- public class MethodText {
- public static void main(String[] args) {
- getMax(10,30);
- int a=10;
- int b=30;
- getMax(a,b);
- }
- public static void getMax(int a,int b){
- if(a>b){
- System.out.println(a);
- }else{
- System.out.println(b);
- }
- }
- }
格式:
- public static 数据类型 方法名(参数){
- return 数据;
- }
范例1:
- public static boolean isEvenNumber(int number){
- return true;
- }
范例2:
- public static int getMax(int a, int b){
- return 100;
- }
注意:
方法定义时return后面的返回值与方法定义上的数据类型要匹配,否则程序将报错
格式1:
方法名(参数);
范例1:
isEvenNumber(5);
格式2:
数据类型 变量名 = 方法名(参数);
范例2:
boolean flag = isEvenNumber(5);
注意:
方法的返回值通常会使用变量接收,否则该返回值将无意义
- public class MethodText {
- public static void main(String[] args) {
- isEvenNumber(10);
- boolean flag=isEvenNumber(10);
- System.out.println(flag);
- }
- //定义一个方法,该方法接收一个参数,判断该参数是否是偶数,并返回真假
- public static boolean isEvenNumber(int number){
- if(number%2==0){
- return true;
- }else{
- return false;
- }
- }
- }
设计一个方法可以获取两个数的较大值,数据来自参数
- public class MethodText {
- public static void main(String[] args) {
- int i=getMax(10,20);
- System.out.println(i);
- System.out.println(getMax(30,70));
- }
- //定义一个方法,该方法接收一个参数,判断该参数是否是偶数,并返回真假
- public static int getMax(int a,int b){
- if(a>b){
- return a;
- }else {
- return b;
- }
- }
- }
正确
- public class ForForDemo {
- public static void methoodOne(){
-
- }
-
- public static void methoonTow(){
-
-
- }
-
- }
错误
- public class ForForDemo {
- public static void methoodOne(){
- public static void methoonTow(){
-
- }
- }
-
- }
2、void 表示无返回值,可以省略return,也可以单独的书写return,后面不加数据
正确
- public class ForForDemo {
- public static void methood(){
-
- }
- }
正确
- public class ForForDemo {
- public static void methood(){
- return;
- }
- }
错误
- public class ForForDemo {
- public static void methood(){
- return 100;
- }
- }
格式:
- public static 返回值类型 方法名(参数){
- 方法体;
- return 数据;
-
- }
public static 修饰符
返回值类型 方法操作完毕之后返回的数据类型
如果方法操作完毕,没有数据返回,这里写void,而且方法体中一般不写return
方法名 调用方法时候使用的标识
参数 由数据类型和变量名组成,多个参数之间用逗号隔开
方法体 完整功能的代码块
return 如果方法操作完毕,有数据返回,用于把数据返回给调用者
明确返回值类型:主要是明确方法操作完毕之后是否有数据返回,如果没有,写 void 如果有,写对应的数据类型
明确参数:主要是明确参数的类型和数量
void 类型的方法,直接调用即可
非void类型的方法,推荐用变量接收调用
方法重载指同一个类中定义的多个方法之间的关系,满足下列条件的多个方法相互构成重载
1、多个方法在同一个类中
2、多个方法具有相同的方法名
3、多个方法的参数不相同,类型不同或者数量不同
重载仅对应方法的定义,与方法调用无关,调用方式参照标准格式
重载仅针对同一个类中方法的名称与参数进行识别,与返回值无关,换句话说不能通过返回值来判定两个方法是否相互构成重载。
不是方法重载
- public class MethodDemo{
- //方法重载与返回值无关,不能通过返回值判定方法重载
- public static void fn(int a){
-
- }
- public static int fn(int a){
-
- }
-
- }
- //不在同一个类
- public class MethodDemo01 {
- public static void fn(int a) {
-
- }
-
- }
- public class MethodDemo02{
- public static int fn(double a){
-
- }
- }
是方法重载
- public class MethodDemo {
- public static float fn(int a){
-
- }
- public static int fn(int a,int b){
-
- }
- }
- public class MethodDemo {
- public static void fn(int a){
-
- }
- public static int fn(double a){
-
- }
- }
案例
- public class MethodDemo {
- //与返回值无关
- //在调用的时候,java虚拟机会通过参数的不同来区分同名方法
-
- public static void main(String[] agrs){
- int i=sum(10,20);
- System.out.println(i);
-
- double j=sun(20.1,30.2);
- System.out.println(j);
-
- int k=sun(1,3,4);
- System.out.println(k);
-
- }
-
- //求两个int类型数据和的方法
- public static int sum(int a,int b) {
- return a+b;
- }
-
- //求两个double类型数据和的方法
- public static double sun(double a,double b){
- return a+b;
- }
-
- //求三个int类型数据和的方法
- public static int sun(int a,int b,int c){
- return a+b+c;
- }
-
-
- }

需求:使用方法重载的思想,设计比较两个整数是否相同的方法,兼容全整数类型(byte,short,int,long)
- public class MethodDemo {
-
- public static void main(String[] agrs){
- System.out.println(compare(12,12));
- System.out.println(compare((long)10,(long)90));
- System.out.println(compare((byte)2,(byte)2));
- System.out.println(compare((short)3,(short)3));
- }
-
- public static boolean compare(int a,int b){
- return a==b;
- }
-
- public static boolean compare(long a,long b){
- return a==b;
- }
-
- public static boolean compare(byte a, byte b){
- return a==b;
- }
-
- public static boolean compare(short a,short b){
- return a==b;
- }
-
-
-
- }

- public class MethodDemo {
-
- public static void main(String[] agrs){
- int number=100;
- System.out.println("调用change之前:"+number);
- change(number);
- System.out.println("调用change之后:"+number);
- }
- public static void change(int number){
- number=200;
- }
-
-
-
- }
对于基本数据类型的参数,形式参数的改变,不影响实际参数的值
- public class MethodDemo {
-
- public static void main(String[] agrs){
- int arr[] ={10,20,30};
- System.out.println("调用change之前:"+arr[0]);
- change(arr);
- System.out.println("调用change之后:"+arr[0]);
- }
- public static void change(int arr[]){
- arr[0]=200;
- }
-
- }
对引用类型的参数,形参式参数的改变,影响实际参数的值
设计一个方法用于数组遍历,要求遍历的结果是在一行上,如[11,22,33,44,55]
System.out.println("内容"); 输出的内容并换行
System.out.print("内容"); 输出内容不换行
- public class MethodDemo {
-
- public static void main(String[] agrs){
- int arr[]={11,22,33,44,55};
- printArray(arr);
-
- }
- public static void printArray(int arr[]){
- System.out.print("[");
- for(int i=0;i<arr.length;i++){
- //System.out.println(arr[i]);
- System.out.print(arr[i]);
- if(i<arr.length-1)
- {
- System.out.print(",");
- }
- }
- System.out.print("]");
- }
- }

- public class MethodDemo {
-
- public static void main(String[] agrs){
- int arr[]={80,50,60,90,100};
- System.out.println(getMax(arr));
-
- }
- public static int getMax(int[] arr){
- int max=arr[0];
- for(int x=1;x<arr.length;x++){
- if(arr[x]>max){
- max=arr[x];
- }
- }
- return max;
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。