客户端获得自定义对象包路径必须和服务器端相同,当然在客户端也可以不用建该对象,可以将客户端的自定义对象打成jar包,然后在客户端引用。
松阳网站制作公司哪家好,找成都创新互联公司!从网页设计、网站建设、微信开发、APP开发、自适应网站建设等网站项目制作,到程序开发,运营维护。成都创新互联公司从2013年成立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联公司。
猜想用反射也可以实现改对象,但目前没有写相关例子。
pimport java.io.Serializable;/pppublic class Hello implements Serializable {
/**
*客户端必须有与服务器端相同的自定义对象
*/
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
*该方法名必须和服务接口一致
[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是实现PCI 接口的意思;
2.不是;这两个方法是必须写的,不能少;这两个方法来自接口中的,既然现实了PCI接口,就一定要实现接口中的所有方法
3.PCI nc= new NetworkCard(); 因为NetworkCard实现了PCI接口,PCI就类似是NetworkCard的父类,这个体现了面相对象编程中的多态;就好比你是一个男生,我可以说你是一个人吧,意思是一样的;
4.PCI nc = new PCI()这个是不能直接这样写的,因为PCI是接口,接口中没有构造方法,这个是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("生成数组如下: ");
for(int i: dist) {
System.out.print(i + "\t");
}
java.util.Scanner san = new java.util.Scanner(System.in);
System.out.print("输入要查找的数: ");
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 类的构造方法需要给重量参数,因为你只定义了一个构造方法。
我调试的没问题,改后代码:
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());
}
}