Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我会犯这个错误?_Java_Classcastexception - Fatal编程技术网

Java 为什么我会犯这个错误?

Java 为什么我会犯这个错误?,java,classcastexception,Java,Classcastexception,我的项目中有一个Customer对象,它扩展了User对象 User.java public class User { private int userId; private int user_type; private String username; private String password; public User(int id, int user_type, String username, String password) { super(); this.us

我的项目中有一个Customer对象,它扩展了User对象

User.java

public class User {

private int userId;
private int user_type;
private String username;
private String password;

public User(int id, int user_type, String username, String password) {
    super();
    this.userId = id;
    this.user_type = user_type;
    this.username = username;
    this.password = password;
}

public int getUserId() {
    return userId;
}
public void setUserId(int userId) {
    this.userId = userId;
}

public int getUser_type() {
    return user_type;
}

public void setUser_type(int user_type) {
    this.user_type = user_type;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

}
还有我的Customer.java

public class Customer extends User{

private String cartId;

public Customer(int id, int user_type, String username, String password) {
    super(id, user_type, username, password);
    // TODO Auto-generated constructor stub
}

public String getCartId() {
    return cartId;
}

public void setCartId(String cartId) {
    this.cartId = cartId;
}
}
现在,我为我的网站创建了一个简单的登录,首先检查一个用户是否存在,如果存在,它会检查它是客户还是来自管理层。如果是客户,我有一个获取其购物车id的方法。然后我将用户转换为客户类型,然后通过setter设置购物车id

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    boolean userDoesNotExist = false;

    User user = ServiceFactory.userService().getUser(username);
    System.out.println(user.getPassword() + " " + user.getUsername());

    if(user != null){
        if(user.getUser_type() == 1){
            String cartId = ServiceFactory.customerService().getCartId(user.getUserId());
            Customer customer = (Customer) user;
            customer.setCartId(cartId);

            request.getSession().setAttribute("customer", customer);
            response.sendRedirect("customer");
        }else{

        }
    }else{
        userDoesNotExist = true;

        request.setAttribute("userDoesNotExist", userDoesNotExist);
        request.setAttribute("username", username);
        RequestDispatcherManager.dispatch(this, "/login.jsp", request, response);
    }
}
那么,回到我的问题,为什么我会得到一个

java.lang.ClassCastException: com.qbryx.domain.User cannot be cast to com.qbryx.domain.Customer
com.qbryx.servlets.LoginServlet.doPost(LoginServlet.java:50)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

这是正确的-是客户扩展了用户,而不是相反。将客户强制转换为用户总是可以的,但除非对象实际上是客户,否则将用户强制转换为客户将不会成功

如果您将extends从Java翻译成英语,您会发现每个客户都是用户,而不是evry用户

java.lang.ClassCastException:

当在不同的层次结构中进行向下投射和向上投射时,会发生此异常

在这里,您试图将User ObjectParent类强制转换为Customer对象子类

Customer customer = (Customer) user;// this is wrong since user is not a customer
为了避免ClassCastException,您可以检查运算符的用户实例

if(user instanceof Customer){
   customer = (Customer) user;
}