22-方法







方法

  • System.out.println()System 系统类中的 out 对象的 println 方法

  • JAVA 方法时语句的集合,他们在一起执行一个功能

    • 方法是解决一类问题的步骤的有序组合

    • 方法包含于类或对象中

    • 方法在程序中被创建,在其他地方被引用

  • 设计方法的原则:方法的本意是功能块,就是实现某个功能的语句块的集合。我们设计方法的时候,最好保持方法的原子性。就是一个方法只完成1个功能,这样利于我们后期的扩展。

  • 一个简单的方法

    public class Demo_01 {
        public static void main(String[] args) {
            int sum = add(1,2);
            System.out.println(sum);
        }
        public static int add(int a,int b) {
            return a + b;
        }
    }

    可以获得 a+b 的结果

  • 或者可将之前 1~1000 能被5整除的数代码调用过来

    public class Demo_01 {
        public static void main(String[] args) {
            test();
        }
        public static void test() {
            int sum = 0;
            for (int i = 0; i <= 1000; i++) {
                if (i % 5 == 0) {
                    if (sum % 3 == 0) {
                        System.out.print('\n');
                    }
                    sum++;
                    System.out.print(i + "\t");
                }
            }
        }
    }
  • JAVA 的方法类似于其他语言的函数,是 一段用来完成特定功能的代码片段 ,一般情况下,定义一个方法包含以下语法

    修饰符 返回值类型 方法名 (参数类型 参数名) {
        ···
        方法体
        ···
        return 返回值;
    }
  • 方法包含一个方法头和一个方法体 ,下面是一个方法的所有部分

    • 修饰符: 修饰符,这是可选的,告诉编译器如何调用该方法。定义了该方法的访问类型

    • 返回值类型: 方法可能会返回值。returnValueType 是方法返回值的数据类型。有些方法执行所需的操作,但没有返回值。在这种情况下, returnValueType 是关键字 boid

    • 方法名: 是方法的实际名称。方法名和参数表共同构成方法签名

    • 参数类型: 参数像是一个占位符。当方法被调用时,传递值给参数。这个值被称为实参或变量。参数列表是值方法的参数类型、顺序和参数的个数。参数是可选的,方法可以不包含任何参数

      • 形式参数: 在方法被调用时用于接收外界输入的数据

      • 实参: 调用方法时实际传给方法的数据

    • 方法体: 方法体包含具体的语句,定义该方法的功能

  • 比大小 方法示例

    public class Demo_02 {
        public static void main(String[] args) {
            int max = max (10,20);
            System.out.println(max);
        }
        public static int max(int a,int b) {
            int result = 0;
            if (a > b) {
                result = a;
            }else if (a < b) {
                result = b;
            }else {
                System.out.println("a==b");
                return 0;
            }
            return result;
        }
    } result;
        }
    }
    • 方法调用

      • 调用方法: 对象名.方法名(实参列表)

      • JAVA 支持两种调用方法的方式,根据方法是否返回值来选择

      • 当方法返回一个值的时候,方法调用通常被当作一个值,例如

        int larger = max(10,40);
      • 如果方法返回值是 void ,方法调用一定是一条语句

        System.out.println("Hello,World")
  • 找到范围内的所有 奇数 和 偶数

    public class EvenOdd_Demo_01 {
        public static void main(String[] args) {
            evenOdd(10,20);
        }
        public static void evenOdd (int min,int max) {
            int schedule = 0;
            int i = max - min;
            if (max >= min) {
                for (schedule = 0;schedule <= i;schedule++) {
                    int schedule_2 = min + schedule;
                    if (schedule_2 % 2 == 0) {
                        System.out.print(schedule_2 + "是偶数\t");
                    }
                }
                System.out.println();
                for (schedule = 0;schedule <= i;schedule++) {
                    int schedule_2 = min + schedule;
                    if (schedule_2 % 2 != 0) {
                        System.out.print(schedule_2 + "是奇数\t");
                    }
                }
            }else {
                System.out.println("max 小于 min");
            }
        }
    }


暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇