我有两个班级:
模型/ User.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
我做错了什么?
答案 0 :(得分:4)
您应该将类User
的构造函数设为public
。
答案 1 :(得分:1)
User user = new User( id, firstName, lastName );
您使用此功能时未声明id
,firstName
和lastName
答案 2 :(得分: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 。
答案 3 :(得分:0)
将您的方法更改为
public User GetUserById(int id, String firstName, String lastName)
{
User user = new User( id, firstName, lastName );
return user;
}
答案 4 :(得分:0)
将您的User类构造函数设为public,例如:
public User( int id, String firstName, String lastName )
{
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}