我正在编写一个 Java RMI 程序来计算数字的阶乘。
这是界面:
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface FactorialRMI extends Remote{
public void setNumber(int val) throws RemoteException;
public int calculateFactorial() throws RemoteException;
}
Run Code Online (Sandbox Code Playgroud)
那么这是实现该接口的类:
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class FactorialRMIImpl extends UnicastRemoteObject implements FactorialRMI{
private int number;
public FactorialRMIImpl(String name) throws RemoteException {
super();
try {
Naming.rebind("myFactorial",this);
}catch(Exception e) {
System.out.println("Exception: "+e.getMessage());
}
}
@Override
public void setNumber(int val) throws RemoteException {
this.number=val;
}
@Override
public int calculateFactorial() throws RemoteException {
int p=1;
for(int …Run Code Online (Sandbox Code Playgroud)