java - 线程“主”java.lang.ClassCastException 中的异常:com.sun.proxy.$Proxy0 类无法转换为类组员
问题描述
正如标题中所述,我将继续进行编码中的下一个假设部分。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 num, String service, String day) throws
RemoteException;
}
解决方案
接口必须具有“扩展远程”,这应该可以解决您的第二个问题“线程“主”java.lang.ClassCastException 中的异常:com.sun.proxy.$Proxy0 类不能转换为类...”
推荐阅读
- javascript - 邮递员表单数据和 x-www-form-urlencoded 工作,但原始 json 没有
- javascript - Vue Js,v-for循环,从深度Json数组中获取值
- javascript - 为什么我的文档 cookie 对象没有在 chrome 浏览器中存储任何 cookie?
- c# - Entity Framework Core 中两个实体之间的一对多和一对一关系
- java - 如何在我的跑步者中使用 toString 方法?
- javascript - 如何修复 npm 包中的功能问题?
- kubernetes - 版本“extensions/v1beta1”中没有类型“ReplicaSet”的匹配项
- deployment - Gatsby 动态路由在 gh-pages 部署时中断
- javascript - Svelte Sapper 在页面刷新时保留会话(重新加载)
- javascript - 带有参数的 javascript addEventListener 函数而不使用匿名函数