欢迎光临散文网 会员登陆 & 注册

component组合的用法

2022-06-15 20:38 作者:虚云幻仙  | 我要投稿

/**
* 测试组合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一个information类 通过s1类对象名.in使用information类的内容
   Account ac = new Account();
   //可以组合多个类
   public void print(String name,int age,int id){
       this.in.name = name;
       //this代表调用方法的对象 对象的information对象的name属性
       this.in.age = age;
       this.ac.id = id;
       //对象的account对象的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类对象名.aaa() 和 .ac.aaa() 不冲突
}


component组合的用法的评论 (共 条)

分享到微博请遵守国家法律