component組合的用法
/**
* 測(cè)試組合component
*/
public class TestComponent {
}
class Information {
? ?String name;
? ?int age;
}
class Account{
? ?int id;
? ?public void aaa(){
? ? ? ?System.out.println("a");
? ?}
}
class S1{
? ?Information in =new Information();
? ?//組合component 在s1類里new一個(gè)information類 通過(guò)s1類對(duì)象名.in使用information類的內(nèi)容
? ?Account ac = new Account();
? ?//可以組合多個(gè)類
? ?public void print(String name,int age,int id){
? ? ? ?this.in.name = name;
? ? ? ?//this代表調(diào)用方法的對(duì)象 對(duì)象的information對(duì)象的name屬性
? ? ? ?this.in.age = age;
? ? ? ?this.ac.id = id;
? ? ? ?//對(duì)象的account對(duì)象的id屬性
? ? ? ?System.out.println(this.in.name);
? ? ? ?System.out.println(this.in.age);
? ? ? ?System.out.println(this.ac.id);
? ?}
? ?//in和ac作為s1的組成部分 不同于繼承extends的延伸、拓展
? ?public void aaa(){
? ? ? ?System.out.println("aa");
? ?}
? ?//s1類對(duì)象名.aaa() 和 .ac.aaa() 不沖突
}