装配bean(继承装配)

假如一个类继承了其他的类,那么它的bean该如何配置?
 
假如我们有一个父类:
package com.extendClass.bean;
 
public class Father {
protected String name;
protected int age;
private String company;
public void wealth(){
System.out.println("很多钱");
}
public void house(){
System.out.println("很多房产");
}
public void car(){
System.out.println("很多车");
}
public String getName(){
return this.name;
}
public void setName(String new_name){
this.name = new_name;
}
public int getAge(){
return this.age;
}
public void setAge(int new_age){
this.age = new_age;
}
 
public String getCompany() {
return company;
}
 
public void setCompany(String company) {
this.company = company;
}
}
 
 
有一个子类Son.java 继承了这个父类
package com.extendClass.bean;
 
public class Son extends Father {
private String sonName;
private int sonAge;
public void extend(){
this.car();
this.house();
this.wealth();
}
 
public String getSonName() {
return sonName;
}
 
public void setSonName(String sonName) {
this.sonName = sonName;
}
 
public int getSonAge() {
return sonAge;
}
 
public void setSonAge(int sonAge) {
this.sonAge = sonAge;
}
}
 
然后我们配置bean
 
<!-- 有继承的类的配置 parent 表示该类是father bean的一个子类-->
<bean id="son" class="com.extendClass.bean.Son" parent="father">
<property name="sonName">
<value>子类</value>
</property>
<property name="sonAge">
<value>1</value>
</property>
</bean>
<!--父类 -->
<bean id="father" class="com.extendClass.bean.Father">
<property name="name">
<value>父类</value>
</property>
<property name="age">
<value>2</value>
</property>
<property name="company">
<value>alibaba</value>
</property>
</bean>
 
posted @ 2018-01-13 17:34  晴天小猫  阅读(156)  评论(0编辑  收藏  举报