JavaSE07笔记
发布日期:2021-05-18 05:20:09 浏览次数:25 分类:精选文章

本文共 6012 字,大约阅读时间需要 20 分钟。

Java方法学习笔记

知识点1:方法的声明

在Java编程中,方法是类中的一种成员,用于实现功能。以下是方法的声明相关知识点:

1. 方法的基本结构

方法的声明格式如下:

权限修饰符 返回值类型 方法名(形参列表) {    方法体}
  • 权限修饰符:如public, protected, private,默认为public
  • 返回值类型:可以是基本数据类型或引用数据类型,若无返回值使用void
  • 方法名:需满足命名规范,尽量用有意义的名称。
  • 形参列表:格式为类型 参数名,多个参数用逗号分隔。
  • 方法体:方法的具体实现逻辑。

2. 方法的调用与返回

  • 有返回值类型的方法:必须使用return语句返回相应的值。
  • 无返回值类型的方法:可使用return;结束方法执行。

3. 方法的重载

在同一个类中,方法名相同但参数列表不同即为重载。重载与权限修饰符、返回值类型无关,主要通过参数列表区分。

4. 可变形参方法

从Java 5.0开始支持可变形参,格式为...,允许传递0到多个参数。可变形参方法与其他方法重载,但不能与同名的数组方法共存。

知识点2:方法的重载

方法重载的概念

方法重载是指在同一个类中,相同方法名但参数列表不同的多个方法。重载的关键在于参数列表的差异。

代码示例

public class OverLoadTest {    public static void main(String[] args) {        OverLoadTest test = new OverLoadTest();        test.show();        test.show(1, 2);    }    public void show() {        System.out.println("hello");    }    public void show(int a) {        System.out.println("萨瓦迪卡");    }    void show(String info) {        System.out.println(info);    }    public int show(int i, int j) {        return i + j;    }    private void show(int i, String info) {    }    public void show(String info, int i) {    }    public String show(String info, int i) {        return info + i;    }}

重点

在调用方法时,首先通过方法名匹配,然后根据参数列表确定具体方法。

知识点3:可变形参的方法

可变形参的定义

可变形参方法允许传递多个参数,格式为类型 ... 参数名。可变形参方法与其他方法重载,但不能与同名的数组方法共存。

代码示例

public class ArgsTest {    public static void main(String[] args) {        ArgsTest test = new ArgsTest();        test.show("abc");        test.show("abc", "123", "xyz");        test.show(new String[]{"abc", "def"});    }    public void show() {        System.out.println("show()");    }    public void show(String s) {        System.out.println("show(String s)");    }    public void show(int i) {        System.out.println("show(int i)");    }    public void show(String... info) {        System.out.println("show(String... info)");        for (int i = 0; i < info.length; i++) {            System.out.println(info[i]);        }    }}

重点

可变形参方法在调用时可传递0到多个参数,适用于不确定参数数量的情况。

知识点4:方法参数的值传递机制

1. 变量传递机制

  • 基本数据类型:按值传递,赋值直接传递。
  • 引用数据类型:按引用传递,传递对象地址。

2. 方法参数传递机制

  • 形参:方法声明时的小括号内参数。
  • 实参:方法调用时传递的值或对象。

代码示例

public class VariableTest {    public static void main(String[] args) {        int m = 10;        int n = 20;        System.out.println("m = " + m + ", n = " + n);        ValueTransferTest2 test = new ValueTransferTest2();        test.sort(arr, "ascend");        System.out.println(Arrays.toString(arr));    }    public void sort(int[] arr, String sortMethod) {        if ("ascend".equals(sortMethod)) {            for (int i = 0; i < arr.length - 1; i++) {                for (int j = 0; j < arr.length - 1 - i; j++) {                    if (arr[j] > arr[j + 1]) {                        swap(arr, j, j + 1);                    }                }            }        } else if ("descend".equals(sortMethod)) {            for (int i = 0; i < arr.length - 1; i++) {                for (int j = 0; j < arr.length - 1 - i; j++) {                    if (arr[j] < arr[j + 1]) {                        swap(arr, j, j + 1);                    }                }            }        } else {            System.out.println("排序方式错误!");        }    }    public void swap(int i, int j) {        int temp = i;        i = j;        j = temp;    }    public void swap(int[] arr, int i, int j) {        int temp = arr[i];        arr[i] = arr[j];        arr[j] = temp;    }}

引用数据类型传递

public class ValueTransferTest1 {    public static void main(String[] args) {        Data data = new Data();        data.m = 10;        data.n = 20;        System.out.println("m = " + data.m + ", n = " + data.n);        ValueTransferTest1 test = new ValueTransferTest1();        test.swap(data);        System.out.println("m = " + data.m + ", n = " + data.n);        System.out.println(data);    }    public void swap(Data data1) {        int temp = data1.m;        data1.m = data1.n;        data1.n = temp;    }}

练习1

public class ValueTransferTest2 {    public static void main(String[] args) {        int[] arr = new int[]{34, 4, 2, 6, 6, 4, 7, -87, 0, 55, 98};        System.out.println(Arrays.toString(arr));        ValueTransferTest2 test = new ValueTransferTest2();        test.sort(arr, "ascend");        System.out.println(Arrays.toString(arr));    }    public void sort(int[] arr, String sortMethod) {        if ("ascend".equals(sortMethod)) {            for (int i = 0; i < arr.length - 1; i++) {                for (int j = 0; j < arr.length - 1 - i; j++) {                    if (arr[j] > arr[j + 1]) {                        swap(arr, j, j + 1);                    }                }            }        } else if ("descend".equals(sortMethod)) {            for (int i = 0; i < arr.length - 1; i++) {                for (int j = 0; j < arr.length - 1 - i; j++) {                    if (arr[j] < arr[j + 1]) {                        swap(arr, j, j + 1);                    }                }            }        } else {            System.out.println("排序方式错误!");        }    }    public void swap(int i, int j) {        int temp = i;        i = j;        j = temp;    }    public void swap(int[] arr, int i, int j) {        int temp = arr[i];        arr[i] = arr[j];        arr[j] = temp;    }}

补充内容

NullPointerException

NullPointerException是引用数据类型变量在操作时为空的异常。如以下示例:

public class NullPointerExceptionTest {    public static void main(String[] args) {        User u1 = new User();        u1.show();        System.out.println(u1.name);    }    class User {        String name;        public void show() {            String str = null;            System.out.println("我是一个用户");        }    }}

对象数组

public class StudentArrayTest {    public static void main(String[] args) {        Student[] stus = new Student[20];        for (int i = 0; i < stus.length; i++) {            stus[i] = new Student();            stus[i].number = i + 1;            stus[i].state = (int) (Math.random() * 6 + 1);            stus[i].score = (int) (Math.random() * 101);        }        for (int i = 0; i < stus.length; i++) {            stus[i].info();        }    }}

通过上述学习,可以更好地理解Java方法的使用与管理,提升对面向对象编程的理解。

上一篇:数据结构——链表有关
下一篇:JavaSE06笔记

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2025年04月24日 20时38分21秒