Tài liệu VẤN ĐỀ THAM TRỊ VÀ THAM BIẾN TRONG KỸ THUẬT LẬP TRÌNH PHÂN TÁN ĐỐI doc

3 1.3K 4
Tài liệu VẤN ĐỀ THAM TRỊ VÀ THAM BIẾN TRONG KỸ THUẬT LẬP TRÌNH PHÂN TÁN ĐỐI doc

Đang tải... (xem toàn văn)

Thông tin tài liệu

VẤN ĐỀ THAM TRỊ THAM BIẾN TRONG KỸ THUẬT LẬP TRÌNH PHÂN TÁN ĐỐI TƯỢNG  Kế thừa từ lớp Remote: đối tượng được tham khảo theo tham biến (GET OBJECT BY REFERENCE) Ví dụ: RemoteClass: (interface & implement): đối tượng được gọi từ xa MyRemoteClass.java import java.rmi.*; public interface MyRemoteClass extends Remote{ public int getMyAttribute() throws RemoteException; public void setMyAttribute(int value) throws RemoteException; } MyRemoteClassImpl.java import java.rmi.*; public class MyRemoteClassImpl implements MyRemoteClass{ private int myAttribute=0; public int getMyAttribute() throws RemoteException { return myAttribute; } public void setMyAttribute(int value) throws RemoteException { myAttribute=value; } } ServerClass : đăng đối tượng RemoteClass MyRemoteClassServer.java import java.rmi.server.*; import java.rmi.*; public class MyRemoteClassServer{ public static void main(String args[]){ try{ MyRemoteClassImpl c=new MyRemoteClassImpl(); System.out.println("Exporting MyRemoteClass ."); UnicastRemoteObject.exportObject(c); Naming.bind("rmi://localhost/MyRemoteClass",c); System.out.println("Register MyRemoteClass!"); while (true){ System.out.println("Value of c " +c.getMyAttribute()); } }catch(Exception e){ System.out.println(e); } } } ClientClass : sử dụng đối tượng RemoteClass MyRemoteClassClient.java import java.rmi.*; public class MyRemoteClassClient{ public static void main(String args[]){ try{ System.out.println("DEMO: GET OBJECT BY REFERENCE"); MyRemoteClass c=(MyRemoteClass)Naming.lookup("rmi://localhost/MyRemoteClass"); c.setMyAttribute(12); System.out.println("Value of c: " +c.getMyAttribute()); MyRemoteClass c1=(MyRemoteClass)Naming.lookup("rmi://localhost/MyRemoteClass"); System.out.println("Value of c1: " + c1.getMyAttribute()); c1.setMyAttribute(16); System.out.println("Value of c after c1 set to 16: " +c.getMyAttribute()); }catch(Exception e){ System.out.println(e); } } }  Hiện thực từ lớp Serializable: đối tượng được tham khảo theo tham trị RemoteClass: (interface & implement): đối tượng được gọi từ xa MyRemoteClass.java import java.rmi.*; public interface MyRemoteClass extends Remote{ public MySerializableClass myFunction(MySerializableClass c) throws RemoteException; } MyRemoteClassImpl.java import java.rmi.*; public class MyRemoteClassImpl implements MyRemoteClass{ public MySerializableClass myFunction(MySerializableClass c) throws RemoteException { c.setMyAttribute(c.getMyAttribute()*2);//Change c return c; } } SerializableClass : đối tượng làm tham số gọi qua RemoteClass MySerializableClass.java import java.io.*; public class MySerializableClass implements Serializable{ private int myAttribute=0; public int getMyAttribute() { return myAttribute; } public void setMyAttribute(int value) { myAttribute=value; } } ServerClass : đăng đối tượng RemoteClass MySerializableClassServer.java import java.rmi.server.*; import java.rmi.*; public class MySerializableClassServer{ public static void main(String args[]){ try{ MyRemoteClassImpl c=new MyRemoteClassImpl(); System.out.println("Exporting MyRemoteClass ."); UnicastRemoteObject.exportObject(c); Naming.bind("rmi://localhost/MyRemoteClass",c); System.out.println("Register MyRemoteClass!"); }catch(Exception e){ System.out.println(e); } } } ClientClass : sử dụng đối tượng RemoteClass MySerializableClassClient.java import java.rmi.*; public class MySerializableClassClient{ public static void main(String args[]){ try{ System.out.println("DEMO: GET OBJECT BY VALUE"); MyRemoteClass c=(MyRemoteClass)Naming.lookup("rmi://localhost/MyRemoteClass"); // MySerializableClass myData=new MySerializableClass(); myData.setMyAttribute(16); System.out.println("Data before call Remote Function: " + myData.getMyAttribute()); // MySerializableClass myNewData=c.myFunction(myData); // System.out.println("Data after call Remote Function: " + myData.getMyAttribute()); System.out.println("Return Data from Remote Function: " + myNewData.getMyAttribute()); }catch(Exception e){ System.out.println(e); } } } . VẤN ĐỀ THAM TRỊ VÀ THAM BIẾN TRONG KỸ THUẬT LẬP TRÌNH PHÂN TÁN ĐỐI TƯỢNG  Kế thừa từ lớp Remote: đối tượng được tham khảo theo tham biến (GET. }  Hiện thực từ lớp Serializable: đối tượng được tham khảo theo tham trị RemoteClass: (interface & implement): đối tượng được gọi từ xa MyRemoteClass.java

Ngày đăng: 24/12/2013, 18:15

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan