2016-10-01 6 views
-2

Userオブジェクトを拡張しているCustomerオブジェクトがプロジェクトにあります。なぜこのキャストエラーが発生しますか?

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を取得する方法があります。次に、ユーザーを顧客タイプにキャストして、セッターを介してカート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) 

答えて

1

これは正しいです - それはユーザー、周りにいない他の方法を拡張し、顧客の。顧客をユーザーにキャストするのはいつでもうまくいきますが、ユーザーが実際にカスタマーでない限り、ユーザーを顧客にキャスティングすることはできません。

英語から英語に「extend」を翻訳すると、すべての顧客はユーザーであるが、evryユーザーは顧客ではないということになります。

1

とjava.lang.ClassCastException:

(ダウンキャストとアップキャスト)を鋳造する際に、この例外が発生するさまざまな階層で発生します。ここで

あなたは顧客オブジェクトにユーザーオブジェクト(親クラス)をキャストしようとしている(子クラス)

Customer customer = (Customer) user;// this is wrong since user is not a customer 

ClassCastExceptionことができますユーザーinstanceofオペレータチェック

if(user instanceof Customer){ 
    customer = (Customer) user; 
} 
を避けるために、
関連する問題