线程“主”中的异常java.lang.ClassCastException:类com.sun.proxy$ Proxy0无法强制转换为类crewcut

问题描述

按照标题中所述, 因为我要继续编码的下一个假定部分。 RMI系统未根据我的输入显示操作。当系统将输入识别为客户/理发店时,我希望它显示编码的下一部分。

我如何显示一个案例声明?

客户

import java.rmi.*;
import java.net.*;
import java.io.*;
import java.util.*;

public class crewcut_client {

public static void main(String[] args) {


    try{
        Scanner sc = new Scanner(system.in);

        System.out.println("Welcome to CrewCut Service");
        System.out.println("Are you a 'CUSTOMER' | 'BARBER' ? Type in below");
        String resp = sc.next();

        crewcut cc = (crewcut) Naming.lookup("rmi://localhost/CrewCut");
        switch (resp){
            case "CUSTOMER":
            case "customer":
            case "Customer":
            {
                System.out.println("Hello Customer !");
                System.out.println("Welcome to our system");
                System.out.println("Please enter your name :");
                String name = sc.nextLine();
                System.out.println("Please enter your contact num :");
                String num = sc.next();
                System.out.println("Here are our services :");
                System.out.println("Hair Cut RM15");
                System.out.println("Hair Wash RM20");
                System.out.println("Hair Dye RM25");
                System.out.println("Press '1' for Hair Cut");
                System.out.println("Press '2' for Hair Wash");
                System.out.println("Press '3' for Hair Dye");
                String service = sc.next();
                System.out.println("Your day of booking ? Monday | Tuesday | Wednesday | Thursday | Friday |Saturday | Sunday");
                String day = sc.next();
                //impl code
                System.out.println(cc.check_client(name,num,service,day));
            break;
            }
            case "BARBER":
            case "barber":
            case "Barber":
            {
                System.out.println("Hello Barber !");
                System.out.println("Are you in for work today? Enter 'Y' for YES or 'N' for NO" );
                String attend = sc.nextLine();
                if (attend.equals("Y")){
                    System.out.println("Welcome to work ! ");
                    System.out.println("The shop shall be closed in 9pm");
                }else if (attend.equals("N")){
                    System.out.println("Hope to see you soon !");
                }else{
                    System.out.println("Invalid input");
                }
            }
        }
    }catch (remoteexception re){
        re.printstacktrace();
    }catch (NotBoundException nbe){
        nbe.printstacktrace();
    }catch(MalformedURLException mfe){
        mfe.printstacktrace();
    }
}}

服务器

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.remoteexception;

public class crewcut_server {
public static void main(String[] args) {
    try{
        crewcutimpl ccl = new crewcutimpl();
        Naming.rebind("rmi://localhost/CrewCut",ccl);
    }catch (remoteexception re){
        re.printstacktrace();
    }catch (MalformedURLException mfe){
        mfe.printstacktrace();
    }
}
}

实施

import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import java.util.Scanner;

public class crewcutimpl extends UnicastRemoteObject implements crewcut{
public  crewcutimpl() throws remoteexception{
    super();
}
public String check_client (String name,String num,String service,String day){
    String display;
    int price = 0;
    switch (service){
        case "1":
            price = 15;
            break;
        case "2" :
            price = 20;
            break;
        case "3":
            price = 25;
            break;
    }display =" Your booking has been entered into the system Mr/Mrs " + name + "\n We shall contact 
     you through this num : " + num + "\n Service wanted : "+ service + "Total price :" + price +"Day 
     of appointment :" + day;
    return display;

    }
    }

接口

import java.rmi.remoteexception;

 public interface crewcut {

public String check_client(String name,String day) throws 
remoteexception;
}

解决方法

接口必须有“extends Remote”来解决你的第二个问题 “线程“main”java.lang.ClassCastException 中的异常:类 com.sun.proxy.$Proxy0 不能转换为类...”