• Java方法


    一.什么是方法

      所谓方法就是用来解决一类问题的代码的有序集合,是一个功能模块。

    二.方法声明

     

       访问修饰符:public/private/protected

       返回类型:void及各种数据类型;

       方法名:和变量一样

    三.方法分类

      

      方法在类的内部定义

      1.无参无返回值:

     1 package com.imooc.function;
     2 
     3 public class MethodDemo1 {
     4     //打印星号
     5     public void printStar(){
     6         System.out.println("********************");
     7     }
     8 
     9     public static void main(String[] args) {
    10         // TODO Auto-generated method stub
    11         MethodDemo1 myMethod=new MethodDemo1();
    12         myMethod.printStar();
    13         System.out.println("哈哈");
    14         myMethod.printStar();
    15 
    16     }
    17 
    18 }
    View Code

      2.无参带返回值:

     1 package com.imooc.function;
     2 
     3 public class MethodDemo1 {
     4     //打印星号
     5     public int Rectangle(){
     6         int length=10,width=3;
     7         int getArea=length*width;
     8         return getArea;
     9     }
    10 
    11     public static void main(String[] args) {
    12         // TODO Auto-generated method stub
    13         int result;
    14         MethodDemo1 myMethod=new MethodDemo1();
    15         System.out.println("面积为:"+myMethod.Rectangle());
    16     }
    17 
    18 }
    View Code

      3.带参无返回值:

     1 package com.imooc.function;
     2 
     3 public class MethodDemo1 {
     4     public float max(float a,float b){
     5         float max;
     6         if(a>b){
     7             max=a;
     8         }
     9         else{
    10             max=b;
    11         }
    12         SYstem.out.print(a+"和"+b+"的最大值是:"+max);
    13     }
    14     
    15     public static void main(String[] args) {
    16         // TODO Auto-generated method stub
    17         MethodDemo1 myMethod=new MethodDemo1();
    18         float a=10.45f,b=5.12f;
    19         myMethod.max(a, b);
    20     }
    21 
    22 }
    View Code

      4.带参带返回值:

     1 package com.imooc.function;
     2 
     3 public class MethodDemo1 {
     4     public int fac(int n){
     5         int s=1;
     6         int sum=0;
     7         for(int i=1;i<=n;i++){
     8             for(int j=1;j<=i;j++){
     9                 s*=j;
    10             }
    11             sum+=s;
    12             s=1;
    13         }
    14         return sum;
    15     }
    16     
    17     public static void main(String[] args) {
    18         // TODO Auto-generated method stub
    19         MethodDemo1 myMethod=new MethodDemo1();
    20         System.out.println("大的为为:"+myMethod.fac(3));
    21     }
    22 
    23 }
    View Code

      注:Java方法不能嵌套定义(而Python可以)

    四.数组作为参数  

      例:

     1 package com.imooc.function;
     2 
     3 import java.util.Scanner;
     4 
     5 public class GetValue {
     6     public boolean search(int n,int[] arr){
     7         boolean flag=false; 
     8         for(int val:arr){
     9             if(val==n){
    10                 flag=true;
    11                 break;
    12             }
    13         }
    14         return flag;
    15     }
    16     public static void main(String[] args) {
    17         // TODO Auto-generated method stub
    18         System.out.println("请输入要查找的数据:");
    19         int num;
    20         boolean flag;
    21         Scanner sc=new Scanner(System.in);
    22         num=sc.nextInt();
    23         int[] arr={1,3,5,7,8,2,10,20};
    24         GetValue getvalue=new GetValue();
    25         flag=getvalue.search(num, arr);
    26         if(flag){
    27             System.out.print("找到了");
    28         }
    29         else{
    30             System.out.print("未找到");
    31         }
    32 
    33 
    34     }
    35 
    36 }
    View Code

    五.方法重载

      方法名相同,参数列表不同。

      例:

     1 package com.imooc.function;
     2 
     3 public class MathAdd {
     4     //求int类型和
     5     public int plus(int m,int n){
     6         return m+n;
     7     }
     8     //求double类型和
     9     public double plus(double m,double n){
    10         return m+n;
    11     }
    12     //q求数组元素累加和
    13     public int plus(int[] arr){
    14         int sum=0;
    15         for(int n:arr){
    16             sum+=n;
    17         }
    18         return sum;
    19     }
    20     public static void main(String[] args) {
    21         MathAdd mathadd=new MathAdd();
    22         //int型
    23         int intplus=mathadd.plus(1,2);
    24         System.out.println("int型和:"+intplus);
    25         double doubleplus=mathadd.plus(1.23,3.468);
    26         System.out.println("double型和:"+doubleplus);
    27         int[] arr={1,2,3,4,5,6};
    28         int arrplus=mathadd.plus(arr);
    29         System.out.println("数组型和:"+arrplus);
    30 
    31         
    32         
    33     }
    34 
    35 }
    View Code

     六.传值

      如果在同一个类里面,某一个方法(非main方法)要调用其他方法,直接调用,而不需要创建对象。

      交换值例(基本数据类型):主方法中的m,n值不会变(方法中的值改变不会影响主方法的),只是把值传递给a,b

     1 package com.imooc.function;
     2 
     3 public class SwapDemo {
     4     
     5     public void swap(int a,int b){
     6         System.out.println("交换前:a="+a+"n="+b);
     7         int temp;
     8         temp=a;
     9         a=b;
    10         b=temp;
    11         System.out.println("交换后:a="+a+"b="+b);
    12 
    13     }
    14     public static void main(String[] args) {
    15         int m=3,n=5;
    16         SwapDemo swapdemo=new SwapDemo();
    17         System.out.println("交换前:m="+m+"n="+n);
    18         swapdemo.swap(m, n);
    19         System.out.println("交换后:m="+m+"n="+n);
    20 
    21     }
    22 
    23 }
    View Code

       数组传值(引用数据类型)例:

     1 package com.imooc.function;
     2 
     3 public class ListDemo {
     4     
     5     public void change(int[] arr){
     6         arr[3]=-1;
     7         System.out.println("arr修改数据后:");
     8         for(int n:arr){
     9             System.out.print(n+" ");
    10         }
    11         System.out.println();
    12 
    13     }
    14     
    15     public static void main(String[] args) {
    16         int[] a={1,2,3,4,5,6,7};
    17         ListDemo listdemo=new ListDemo();
    18         System.out.println("a修改前:");
    19         for(int n:a){
    20             System.out.print(n+" ");
    21         }
    22         System.out.println();
    23         listdemo.change(a);
    24         System.out.println("a修改后:");
    25         for(int n:a){
    26             System.out.print(n+" ");
    27         }
    28         
    29     }
    30 
    31 }
    32 /*
    33  输出:
    34     a修改前:
    35     1 2 3 4 5 6 7 
    36     arr修改数据后:
    37     1 2 3 -1 5 6 7 
    38     a修改后:
    39     1 2 3 -1 5 6 7 
    40 */
    View Code

     Java中参数都是值传递:

       https://www.zhihu.com/question/31203609

     注:Python中都是共享变量     

    七.可变参数列表

      可变参数列表:如int...n(参数中如果有两个以上参数,可变参数一定是放在最后的位置。且可以把数组的值传递给可变参数列表,但是数组作为参数时不可以将多个值传递给数组,一个方法中只能有一个可变参数)

      注:Python为(*args【元组】,**kwargs【字典】)

     1 package com.imooc.function;
     2 
     3 public class ArgsDemo {
     4     
     5     public void sum(int...n){
     6         int sum=0;
     7         for(int i:n){
     8             sum+=i;
     9         }
    10         System.out.println("sum="+sum);
    11     }
    12     public static void main(String[] args) {
    13         ArgsDemo argsdemo=new ArgsDemo();
    14         argsdemo.sum(1);
    15         argsdemo.sum(1,2);
    16         argsdemo.sum(1,2,3);
    17 
    18 
    19     }
    20 
    21 }
    22 /*
    23  输出:
    24       sum=1
    25       sum=3
    26       sum=6
    27 
    28  
    29  */
    View Code

       可变参数重载问题:可变参数列表所在方法是最后被访问的

     1 package com.imooc.function;
     2 
     3 public class Argsdemo4 {
     4     public int plus(int a,int b){
     5         System.out.println("不可变参数方法被调用");
     6         return a+b;
     7     }
     8     public int plus(int...n){
     9         System.out.println("可变参数列表方法被调用");
    10         int sum=0;
    11         for(int i:n){
    12             sum+=i;
    13         }
    14         return sum;
    15     }
    16     public static void main(String[] args){
    17         Argsdemo4 argsdemo=new Argsdemo4();
    18         System.out.print("和为:"+argsdemo.plus(1,2));
    19     }
    20 }
    21 /*输出:
    22      不可变参数方法被调用
    23     和为:3
    24 */
    View Code

      文档注释:/**(可以使用javadoc命令获取文档注释生成帮助文档)

        

    标签描述示例
    @author 标识一个类的作者 @author description
    @deprecated 指名一个过期的类或成员 @deprecated description
    {@docRoot} 指明当前文档根目录的路径 Directory Path
    @exception 标志一个类抛出的异常 @exception exception-name explanation
    {@inheritDoc} 从直接父类继承的注释 Inherits a comment from the immediate surperclass.
    {@link} 插入一个到另一个主题的链接 {@link name text}
    {@linkplain} 插入一个到另一个主题的链接,但是该链接显示纯文本字体 Inserts an in-line link to another topic.
    @param 说明一个方法的参数 @param parameter-name explanation
    @return 说明返回值类型 @return explanation
    @see 指定一个到另一个主题的链接 @see anchor
    @serial 说明一个序列化属性 @serial description
    @serialData 说明通过writeObject( ) 和 writeExternal( )方法写的数据 @serialData description
    @serialField 说明一个ObjectStreamField组件 @serialField name type description
    @since 标记当引入一个特定的变化时 @since release
    @throws 和 @exception标签一样. The @throws tag has the same meaning as the @exception tag.
    {@value} 显示常量的值,该常量必须是static属性。 Displays the value of a constant, which must be a static field.
    @version 指定类的版本 @version info

      方法的调式:F5进入方法,F7由方法内部放回调用处

    八.实现简单综合例子

     1 package com.imooc.function;
     2 
     3 import java.util.Scanner;
     4 
     5 public class TotalDemo {
     6     //在末尾插入数据
     7     public int[] insertData(int[] arr){
     8         Scanner sc=new Scanner(System.in);
     9         for(int i=0;i<arr.length-1;i++){
    10         System.out.println("请输入第"+(i+1)+"个整数:");
    11         arr[i]=sc.nextInt();
    12         }
    13         return arr;
    14         
    15     }
    16     //展示数据
    17     public void showData(int[] arr,int length){
    18         if(length<=arr.length){
    19         System.out.println("数组的前"+length+"个数为:");    
    20         for(int i=0;i<length;i++){
    21             System.out.print(arr[i]+" ");
    22             
    23         }
    24         }
    25         else{
    26             System.out.println("数组长度越界了!");
    27         }
    28         System.out.println();
    29         }
    30     //在指定位置插入数据
    31     public int[] insertatArray(int[] arr,int n,int k){
    32         for(int i=arr.length-1;i>n;i--){
    33             arr[i]=arr[i-1];
    34         }
    35         arr[n]=k;
    36         return arr;
    37     }
    38     //查询能被n整除的数
    39     public void div(int[] arr,int n){
    40         for(int i:arr){
    41             if(i%n==0){
    42                 System.out.print(i+" ");
    43             }
    44         }
    45         System.out.println();
    46     }
    47     public void notice(){
    48         System.out.println("***************************");
    49         System.out.println("        1-插入数据 ");
    50         System.out.println("        2-显示数据 ");
    51         System.out.println("        3-在指定位置插入数据 ");
    52         System.out.println("        4-查询能被n整除数据 ");
    53         System.out.println("        0-退出");
    54         System.out.println("***************************");
    55 
    56         
    57     } 
    58     public static void main(String[] args) {
    59         // 实现增删改查
    60         TotalDemo total=new TotalDemo();
    61         Scanner sc=new Scanner(System.in);
    62         int[] arr=new int[10];
    63         while(true){
    64             total.notice();
    65             int n=sc.nextInt();
    66             if(n==0){
    67                 System.out.println("您退出了");
    68                 break;
    69                 
    70             }
    71             switch(n){
    72             case 1:{arr=total.insertData(arr);break;}
    73             case 2:total.showData(arr, arr.length);break;
    74             case 3:{
    75                 System.out.println("请输入插入的位置:");
    76                 int n1=sc.nextInt();
    77                 System.out.println("请输入插入的值:");
    78                 int k1=sc.nextInt();
    79                 total.insertatArray(arr, n1, k1);
    80                 break;}
    81             case 4:{
    82                 System.out.println("请输入被除数:");
    83                 int n1=sc.nextInt();
    84                 total.div(arr, n);
    85                 break;
    86             }
    87             default:{
    88                 System.out.println("输入错误");break;
    89             }
    90 
    91             }
    92         }
    93 
    94     }
    95 
    96 }
    View Code
  • 相关阅读:
    iOS 多线程/GCD
    iOS推送通知的实现步骤
    Swift中文教程-学习
    设计模式——观察者模式
    SSM学习
    Servlet 学习
    java基础
    DOM中节点
    会议管理系统设计
    springboot与thymeleaf 整合
  • 原文地址:https://www.cnblogs.com/lyq-biu/p/10666750.html
Copyright © 2020-2023  润新知