close
Bean和Bean之間也可以利用繼承(Inheritance)的方式。
HelloWorld.java
package com.example ; public class HelloWorld { private String message ; private String message1 ; public void getMessage(){ System.out.println("Your Message : " + message) ; } public void setMessage(String message) { this.message = message; } public void getMessage1(){ System.out.println("Your Message1 : " + message1) ; } public void setMessage1(String message1) { this.message1 = message1; } }
HelloPaul.java
package com.example ; public class HelloPaul { private String message ; private String message1 ; private String message2 ; public void getMessage(){ System.out.println("Your Message : " + message) ; } public void setMessage(String message) { this.message = message; } public void getMessage1(){ System.out.println("Your Message1 : " + message1) ; } public void setMessage1(String message1) { this.message1 = message1; } public void getMessage2(){ System.out.println("Your Message2 : " + message2) ; } public void setMessage2(String message2) { this.message2 = message2; } }
Main.java
package com.example ; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); obj.getMessage(); obj.getMessage1(); HelloPaul objPaul = (HelloPaul) context.getBean("helloPaul"); objPaul.getMessage(); objPaul.getMessage1(); objPaul.getMessage2(); } }
Beans.xml
結果顯示如下所示:
Your Message : Hello World Your Message1 : Hello World1 Your Message : Hello Paul Your Message1 : Hello World1 Your Message2 : Hello Paul2
另外亦可用template的方式,如下所示(將abstract屬性設定為true):
全站熱搜
留言列表