[nhn next] java 강의- week3

Post on 10-May-2015

2.128 Views

Category:

Software

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

Java 강의 3주차

TRANSCRIPT

PL����������� ������������������  in����������� ������������������  Java����������� ������������������  ����������� ������������������  Week03����������� ������������������  

I n h e r i t a n c e

조영호����������� ������������������  snatcher@nhn.com����������� ������������������  

상속

Conceptual Perspective����������� ������������������  

Implementation Perspective����������� ������������������  

객체지향����������� ������������������  Object-Orientation����������� ������������������  

개념����������� ������������������  관점����������� ������������������  

구현����������� ������������������  관점����������� ������������������  

Conceptual Perspective����������� ������������������  

Implementation Perspective����������� ������������������  

객체지향����������� ������������������  Object-Orientation����������� ������������������  

개념����������� ������������������  관점����������� ������������������  

구현����������� ������������������  관점����������� ������������������  

Type����������� ������������������  

Class����������� ������������������  

Conceptual Perspective����������� ������������������  

Implementation Perspective����������� ������������������  

객체지향����������� ������������������  Object-Orientation����������� ������������������  

개념����������� ������������������  관점����������� ������������������  

구현����������� ������������������  관점����������� ������������������  

Generalization����������� ������������������  

Inheritance����������� ������������������  

To p i c s ����������� ������������������  

Inheritance

Generalization

Composition

Method Overriding

super

super()

사람����������� ������������������  

요츠바����������� ������������������  5살����������� ������������������  

class Person { String name; int age; Person() { this("사람", 1); } Person(String name, int age) { this.name = name; this.age= age; } String introduce() { return "이름 : " + name + ", 나이 " + age + "세"; }}

Person����������� ������������������  

Person yotsuba = new Person("요츠바", 5);System.out.println(yotsuba.introduce());

Person yotsuba = new Person("요츠바", 5);

name = 요츠바 age = 5����������� ������������������  

Person

new Person()����������� ������������������  

킬러����������� ������������������  

class Killer { String name; int age; String warning; String weapon; Killer(String name, int age, String warning, String weapon) { this.name = name; this.age = age; this.warning = warning; this.weapon = weapon ; } String introduce() { return "이름 : " + name + ", 나이 " + age + "세"; } String getWeapon() { return weapon; }}

Killer����������� ������������������  

Killer killerYotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총");System.out.println(killerYotsuba.introduce());System.out.println(killerYotsuba.getWeapon());

Killer killerYotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총");

name = 요츠바 age = 5 warning = You.. weapon = 총 ����������� ������������������  

Killer

new Killer()����������� ������������������  

Person & Killer����������� ������������������  

Person����������� ������������������  name age����������� ������������������  

introduce()����������� ������������������  

Killer����������� ������������������  name age warning weapon����������� ������������������  

introduce() getWeapon()����������� ������������������  

Code Duplication����������� ������������������  

Person����������� ������������������  name age����������� ������������������  

introduce()����������� ������������������  

Killer����������� ������������������  name age warning weapon����������� ������������������  

introduce() getWeapon()����������� ������������������  

Inheritance����������� ������������������  

Person����������� ������������������  name age����������� ������������������  

introduce()����������� ������������������  

Killer����������� ������������������   warning weapon����������� ������������������  

getWeapon()����������� ������������������  

class Killer extends Person { String warning; String weapon; Killer(String name, int age, String warning, String weapon) { this.name = name; this.age = age; this.warning = warning; this.weapon = weapon; } String getWeapon() { return weapon; } }

Killer extends Person����������� ������������������  

Person yotsuba = new Person("요츠바", 5);

name = 요츠바 age = 5����������� ������������������  

Person

Killer killerYotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총");

name = 요츠바 age = 5 warning = You.. weapon = 총 ����������� ������������������  

Killer

Member Variable Inheritance����������� ������������������  

Person yotsuba = new Person("요츠바", 5);System.out.println(yotsuba.introduce());Killer killerYotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총");System.out.println(killerYotsuba.introduce());System.out.println(killerYotsuba.getWeapon());

Method Inheritacne����������� ������������������  

음악가����������� ������������������  

Person Code Reuse����������� ������������������  

Inheritance����������� ������������������  

Person����������� ������������������  name age����������� ������������������  

introduce()����������� ������������������  

Musican����������� ������������������   instrument

play()����������� ������������������  

public class Musician extends Person { String instrument; Musician(String name, int age, String instrument) { this.name = name; this.age = age; this.instrument = instrument; } String play() { return instrument + " 연주"; }}

Musician extends Person����������� ������������������  

Musician musicanYotsuba = new Musician("요츠바", 5, "피리");System.out.println(musicanYotsuba.introduce());System.out.println(musicanYotsuba.play());

Code Reuse����������� ������������������  

Inheritance����������� ������������������  

Remove Code Duplication����������� ������������������  

Root Class����������� ������������������  

Object����������� ������������������  

class Person {}

Object����������� ������������������  

class Person extends Object {}

To p i c s ����������� ������������������  

Inheritance

Generalization

Composition

Method Overriding

super

super()

사람과����������� ������������������  킬러����������� ������������������  

사람과����������� ������������������  킬러����������� ������������������  더����������� ������������������  일반적인����������� ������������������  개념����������� ������������������  

사람과����������� ������������������  킬러����������� ������������������  더����������� ������������������  특수한����������� ������������������  개념����������� ������������������  

왜?����������� ������������������  

왜?����������� ������������������  의미적으로����������� ������������������  킬러를����������� ������������������  포괄하면서

더����������� ������������������  적은����������� ������������������  설명����������� ������������������  필요����������� ������������������  

사람의����������� ������������������  개념을����������� ������������������  포함하는 더����������� ������������������  많은����������� ������������������  설명����������� ������������������  필요����������� ������������������  

왜?����������� ������������������  

이����������� ������������������  세상에는����������� ������������������  사람이����������� ������������������  더����������� ������������������  많을까요?����������� ������������������  킬러가����������� ������������������  더����������� ������������������  많을까요?����������� ������������������  

사람����������� ������������������  

사람����������� ������������������  킬러����������� ������������������  

사람����������� ������������������  킬러����������� ������������������  더����������� ������������������  일반적인����������� ������������������  개념����������� ������������������  

더����������� ������������������  특수한����������� ������������������  개념����������� ������������������  

일반화와����������� ������������������  특수화����������� ������������������  일반화����������� ������������������   특수화����������� ������������������  

일반화와����������� ������������������  특수화����������� ������������������  일반화����������� ������������������   특수화����������� ������������������  

설명은����������� ������������������  더����������� ������������������  추상적이고 집합의����������� ������������������  크기는����������� ������������������  더����������� ������������������  크고����������� ������������������  

일반화와����������� ������������������  특수화����������� ������������������  일반화����������� ������������������   특수화����������� ������������������  

설명은����������� ������������������  더����������� ������������������  구체적이고 집합의����������� ������������������  크기는����������� ������������������  더����������� ������������������  작고����������� ������������������  

일반화와����������� ������������������  특수화����������� ������������������  일반화����������� ������������������   특수화����������� ������������������  

특수화����������� ������������������  집합은����������� ������������������  일반화����������� ������������������  집합의����������� ������������������  부분집합����������� ������������������  

is-a Relationship����������� ������������������  

Killer is-a Person����������� ������������������  

in OO����������� ������������������  

Concept == Type����������� ������������������  

사람����������� ������������������  킬러����������� ������������������  Type

Type

in OO����������� ������������������  

Type Class����������� ������������������  implementation

사람����������� ������������������  킬러����������� ������������������  Class

Class

class  Person  {      String  name;      int  age;            String  introduce()  {      }  }  

Person & Killer����������� ������������������  

class  Killer  extends  Person  {      String  warning;      String  weapon;      String  getWeapon()  {      } }  

더����������� ������������������  추상적인����������� ������������������  설명����������� ������������������  

더����������� ������������������  구체적인����������� ������������������  설명����������� ������������������  

Killer  killerYotsuba  =  new  Killer("요츠바",  5,                                      "You  can  tell  me  in  hell.",  "총");  

name = 요츠바 age = 5 warning = You.. weapon = 총 ����������� ������������������  

Killer

Person

Killer  killerYotsuba  =  new  Killer("요츠바",  5,                                      "You  can  tell  me  in  hell.",  "총");  

name = 요츠바 age = 5 warning = You.. weapon = 총 ����������� ������������������  

Killer

Person

Person  yotsuba  =  new  Killer("요츠바",  5,                                      "You  can  tell  me  in  hell.",  "총");  

Person����������� ������������������  Killer����������� ������������������  Class

Class

new Killer()����������� ������������������  

new Killer()����������� ������������������  

new Killer()����������� ������������������  

new Person()����������� ������������������  

new Person()����������� ������������������  

new Person()����������� ������������������  

new Person()����������� ������������������  

new Person()����������� ������������������  

new Person()����������� ������������������  

new Person()����������� ������������������  

new Person()����������� ������������������  

new Person()����������� ������������������  

new Person()����������� ������������������  

class  Person  {      String  name;      int  age;            String  introduce()  {      }  }  

Person & Killer����������� ������������������  

class  Killer  extends  Person  {      String  warning;      String  weapon;      String  getWeapon()  {      } }  

일반화����������� ������������������  

특수화����������� ������������������  

in OO����������� ������������������  

Type Type����������� ������������������  

Generalization Specialization

in OO����������� ������������������  

Class Class����������� ������������������  Inheritance

in OO����������� ������������������  

Generalization Inheritance����������� ������������������  

implementation

To p i c s ����������� ������������������  

Inheritance

Generalization

Composition

Method Overriding

super

super()

Method Overloading?����������� ������������������  

Method Overloading����������� ������������������  public class Elevator { Elevator() { ... } Elevator(int floor) { ... } void moveUp() { ... } void moveUp(int floor) { ... } void moveDown() { ... } void moveDown(int floor) { ... }}

Method Overriding?����������� ������������������  

this

class Person { String name; int age; ... String introduce() { return "이름 : " + name + ", 나이 " + age + "세"; }}

Person yotsuba = new Person("요츠바", 5);

name = 요츠바 age = 5����������� ������������������  

this class  Person  String  introduce()  {    return  "이름 :  "  +  name  +              ",  나이 "  +  age  +  "세";      }

class

class  Object

superclass

 String  toString()  {  ...  }  

Method Lookup

this에서����������� ������������������  시작����������� ������������������  

class Person { String introduce() { return "이름 : " + name + ", 나이 " + age + "세"; }}

name = 요츠바 age = 5����������� ������������������  

this class  Person  String  introduce()  {    return  "이름 :  "  +  name  +              ",  나이 "  +  age  +  "세";      }

class

class  Object

superclass

 String  toString()  {  ...  }  

yotsuba.introduce()

class  Person  {    String  introduce()  {          return  "이름 :  "  +  name  +  ",  나이 "  +  age  +  "세";      }  }  

name = 요츠바 age = 5����������� ������������������  

this class  Person  String  introduce()  {    return  "이름 :  "  +  name  +              ",  나이 "  +  age  +  "세";      }

class

class  Object

superclass

 String  toString()  {  ...  }  

yotsuba.toString()

class Killer extends Person { }

class Person { String introduce() { return "이름 : " + name + ", 나이 " + age + "세"; }}

Person yotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총");

name = 요츠바 age = 5 warning = You.. weapon = 총

����������� ������������������  

this

class  Person  String  introduce()  {    return  "이름 :  "  +  name  +              ",  나이 "  +  age  +  "세";      }

class

class  Object

superclass

 String  toString()  {  ...  }  

class  Killer

superclass

name = 요츠바 age = 5 warning = You.. weapon = 총

����������� ������������������  

this

class  Person  String  introduce()  {    return  "이름 :  "  +  name  +              ",  나이 "  +  age  +  "세";      }

class

class  Object

superclass

 String  toString()  {  ...  }  

class  Killer

superclass

yotsuba.introduce()

class  Killer  extends  Person  {      }  

class  Person  {      String  introduce()  {          return  "이름 :  "  +  name  +  ",  나이 "  +  age  +  "세";      }  }  

name = 요츠바 age = 5 warning = You.. weapon = 총

����������� ������������������  

this

class  Person  String  introduce()  {    return  "이름 :  "  +  name  +              ",  나이 "  +  age  +  "세";      }

class

class  Object

superclass

 String  toString()  {  ...  }  

class  Killer

superclass

yotsuba.introduce()

 String  introduce()  {    return  "무기 :  "  +  weapon  +  ","  +            "이름 :  "  +  name  +            ",  나이 "  +  age  +  "세";      }

class Killer extends Person { String introduce() { return "무기 : " + weapon + "," + "이름 : " + name + ", 나이 " + age + "세"; } }

class Person { String introduce() { return "이름 : " + name + ", 나이 " + age + "세"; }}

class  Object

Killer의����������� ������������������  introduce()가����������� ������������������  

Person의����������� ������������������  introduce()를����������� ������������������  

감춤����������� ������������������  

Method Overriding?����������� ������������������  

To p i c s ����������� ������������������  

Inheritance

Generalization

Composition

Method Overriding

super

super()

Method Lookup

this에서����������� ������������������  시작����������� ������������������  

this

메시지를����������� ������������������  수신한����������� ������������������  현재����������� ������������������  객체����������� ������������������  

현재����������� ������������������  클래스����������� ������������������  

public class Person { String message() { return "Message : [" + introduce() + "]"; }

String introduce() { return "이름: " + name + ", 나이: " + age + "세"; }}

public class Killer extends Person { String introduce() { return "무기 : " + weapon + "," + "이름 : " + name + ", 나이 " + age + "세"; }}

Quiz

Person killerYotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총");killerYotsuba.message();

name = 요츠바 age = 5 warning = You.. weapon = 총

����������� ������������������  

this

class  Person String message() { return "Message : [" + introduce() + "]"; }

String  introduce()  {    return  "이름 :  "  +  name  +              ",  나이 "  +  age  +  "세";      }  

class

class  Object

superclass

 String  toString()  {  ...  }  

class  Killer

superclass

 String  introduce()  {        return  "무기 :  "  +  weapon  +  ","  +            "이름 :  "  +  name  +            ",  나이 "  +  age  +  "세";      }

Person killerYotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총");

killerYotsuba.message(); class  Object

this부터����������� ������������������  Look����������� ������������������  Up����������� ������������������  

name = 요츠바 age = 5 warning = You.. weapon = 총

����������� ������������������  

this

class  Person String message() { return "Message : [" + introduce() + "]"; }

String  introduce()  {    return  "이름 :  "  +  name  +              ",  나이 "  +  age  +  "세";      }  

class

class  Object

superclass

 String  toString()  {  ...  }  

class  Killer

superclass

 String  introduce()  {        return  "무기 :  "  +  weapon  +  ","  +            "이름 :  "  +  name  +            ",  나이 "  +  age  +  "세";      }

Person killerYotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총");killerYotsuba.message();

class  Object

introduce()

name = 요츠바 age = 5 warning = You.. weapon = 총

����������� ������������������  

this

class  Person String message() { return "Message : [" + introduce() + "]"; }

String  introduce()  {    return  "이름 :  "  +  name  +              ",  나이 "  +  age  +  "세";      }  

class

class  Object

superclass

 String  toString()  {  ...  }  

class  Killer

superclass

 String  introduce()  {        return  "무기 :  "  +  weapon  +  ","  +            "이름 :  "  +  name  +            ",  나이 "  +  age  +  "세";      }

Person killerYotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총");killerYotsuba.message();

class  Object

introduce()

this부터����������� ������������������  Look����������� ������������������  Up����������� ������������������  

name = 요츠바 age = 5 warning = You.. weapon = 총

����������� ������������������  

this

class  Person String message() { return "Message : [" + introduce() + "]"; }

String  introduce()  {    return  "이름 :  "  +  name  +              ",  나이 "  +  age  +  "세";      }  

class

class  Object

superclass

 String  toString()  {  ...  }  

class  Killer

superclass

 String  introduce()  {        return  "무기 :  "  +  weapon  +  ","  +            "이름 :  "  +  name  +            ",  나이 "  +  age  +  "세";      }

Person killerYotsuba = new Killer("요츠바", 5, "You can tell me in hell.", "총");killerYotsuba.message();

class  Object

introduce()

this부터����������� ������������������  Look����������� ������������������  Up����������� ������������������  

this Dynamic����������� ������������������  

super

Method Lookup

부모����������� ������������������  클래스에서����������� ������������������  시작����������� ������������������  

public class Person { String introduce() { return "이름: " + name + ", 나이: " + age + "세"; } }

public class Killer extends Person { String introduce() { return "무기 : " + weapon + "," + "이름 : " + name + ", 나이 " + age + "세"; }}

Code Duplication

public class Person { String introduce() { return "이름: " + name + ", 나이: " + age + "세"; } }

public class Killer extends Person { String introduce() { return "무기 : " + weapon + "," + introduce(); }} Recursive Call

Code Reuse

public class Person { String introduce() { return "이름: " + name + ", 나이: " + age + "세"; } }

public class Killer extends Person { String introduce() { return "무기 : " + weapon + "," + super.introduce(); }}

super

super

super가����������� ������������������  있는����������� ������������������  클래스의����������� ������������������  부모����������� ������������������  클래스����������� ������������������  

현재����������� ������������������  객체����������� ������������������  

super Static����������� ������������������  

To p i c s ����������� ������������������  

Inheritance

Generalization

Composition

Method Overriding

super

super()

this()

this()����������� ������������������  class Person { Person() { this("사람", 1); } Person(String name, int age) { this.name = name; this.age= age; }}

class Killer extends Person { Killer(String name, int age, String warning, String weapon) { this.name = name; this.age = age; this.warning = warning; this.weapon = weapon; }}

class Person { Person() { this("사람", 1); } Person(String name, int age) { this.name = name; this.age= age; }}

Code Duplication

super()

class Killer extends Person { Killer(String name, int age, String warning, String weapon) { super(name, age); this.warning = warning; this.weapon = weapon; }}

class Person { Person() { this("사람", 1); } Person(String name, int age) { this.name = name; this.age= age; }}

Code Duplication

To p i c s ����������� ������������������  

Inheritance

Generalization

Composition

Method Overriding

super

super()

아이언맨����������� ������������������  

class  IronMan  extends  Person  {   }  

?����������� ������������������  

사람����������� ������������������  킬러����������� ������������������  

아이언맨����������� ������������������  

?����������� ������������������  

IronMan����������� ������������������   Person����������� ������������������  

Composition����������� ������������������  

Not Generalization����������� ������������������  

class  IronMan  {      Person  person;        IronMan(Person  person)  {          this.person  =  person;      }  }  

합성Composition����������� ������������������  

IronMan  ironMan  =        new  IronMan(new  Person("토니 스타크",  40));    IronMan  ironYotsuba  =        new  IronMan(new  Killer("요츠바",  5,                "You  can  tell  me  in  hell.",  "총"));  

사람����������� ������������������   킬러����������� ������������������  아이언맨����������� ������������������  

포함����������� ������������������  

아이언맨이����������� ������������������  말을����������� ������������������  하는����������� ������������������  것이����������� ������������������  아니라����������� ������������������  

안에����������� ������������������  들어����������� ������������������  있는����������� ������������������  사람이����������� ������������������  말을����������� ������������������  하는����������� ������������������  것이죠����������� ������������������  

public  class  IronMan  {      Person  person;        IronMan(Person  person)  {          this.person  =  person;      }      String  introduce()  {          return  person.introduce();      }  }  

위임Delegation����������� ������������������  

has-a Relationship����������� ������������������  

IronMan has-a Person����������� ������������������  

Code Reuse����������� ������������������  

Inheritance����������� ������������������  

Composition����������� ������������������  

White-Box Reuse����������� ������������������  

Black-Box Reuse����������� ������������������  

Favor composition over inheritance

top related