[英]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. 这有一些数字字段,如firstName
, lastName
等。在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 那么,它的作用是什么
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.