java方法

1. 什么是方法

  • 方法是语句的集合,完成一类的功能,解决某一类问题。

  • 方法包含在类或对象中。

  • 追求原子性和模块化

  • 就像是我们cc++中的函数

2. 方法的定义和调用

  • 仍旧是含有形参(定义时候)、实参(就是我们调用是赋的值)
  • 修饰符不分先后
修饰符 返回值类型 方法名(参数类型 参数名) {
	---
	方法体
	---
	return 返回值;  // void 就不需要
}

java中只有值传递

3. 方法重载

  • 和c ++中函数重载类似

  • 重载就是在一个类中,有相同的函数名称,但是形参不同的函数

  • 重载的规则

    • 方法名称不同
    • 形参列表必须不同(个数不同or类型不同or参数排列顺序等等).
    • 返回类型可以相同,也可以不同;
    • 仅仅返回类型不同算不上是方法的重载。
  • 底层实现是,编译器会根据调用方法实参的个数、参数的类型进行逐个匹配,已选择对应的重载的方法,若匹配失败,编译器就会报错。

package com.luckylight.package4;

public class demo1 {
    public static void main(String[] args) {
        System.out.println(max(1, 2));	// int max
        System.out.println(max(10, 1.0));	// double max
        System.out.println(max(1.2, 3.3));	// double max
    }
    public static int max(int a, int b) {
        System.out.println("int max");
        return a >= b ? a : b;
    }
    public static double max(double a, double b) {
        System.out.println("double max");
        return a >= b ? a : b;
    }
}

4. 命令行传参

  • 首先是找到 java 文件的路径(在package 下面),进入cmd
  • javac 该文件
  • 再cd命令回退 src 目录下,进行java 该class 文件
  • java 文件名 参数

文件示例如下所示(对main函数进行传参)

package com.luckylight.package4;

public class Demo2 {
    public static void main(String[] args) {
        for (int i = 0; i < args.length; i ++ ) {
            System.out.printf("args[%d]=%s
", i, args[i]);
        }
    }
}

5. 可变参数

  • 本质就是数组的运用
  • 使用说明:
    • 在方法声明中,在指定参数类型后加一个省略号(...)
    • 一个方法只能指定一个可变参数,同时他必须是方法的最后一个参数。任何普通的参数必须在他之前声明。
    • 而且注意传递的参数对应可变参数的类型
package com.luckylight.method;

public class Demo1 {
    public static void main(String[] args) {
        Demo1 demo1 = new Demo1();
        demo1.getMax(-110, 200, -15154, 9565, 15223);
        demo1.getMax(new double[]{1, 2, 3, 4, 4, 3929834, -123});   // 注意这个 new double[]
    }
    public void getMax(double... num) {
        if (num.length == 0) {
            System.out.println("There is no element");
        } else {
            double ret = num[0];
            for (int i = 1; i < num.length; i ++ ) {
                if (ret < num[i]) {
                    ret = num[i];
                }
            }
            System.out.println("The max num of the array is:" + ret);
        }
    }
}

6. 递归

  • 注意递归头,递归终点
  • 递归体,递归主体

先写一个简单的作业,实现加减乘除的计算器,并且可以循环接收新的数据,通过用户的交互实现。

package com.luckylight.method;

import java.util.Scanner;

public class Demo2 {
    static double a, b;
    static int opt;
    public static void main(String[] args) {
        Scanner scanner = new Scanner();
        String str = "";
        while (true) {
            if (scanner.hasNextLine())
                str = scanner.nextLine();
            int judge = dealInput(str);
            double res = 0;
            if (judge == -1) {      // 结束标志
                break;
            } else if (judge == 0) {    // 输入错误
                System.out.println("输入错误,请重新输入");
            } else {
                switch (opt) {
                    case 0:
                        res = a + b; break;
                    case 1:
                        res = a - b;    break;
                    case 2:
                        res = a * b;    break;
                    case 3:
                        res = a / b;    break;
                }
            }
            System.out.println(str + "=" + res);
        }
    }
    public static int dealInput(String str) {
        if (str.equals("-1")) {
            return -1;
        }
        int cnt_opt = 0;
        // 下面就需要要找到操作数,以及操作对象了,但是 str[i] 居然不行,先鸽了。
        /*for (int i = 0; i < str.length(); i ++ ) {
            if (str[i] >= '0' && str[i] <= '9')
                continue;

        }*/
    }
}	

原文地址:https://www.cnblogs.com/lucky-light/p/14480553.html