判断方法的使用范围20250102

判断方法的使用范围20250102

实数顺序选择法

package com.oop.demo01;

public class Student {
    public static int add(int a, int b) {  //public 类下的public的静态方法
        return a+b;
    }
    static int sub(int a, int b) {    //public类 非public的静态方法
        return a-b;
    }
    public int devide(int a, int b) { //public 类下public的非静态方法
        return a/b;
    }
    void print_hello(){     //public类下 非public 非静态方法
        System.out.println("hello");
    }

}
class Student_excellent {
    String name;
    String address;
    int age;
    public static void Student_name(String name) {  //非public类的public 静态方法
        System.out.println(name);
    }
    static void Student_address(String address){    //非public类的 非public 的静态方法
        System.out.println(address);
    }
    public void Student_age(int age) { //非public类的 public 的非静态方法
        System.out.println(age);
    }
    void Student_name_age( String name,int age) { //非public类下的非public 非静态方法
        System.out.println(name+" "+age);

    }
}

同包下另一个java文件

package com.oop.demo01;

public class Teacher {
    public static void main(String[] args) {
        // 调用 Student 类的方法
        System.out.println(Student.add(5, 3));  // 调用 public static 方法
        System.out.println(Student.sub(10, 7)); // 调用包级私有 static 方法(同包内可访问)

        Student student = new Student();       // 实例化对象
        System.out.println(student.devide(9, 3)); // 调用 public 非静态方法

        // 调用 Student_excellent 类的方法
        Student_excellent.Student_name("Alice");   // 调用 public static 方法
        Student_excellent.Student_address("Beijing"); // 调用包级私有 static 方法(同包内可访问)

        Student_excellent studentEx = new Student_excellent(); // 实例化对象
        studentEx.Student_age(20); // 调用 public 非静态方法
        studentEx.Student_name_age("Bob",20);
    }
}

class Teacher_excellent {
    public static void main(String[] args) {
        // 调用 Student 类的方法
        System.out.println(Student.add(2, 1));  // 调用 public static 方法
        System.out.println(Student.sub(5, 2));  // 调用包级私有 static 方法(同包内可访问)

        Student student = new Student();       // 实例化对象
        System.out.println(student.devide(8, 2)); // 调用 public 非静态方法

        // 调用 Student_excellent 类的方法
        Student_excellent.Student_name("Bob");     // 调用 public static 方法
        Student_excellent.Student_address("Shanghai"); // 调用包级私有 static 方法(同包内可访问)

        Student_excellent studentEx = new Student_excellent(); // 实例化对象
        studentEx.Student_age(25); // 调用 public 非静态方法
        studentEx.Student_name_age("jack",25);
    }
}

public类里方法访问权限总结

方法 修饰符 是否静态 同包访问 跨包访问
add(int a, int b) public 是(静态) 直接用 Student.add(3, 4) import 后可以用 Student.add(3, 4)
sub(int a, int b) 默认(package-private) 是(静态) 直接用 Student.sub(3, 4) 不可访问(即使 Student 类被导入)
devide(int a, int b) public 否(非静态) 实例化后用 new Student().devide(3,4) import 后实例化:new Student().devide(3,4)
print_hello() 默认(package-private) 否(非静态) 实例化后用 new Student().print_hello() 不可访问(即使 Student 类被导入)

非public类里方法的访问权限总结

方法 修饰符 是否静态 同包访问 跨包访问
Student_name(String name) public 是(静态) 直接用类名调用:Student_excellent.Student_name("John") 不可访问(类本身不能跨包)
Student_address(String address) 默认(package-private 是(静态) 直接用类名调用:Student_excellent.Student_address("123 St.") 不可访问(类本身不能跨包)
Student_age(int age) public 否(非静态) 实例化后调用:new Student_excellent().Student_age(18) 不可访问(类本身不能跨包)
Student_name_age( String name,int age) 默认(package-private 否(非静态) 实例化后调用:new Student_excellent().Student_name_age("John",18) 不可访问(类本身不能跨包)

这里为什么有两个main函数?

在 Java 中,每个 main 方法都可以作为程序的独立入口点。这意味着如果一个项目中有多个包含 public static void main(String[] args) 方法的类,那么你可以分别运行这些类中的 main 方法,而不需要它们互相干扰。

你的代码中有两个类 TeacherTeacher_excellent,它们各自有一个 main 方法,因此:

  1. 每个 main 方法都是程序的入口点,可以独立运行。
  2. 在运行时,你可以选择运行 Teacher 类的 main 方法,也可以运行 Teacher_excellent 类的 main 方法。IDE(如 IntelliJ IDEA 或 Eclipse)会给出选择,或者通过命令行运行时可以指定具体的类。

为什么会有两个 main 方法?

这种情况在以下场景中比较常见:

  1. 测试不同的功能
    • 你可能会为每个类写一个 main 方法,用于测试该类的功能。
    • Teacher 测试 StudentStudent_excellent 的方法。
    • Teacher_excellent 也测试相同的内容,但可能用不同的数据。
  2. 模块化设计
    • 每个模块或类可以有自己的入口点,方便调试和测试。
    • 在开发过程中,多个类可能同时需要独立测试功能。
  3. 单独运行特定逻辑
    • 在较大的项目中,某些类可能有自己独立的运行逻辑,而不需要依赖于其他部分。

如何选择运行哪个 main 方法?

  1. 在 IDE 中

    • 右键单击你想运行的类文件(例如 Teacher.javaTeacher_excellent.java)。
    • 选择 "Run" 或 "Debug" 选项。
  2. 使用命令行

    • 编译所有文件:

      javac -d . *.java
      
    • 指定类名运行对应的

      main
      

      方法:

      java com.oop.demo01.Teacher
      

      java com.oop.demo01.Teacher_excellent
      

注意事项

  1. 代码组织问题
    • 如果项目最终需要部署或发布,建议保留一个主要的 main 方法作为入口点,其余的 main 方法可以删除或注释。
    • 多个 main 方法容易让代码显得混乱,尤其在大型项目中。
  2. 类的命名
    • 类名应该能够清晰表达其用途,比如 Teacher 类和 Teacher_excellent 类是测试类的话,可以考虑命名为 TeacherTestTeacherExcellentTest
  3. 单元测试的替代
    • 在现代 Java 项目中,通常使用单元测试框架(如 JUnit)来测试方法和类,而不是通过 main 方法。

你的代码完全可以运行,只要明确目标是测试功能或独立运行逻辑即可。如果你希望简化代码,可以选择保留一个 main 方法或者使用测试框架替代手动测试。

运行 Teacher.main()

public class Student {
    public static int add(int a, int b) {  //public 类下的public的静态方法
        return a+b;
    }
    static int sub(int a, int b) {    //public类 非public的静态方法
        return a-b;
    }
    public int devide(int a, int b) { //public 类下public的非静态方法
        return a/b;
    }
    void print_hello(){     //public类下 非public 非静态方法
        System.out.println("hello");
    }

}

image-20250102230211990

输出结果

8
3
3
Alice
Beijing
20
Bob 20

运行Teacher_excellent.main()

class Teacher_excellent {
    public static void main(String[] args) {
        // 调用 Student 类的方法
        System.out.println(Student.add(2, 1));  // 调用 public static 方法
        System.out.println(Student.sub(5, 2));  // 调用包级私有 static 方法(同包内可访问)

        Student student = new Student();       // 实例化对象
        System.out.println(student.devide(8, 2)); // 调用 public 非静态方法

        // 调用 Student_excellent 类的方法
        Student_excellent.Student_name("Bob");     // 调用 public static 方法
        Student_excellent.Student_address("Shanghai"); // 调用包级私有 static 方法(同包内可访问)

        Student_excellent studentEx = new Student_excellent(); // 实例化对象
        studentEx.Student_age(25); // 调用 public 非静态方法
        studentEx.Student_name_age("jack",25);
    }
}

image-20250102230348146

输出结果

3
3
4
Bob
Shanghai
25
jack 25
posted @ 2025-01-02 23:11  panghuhu~  阅读(19)  评论(0)    收藏  举报