我需要一点帮助理解构造函数。它不是完整的代码,我只需要帮助理解一个部分。我的代码如下:Java构造函数语法问题
School.java
public class School {
private String name;
private int busNumber;
enter code here
public School (String name) {
this.name = name;
}
public String getSchoolName() {
return name;
}
public int getBusNumber() {
return bus Number;
}
Main.Java
System.out.println("Enter school number 1: ");
school1 = keyboard.nextLine();
School s1 = new School(school1);
System.out.println("Enter school number 2: ");
school2 = keyboard.nextLine();
School s2 = new School(school2);
System.out.println("School 1 is " + s1.getName());
System.out.println("School 2 is " + s2.getName());
System.out.println("Enter the bus number 1: ");
bus1 = keyboard.nextLine();
//现在我想要做的就是发送总线号码getBusNumber。
//如何发送bus1以便我可以使用s1.getBusNumber();稍后致电该号码!我觉得这应该是如此简单,但我无法理解它或找到如何在任何地方做到这一点。我也不想使用set函数。任何语法帮助将是真棒!
谢谢!
为什么你不想使用set函数吗?这似乎是使用一个完美的场景。 –
它适用于我正在参加的在线课程及其定义的不能改变的学校课程。为了学习的目的,我想,我希望我能以自己的方式去做。 – ImSobesBoosted
如果您提供的学校课程是整个学校课程,并且您无法更改它,那么真的没有办法在对象中设置busNumber。有更多的类定义吗? –