客戶端獲得自定義對象包路徑必須和服務(wù)器端相同,當(dāng)然在客戶端也可以不用建該對象,可以將客戶端的自定義對象打成jar包,然后在客戶端引用。
松陽網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、自適應(yīng)網(wǎng)站建設(shè)等網(wǎng)站項目制作,到程序開發(fā),運營維護(hù)。成都創(chuàng)新互聯(lián)公司從2013年成立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)公司。
猜想用反射也可以實現(xiàn)改對象,但目前沒有寫相關(guān)例子。
pimport?java.io.Serializable;/pppublic?class?Hello?implements?Serializable?{??
/**?
*客戶端必須有與服務(wù)器端相同的自定義對象??
*/??
private?static?final?long?serialVersionUID?=?1L;??
private?String?id;??
private?String?name;??
public?String?getId()?{??
return?id;??
}??
public?void?setId(String?id)?{??
this.id?=?id;??
}??
public?String?getName()?{??
return?name;??
}??
public?void?setName(String?name)?{??
this.name?=?name;??
}??
}/p??
[java]?view?plain?copy
package?client;??
import?po.Hello;??
public?interface?IClientHelloManager?{??
/*??
[java]?view?plain?copy
*該方法名必須和服務(wù)接口一致??
[java]?view?plain?copy
*/??
[java]?view?plain?copy
public?Hello?example();??
[java]?view?plain?copy
pre?class="java"?name="code"package?client;??
import?java點虐 .MalformedURLException;?//import?java點虐 .URL;??
//import?org.codehaus.xfire.client.Client;??
import?org.codehaus.xfire.client.XFireProxyFactory;??
import?org.codehaus.xfire.service.Service;??
import?org.codehaus.xfire.service.binding.ObjectServiceFactory;??
import?po.Hello;??
public?class?ServicesClient?{??
public?static?void?main(String[]?arg)?throws?MalformedURLException,??
Exception?{??
String?xml?=?"";??
ObjectServiceFactory?objectServiceFactory?=?new?ObjectServiceFactory();??
Service?serviceModel?=?objectServiceFactory??
.create(IClientHelloManager.class);??
XFireProxyFactory?xFireProxyFactory?=?new?XFireProxyFactory();??
IClientHelloManager?service?=?(IClientHelloManager)?xFireProxyFactory??
.create(serviceModel,?xml);??
Hello?lHello?=?service.example();??
System.out.println(lHello.getId());??
System.out.println(lHello.getName());??
//?Client?client?=?new?Client(new?URL(??
//?""));??
//?Object[]?rsult?=?client.invoke("example",?new?Object[]?{?"hello"?});??
//?Hello?hello?=?(Hello)?rsult[0];??
//?System.out.println();??
}??
}/pre??
pre/pre??
pre/pre??
pre/pre
1.implements PCI是實現(xiàn)PCI 接口的意思;
2.不是;這兩個方法是必須寫的,不能少;這兩個方法來自接口中的,既然現(xiàn)實了PCI接口,就一定要實現(xiàn)接口中的所有方法
3.PCI nc= new NetworkCard(); 因為NetworkCard實現(xiàn)了PCI接口,PCI就類似是NetworkCard的父類,這個體現(xiàn)了面相對象編程中的多態(tài);就好比你是一個男生,我可以說你是一個人吧,意思是一樣的;
4.PCI nc = new PCI()這個是不能直接這樣寫的,因為PCI是接口,接口中沒有構(gòu)造方法,這個是new不出來對象的
public class Admins implements Userinterface{
private String name;
private String password;
private static int userNum;
public Admins() {
userNum ++;
}
public Admins(String name, String password) {
this.name = name;
this.password = password;
userNum ++;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return this.password;
}
public String toString() {
return "Total: " + Admins.userNum + "\nName: " + this.name + "\nPassword: " + this.password;
}
public static void main(String [] args) {
Userinterface [] users = new Userinterface[]{new Admins("jeryy", "11111"), new Admins("green", "123123")};
for(Userinterface u: users) {
System.out.println(u);
}
}
}
interface Userinterface{
public void setPassword(String password);
public String getPassword();
public String toString();
}
public class Bins{
public static void main(String [] args) {
int len = 10;
int [] dist = new int [len];
for(int i = 0; i dist.length; i ++) {
dist[i] = (int)(Math.random() * 100);
}
java.util.Arrays.sort(dist);
System.out.println("生成數(shù)組如下: ");
for(int i: dist) {
System.out.print(i + "\t");
}
java.util.Scanner san = new java.util.Scanner(System.in);
System.out.print("輸入要查找的數(shù): ");
int key = san.nextInt();
int res = Bins.binSearch(dist, key);
//System.out.println(res);
String info = (res =0 ) ? (key + ":" + res) : "查找失敗";
System.out.println(info);
}
public static int binSearch(int [] dist, int key) {
java.util.Arrays.sort(dist);
return java.util.Arrays.binarySearch(dist, key);
}
}
三個錯:
1.Chicken 類里的howtoeat方法改howtoEat;
2.Apple類和Orange 放到Fruit 類外面。
3。Chicken 類的構(gòu)造方法需要給重量參數(shù),因為你只定義了一個構(gòu)造方法。
我調(diào)試的沒問題,改后代碼:
interface Edible{
public String howtoEat();
}
class Animal{
}
class Chicken extends Animal implements Edible,Comparable{
int weight;
public Chicken(int weight){
this.weight=weight;
}
public String howtoEat(){
return "fry it";
}
public int compareTo(Object o){
return weight-((Chicken)o).weight;
}
}
class Tiger extends Animal{
}
abstract class Fruit implements Edible
{}
class Apple extends Fruit {
public String howtoEat(){
return "Make apple cider";
}
class Orange extends Fruit{
public String howtoEat(){
return "Make orange juice";
}
}
}
public class Test{
public static void main(String[] args){
Object[] objects={new Tiger(),new Chicken(10),new Apple()};
for(int i=0;iobjects.length;i++){
showObject(objects[i]);
}
}
public static void showObject(Object object){
if(object instanceof Edible)
System.out.println(((Edible)object).howtoEat());
}
}