简体   繁体   English

Java接口和实现

[英]Java Interfaces and Implementation

I am consuming a webservice using my java web application.This returns a number of data among which there is a attribute named Consumer . 我正在使用我的java web应用程序来使用web服务。这会返回一些数据,其中有一个名为Consumer的属性。 This is represented as an object when the WSDL is converted to Java code. 当WSDL转换为Java代码时,这表示为对象。 There is only one string variable id inside this class. 此类中只有一个字符串变量id There is another class Person which extends Consumer . 还有另一个Person扩展Consumer This has a number fields like firstName , lastName etc. In the JSF code , there is a reference to consumer.firstName and the value pertaining to firstName is being printed properly when person object is returned as part of response xml. 这有一些数字字段,如firstNamelastName等。在JSF代码中,有一个对consumer.firstName的引用,当person对象作为响应xml的一部分返回时,正确打印了与firstName有关的值。 This confuses me a bit because consumer object does not contain firstName and still the value is getting printed properly when consumer.firstName is used. 这让我感到困惑,因为消费者对象不包含firstName并且在使用consumer.firstName时仍然可以正确打印该值。 Please can you help me in understanding this. 请你帮我理解一下。

JSF Code JSF代码

<h:outputText value="#{myBean.consumer.firstName}" />

Backing Bean 支持Bean

public Consumer getConsumer() {
  return consumer;
}

Consumer Class 消费者阶层

public class Consumer implements java.io.Serializable {

  private java.lang.String id;

  public java.lang.String getId() {
    return this.id;
  }

  public void setId(java.lang.String id) {
    this.id = id;
  }

}

Person Class 人类

public class Person extends Consumer {
     private String firstName;
     private String lastName;
     private String dateOfBirth;

     // Getters and Setters
}

JSF uses reflection to call the methods on your objects. JSF使用反射来调用对象上的方法。 It doesn't know what the type of myBean.consumer is. 它不知道myBean.consumer的类型是什么。 All it knows is that you asked the firstName of this object. 它只知道你问了这个对象的firstName So, what it does is 那么,它的作用是什么

  1. Find an attribute, in any scope, named myBean 在任何范围内查找名为myBean的属性
  2. if not null, find if there is a public no-arg method named "getConsumer" in the class (or any superclass or interface) of this object 如果不为null,查找此对象的类(或任何超类或接口)中是否存在名为“getConsumer”的公共no-arg方法
  3. Call this method, and get the result: another object 调用此方法,并获得结果:另一个对象
  4. if this other object is not null, find if there is a public no-arg method named "getFirstName" in the class (or any superclass or interface) of this other object 如果此其他对象不为null,则查找此另一个对象的类(或任何超类或接口)中是否存在名为“getFirstName”的公共no-arg方法
  5. Call this method 调用此方法

So, even if the method getConsumer() declares that it returns a Consumer , if, at runtime, the actual object returned by getConsumer() is a Person , JSF will search for a getFirstName() method in Person , it will find it, and will happily call it and outut its result. 因此,即使方法getConsumer()声明它返回一个Consumer ,如果在运行时, getConsumer()返回的实际对象是Person ,JSF将在Person搜索getFirstName()方法,它会找到它,并乐意称之为并结果。

When at JSF you refer to consumer.firstName , it's not the type Consumer you are accesing, but an attribute of your page/request/session. 在JSF中,您引用的是consumer.firstName ,它不是您正在访问的Consumer类型,而是您的页面/请求/会话的属性 This attribute can be set as of a concrete type ( Person ) extending Consumer , and thus it presents a firstName value. 此属性可以设置为扩展Consumer的具体类型( Person ),因此它显示firstName值。

In your JSF page, right before you print the firstName of what you think it's a Consumer instance, also print the value of "consumer.class.simpleName". 在您的JSF页面中,在您打印出您认为是Consumer实例的firstName之前,还会打印“consumer.class.simpleName”的值。 Most likely this will print "Person" as your object is an instance of "Person" (and it's polymorphically treated as a Consumer type, because in this case a Person instance is also of type Consumer). 很可能这将打印“Person”,因为您的对象是“Person”的实例(并且它被多态地视为Consumer类型,因为在这种情况下,Person实例也是Consumer类型)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM