-5

我有两节课:

模型/用户.java

package Model;

public class User {
    public int id;
    public String firstName;
    public String lastName;

    User( int id, String firstName, String lastName )
    {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

存储库/UserRespository.java

package Repositories;
import Model.User;

public class UserRepository 
{
    public User CreateUser( int id, String firstName, String lastName )
    {
        User newUser = new User( id, firstName, lastName );
        return newUser;
    }

    public User GetUserById()
    {
        User user = new User( id, firstName, lastName );
        return user;
    }
}

我收到这些错误:

firstName cannot be resolved    MVCExample/src/Repositories UserRepository.java line 14 1372765344696   53

id cannot be resolved   MVCExample/src/Repositories UserRepository.java line 14 1372765344695   52

lastName cannot be resolved MVCExample/src/Repositories UserRepository.java line 14 1372765344696   54

The constructor User(int, String, String) is not visible    MVCExample/src/Repositories UserRepository.java line 8  1372765344695   51

我究竟做错了什么?

4

5 回答 5

4

您应该将类​​的构造函数User设为public.

于 2013-07-02T11:45:13.593 回答
1
User user = new User( id, firstName, lastName );

您在没有声明的情况下使用它idfirstName并且lastName

于 2013-07-02T11:45:37.840 回答
0

在这部分代码中:

public User GetUserById()
{
    User user = new User( id, firstName, lastName );
    return user;
}

您没有声明 id、firstName 和 lastName,如果您希望它们可用,则需要将它们声明为实例变量,此外,在这部分:

User( int id, String firstName, String lastName )
{
    this.id = id;
    this.firstName = firstName;
    this.lastName = lastName;
}

您需要将您的构造函数声明为public

于 2013-07-02T11:46:29.537 回答
0

将您的方法更改为

 public User GetUserById(int id, String firstName, String lastName)
    {
        User user = new User( id, firstName, lastName );
        return user;
    }
于 2013-07-02T11:46:55.033 回答
0

将您的 User 类构造函数设为公开,例如:

public User( int id, String firstName, String lastName )
    {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }
于 2013-07-02T11:48:56.477 回答