1. Trang chủ
  2. » Công Nghệ Thông Tin

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 đã được kiểm tra trùng lặp

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Vấn Đề Tham Trị Và Tham Biến Trong Kỹ Thuật Lập Trình Phân Tán Đối Tượng
Định dạng
Số trang 3
Dung lượng 110,42 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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 OBJECT BY REFERENCE Ví dụ: RemoteClass: int

Trang 1

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 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 ký đố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);

}

}

}

Trang 2

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;

}

Trang 3

ServerClass : đăng ký đố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);

}

}

}

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

TỪ KHÓA LIÊN QUAN

🧩 Sản phẩm bạn có thể quan tâm

w