IT News/Flutter & Dart

Dart Class 기본 예제

skyLove1982 2021. 9. 6. 15:17
반응형

class Company{
  String name='';
  Person ceo = new Person("ceo");
  List<Person> workerList = [];
  
  Company(String this.name);
  
  void setCeo(Person ceo){
    this.ceo = ceo;
  }
  
  void addWorker(Person worker){
    this.workerList.add(worker);
  }
  
  void removeWorker(Person worker){
    this.workerList.remove(worker);
  }
  
  void info(){
    print('company : $name');
    this.ceo.info();
           
    this.workerList.forEach( (e){
      print(e.name);
    });
    
    for(int i=0; i<this.workerList.length; i++){
      print(this.workerList[i].name);
    }
    
    for(Person e in workerList){
      print(e.name);
    }
    
    //print('ceo : ${this.ceo.name}');
  }
}

class Person{
  String name='';
  Person(String this.name);
  
  void info(){
    print(name);
  }
}


void main(){
  
  Person pa = new Person("ceo영희");
  Person pb = new Person("ceo길동");
  
  Person pc = new Person("태현");
  Person pd = new Person("소리");
  
  pa.info();
  pb.info();
  
  
  String aaName = "aa";
  Company aa = new Company(aaName);  
  Company bb = new Company("bb");
  
  aa.addWorker(pc);
  aa.addWorker(pd);
  aa.removeWorker(pc);
  
  aa.setCeo(pa);
  bb.setCeo(pb);
  
  aa.info();
  bb.info();
}

반응형