目前我有两节课.课堂班和学校班.我想在School类中编写一个方法来从教室类调用public void setTeacherName(String newTeacherName).
classroom.java
public class classroom {
private String classRoomName;
private String teacherName;
public void setClassRoomName(String newClassRoomName) {
classRoomName = newClassRoomName;
}
public String returnClassRoomName() {
return classRoomName;
}
public void setTeacherName(String newTeacherName) {
teacherName = newTeacherName;
}
public String returnTeacherName() {
return teacherName;
}
}
Run Code Online (Sandbox Code Playgroud)
School.java
import java.util.ArrayList;
public class School {
private ArrayList<classroom> classrooms;
private String classRoomName;
private String teacherName;
public School() {
classrooms = new ArrayList<classroom>();
}
public void addClassRoom(classroom newClassRoom, String theClassRoomName) {
classrooms.add(newClassRoom);
classRoomName = theClassRoomName;
}
// how to write a method to add a teacher to the classroom by using the
// classroom parameter
// and the teachers name
}
Run Code Online (Sandbox Code Playgroud)
ant*_*ant 17
您应该将类的名称大写.在你的学校课堂上这样做之后,
Classroom cls = new Classroom();
cls.setTeacherName(newTeacherName);
Run Code Online (Sandbox Code Playgroud)
此外,我建议您使用某种IDE,例如eclipse,它可以帮助您使用代码为您生成getter和setter.例如:右键单击Source - > Generate getters and setters
试试这个 :
public void addTeacherToClassRoom(classroom myClassRoom, String TeacherName)
{
myClassRoom.setTeacherName(TeacherName);
}
Run Code Online (Sandbox Code Playgroud)
class A{
public void methodA(){
new B().methodB();
//or
B.methodB1();
}
}
class B{
//instance method
public void methodB(){
}
//static method
public static void methodB1(){
}
}
Run Code Online (Sandbox Code Playgroud)
在学校,
public void addTeacherName(classroom classroom, String teacherName) {
classroom.setTeacherName(teacherName);
}
Run Code Online (Sandbox Code Playgroud)
顺便说一句,使用 Pascal Case 作为类名。另外,我建议将Map<String, classroom>教室名称映射到教室。
然后,如果您使用我的建议,这将起作用
public void addTeacherName(String className, String teacherName) {
classrooms.get(className).setTeacherName(teacherName);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
286344 次 |
| 最近记录: |