通过在无构造方法中设置(传递)参数的方式
第一个类:
public class Student {
public Student(String task) {
if(task == "F15"){
this.show();
}
}
public void show(){
System.out.println("能通过给有参构造方法传入参数获取show方法");
}
}
第二个类:
public class Hello {
public static void main(String[] args) {
Student s = new Student("F15");
}
}
运行结果:
能通过给有参构造方法传入参数获取show方法

本文展示了一个通过构造方法传递参数来调用类方法的Java示例。具体来说,在`Student`类中定义了一个带有字符串参数的构造方法,当参数为F15时,会触发`show`方法的执行并打印一条消息。

被折叠的 条评论
为什么被折叠?



