简体   繁体   English

无法从另一个类[Java]调用方法

[英]Unable to call a method from another class [Java]

So I have three classes: Main, UserGroup, and User. 因此,我有三个类:Main,UserGroup和User。 In User I have three methods that are called in the UserGroup class and one which I am trying to call in the Main class. 在User中,我有三种在UserGroup类中调用的方法,一种是我试图在Main类中调用的方法。 Using the methods in the User class I have created an ArrayList in UserGroup that has three different strings in each element (sorry if wrong term) and using the final method in the User class I am trying to change one of these strings. 使用User类中的方法,我在UserGroup中创建了一个ArrayList,每个元素中都有三个不同的字符串(对不起,如果用错术语,很抱歉),并使用User类中的final方法,我试图更改这些字符串之一。

The method I am trying to call is 我尝试调用的方法是

public class User {
  String UserType;

  //some code removed

  public void setUserType(String x) {
      if ("user editor admin".contains(x)) {
          UserType = x;
      } 
      else {
        UserType = "editor";
      }
  }
}

I am trying to call this on a UserGroup called administrators that has an arraylist in it by putting this in main: 我试图通过将其放在main上的一个名为Administrators的UserGroup上来调用它:

editors.setUserType("editor");

as this is how I called the other methods from UserGroup but I get an error of 因为这是我从UserGroup调用其他方法的方式,但出现错误

error: cannot find symbol
        editors.setUserType("editor");
                      ^
symbol:   method setUserType(String)
location: variable editors of type UserGroup
1 error

EDIT (more of my main): 编辑(更多主要内容):

I create a UserGroup editors by 我通过创建一个UserGroup编辑器

UserGroup editors = new UserGroup();

and add values to the ArrayList with 并添加值到ArrayList与

editors.addValues();

and then print the elements 然后打印元素

editors.printData();

this all works as those methods are in the UserGroup class. 所有这些都可以使用,因为这些方法在UserGroup类中。

You maybe want to read into the topic overloading of methods in Java 您可能想要阅读Java中方法重载的主题

It looks for the method with the header 它寻找带有标题的方法

public void setUserType()

but you only defined the method 但是你只定义了方法

public void setUserType(String x)

So you should call the method you created with something like: 因此,您应该使用类似以下内容的方法来调用创建的方法:

editors.setUserType("Foobar") editors.setUserType(“ Foobar”)

It looks like editors is not actual a User. 看来编辑者不是实际的用户。 Maybe it is a List< User > in which case, it would be 也许这是一个List <用户>,在这种情况下,它将是

editors.get(i).setUserType(x)

Does UserGroup contain a collection of User objects? UserGroup是否包含User对象的集合?

setUserType(...) is defined on User, but doesn't seem to be defined on UserGroup. setUserType(...)在User上定义,但似乎在UserGroup上没有定义。

There are a couple of ways to do what you're trying to do. 有两种方法可以完成您想做的事情。

  1. Grab the list of User objects from your UserGroup, and call setUserType(...) on each one individually in main. 从您的UserGroup中获取User对象的列表,然后分别在main中分别调用setUserType(...)。 Something like: 就像是:

     List<User> users = editors.getUsers() //assuming UserGroup.getUsers() exists for(User user : users) user.setUserType("whatever"); 
  2. Add a method to UserGroup called setUserType(...) that iterates through its collection of User objects and sets the types: 向UserGroup添加一个名为setUserType(...)的方法,该方法将遍历其User对象集合并设置类型:

     public setUserType(String type) { for(User user : users) //assuming you have a collection of User objects user.setUserType(type); 

    } }

If I'm describing this incorrectly, please add the complete implementation of UserGroup (or at least the part that shows how Users are contained), and we can provide more info. 如果我的描述不正确,请添加UserGroup的完整实现(或至少显示用户如何包含的部分),我们可以提供更多信息。

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

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