简体   繁体   English

如何在Java中调用另一个类中的方法?

[英]How to call a method in another class in Java?

Currently I have two classes. 目前我有两节课。 a classroom class and a School class. 课堂班和学校班。 I would like to write a method in the School class to call public void setTeacherName(String newTeacherName) from the classroom class. 我想在School类中编写一个方法来从教室类调用public void setTeacherName(String newTeacherName)。

classroom.java 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;
    }
}

School.java 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
}  

You should capitalize names of your classes. 您应该将类​​的名称大写。 After doing that do this in your school class, 在你的学校课堂上这样做之后,

Classroom cls = new Classroom();
cls.setTeacherName(newTeacherName);

Also I'd recommend you use some kind of IDE such as eclipse, which can help you with your code for instance generate getters and setters for you. 此外,我建议您使用某种IDE,例如eclipse,它可以帮助您使用代码为您生成getter和setter。 Ex: right click Source -> Generate getters and setters 例如:右键单击Source - > Generate getters and setters

Try this : 尝试这个 :

public void addTeacherToClassRoom(classroom myClassRoom, String TeacherName)
{
    myClassRoom.setTeacherName(TeacherName);
}
class A{
  public void methodA(){
    new B().methodB();
    //or
    B.methodB1();
  }
}

class B{
  //instance method
  public void methodB(){
  }
  //static method
  public static  void methodB1(){
  }
}

in School, 在学校,

public void addTeacherName(classroom classroom, String teacherName) {
    classroom.setTeacherName(teacherName);
}

BTW , use Pascal Case for class names. 顺便说一句 ,使用Pascal Case作为类名。 Also, I would suggest a Map<String, classroom> to map a classroom name to a classroom. 另外,我建议使用Map<String, classroom>将教室名称映射到教室。

Then, if you use my suggestion, this would work 然后,如果您使用我的建议,这将有效

public void addTeacherName(String className, String teacherName) {
    classrooms.get(className).setTeacherName(teacherName);
}

Instead of using this in your current class setClassRoomName("aClassName"); 而不是在当前类setClassRoomName("aClassName"); you have to use classroom.setClassRoomName("aClassName"); 你必须使用classroom.setClassRoomName("aClassName");

You have to add the class' and at a point like 你必须在类似的地方添加课程

yourClassNameWhereTheMethodIs.theMethodsName();

I know it's a really late answer but if someone starts learning Java and randomly sees this post he knows what to do. 我知道这是一个非常晚的答案,但如果有人开始学习Java并随机看到这篇文章他知道该怎么做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM