14_Xml继承

【工程截图】

 

【Person.java】

package com.HigginCui;

public class Person {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

【Studnet.java】

package com.HigginCui;

public class Student extends Person{
    public void studentSay(){
        System.out.println(this.getName());
    }
}

【applicationContext.xml】

<?xml version= "1.0" encoding ="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <bean id="person" class="com.HigginCui.Person">
        <property name="name">
            <value>HigginCui</value>
        </property>
    </bean>
    <!-- parent属性说明了student这个bean继承于person这个bean中的属性的值
     若不加parent="person"这句,testStudent中的输出值为null
--> <bean id="student" class="com.HigginCui.Student" parent="person"> </bean> </beans>

【testStudent.java】

package com.HigginCui.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.HigginCui.Student;

public class testStudent {
    @Test
    public void test(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student=(Student) context.getBean("student");
        student.studentSay();
    }
}

【运行结果】

HigginCui

 

转载于:https://www.cnblogs.com/HigginCui/p/5579306.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值