1 / 10

메소드 / 생성자와 다형성

메소드 / 생성자와 다형성. 메소드와 다형성 (polymorphism). : 다형성이란 객체지향언어에서 사용되는 매우 중요한 개념이며 , 이 개념을 구사하기위해 여러 가지 방법들을 제공하고 있습니다. Exam3_10 예문 ) 1 class Exam{ 2 int bit; 3 void data(int cyber){ 4 int bds = cyber; 5 System.out.println(bds); 6 }

duard
Télécharger la présentation

메소드 / 생성자와 다형성

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 메소드/생성자와 다형성

  2. 메소드와 다형성(polymorphism) : 다형성이란 객체지향언어에서 사용되는 매우 중요한 개념이며, 이 개념을 구사하기위해 여러 가지 방법들을 제공하고 있습니다.

  3. Exam3_10 예문) 1 class Exam{ 2 int bit; 3 void data(int cyber){ 4 int bds = cyber; 5 System.out.println(bds); 6 } 7 void data(int cyber, int termi){ 8 int bds_1 = cyber; 9 System.out.println(bds_1 + termi); 10 } 11 void data(int cyber, int termi, int java){ 12 System.out.println(cyber + termi + java); 13 } 14 } 15 class Exam3_10{ 16 public static void main(String str[]){ 17 Exam bit = new Exam(); 18 bit.data(2); 19 bit.data(2, 4); 20 bit.data(2, 3, 5); 21 } 22 }

  4. Exam3_11 예문) 1 class Exam{ 2int bit; 3 void data(int cyber){ 4 int bds = cyber; 5 System.out.println(bds); 6 } 7 int data(int cyber, int termi){ 8 int bds_1 = cyber; 9 return bds_1 + termi; 10 } 11 void data(int cyber, int termi, int java){ 12 System.out.println(cyber + termi + java); 13 } 14 } 15 class Exam3_11{ 16public static void main(String str[]){ 17 Exam bit = new Exam(); 18 bit.data(2); 19 System.out.println(bit.data(2, 4)); 20 bit.data(2, 3, 5); 21 } 22 }

  5. Exam3_12 예문) 1 class Exam{ 2int bit; 3 void data(int cyber){ 4 int bds = cyber; 5 System.out.println(bds); 6 } 7 int data(int cyber, int termi){ 8 int bds_1 = cyber; 9 return bds_1 + termi; 10 } 11void data(float cyb){ //3행의 메서드와 파라미터가 같음. 12 System.out.println(cyb); 13 } 14 } 15 class Exam3_12{ 16public static void main(String str[]){ 17 Exam bit = new Exam(); 18 bit.data(2); 19 System.out.println(bit.data(2, 4)); 20 bit.data(2.3); 21 } 22 } : 위 소스에서 3행의 메서드와 11행의 메서드를 보면 전달받는 값의 자료형이 다릅니다. 파라미터의 자료형이 다르거나, 변수이름이 다르거나, 개수가 다르면 오버로딩인데 위 예제는 이런 모든 조건을 만족했습니다. 파라미터의 변수 이름을 다르게 하고 자료형도 다르게 했지만 에러가 발생됩니다. 하지만 위 예제 20행을 bit.data(2.3f)와 같이 수정하면 정상적으로 출력됩니다. 그 이유는 float와 double의 기본 자료형은 double이고, 만일 float 변수에 값을 대입하고 싶으면 값에 f 기호를 붙여야 하는 것인데 f 기호가 없으므로 기본 자료형 법칙에 위배되어 에러가 되는 것입니다.

  6. <메소드 오버로딩 법칙 정리> 생성자와 다형성

  7. Exam3_13 예문) 1 class Exam{ 2 int bit; 3 Exam(int a){ 4 bit = a; 5 System.out.println("멤버변수 초기화에 사용 : " + bit); 6 } 7 } 8 class Exam3_13{ 9 public static void main(String str[]){ 10 Exam data = new Exam(3); 11 } 12 } Exam3_14 예문) 1 class Exam{ 2 int bit; 3 Exam(int a){ 4 bit = a; 5 System.out.println("멤버변수 초기화에 사용 : " + bit); 6 } 7 } 8 class Exam3_14{ 9 public static void main(String str[]){ 10 new Exam(3); 11 } 12 }

  8. Exam3_15 예문) 1 class Exam{ 2 int bit; 3 Exam(int a){ 4 bit = a; 5 System.out.println("멤버변수 초기화에 사용 : " + bit); 6 } 7 } 8 class Exam3_15{ 9 public static void main(String str[]){ 10 Exam data = new Exam(3); 11 System.out.println("Exam 클래스의 인스턴스 변수 호출: " + data.bit); 12 } 13 } Exam3_16 예문) 1 class Exam{ 2 int bit; 3 Exam(int a){ 4 bit = a; 5 System.out.println("멤버변수 초기화에 사용 : " + bit); 6 } 7 } 8 class Exam3_16{ 9 public static void main(String str[]){ 10 System.out.println("객체이용: " + new Exam(3).bit); 11 } 12 }

  9. Exam3_17 예문) 1 class Exam{ 2 int bit; 3 Exam(int a){ 4 bit = a; 5 return bit; 6 } 7 } 8 class Exam3_17{ 9 public static void main(String str[]){ 10 System.out.println("객체이용: " + new Exam(3)); 11 } 12 } Exam3_18 예문) 1 class Exam{ 2 int bit, bit_one, bit_two; 3 Exam(int a){ 4 bit = a; 5 System.out.println(bit); 6 } 7 Exam(int a, int b){ 8 bit = a; 9 bit_one = b; 10 System.out.println(bit); 11 System.out.println(bit_one); 12 } 13 Exam(int a, int b, int c){ 14 bit = a; 15 bit_one = b; 16 bit_two = c; 17 System.out.println(bit); 18 System.out.println(bit_one); 19 System.out.println(bit_two); 20 } 21 } 22 class Exam3_18{ 23 public static void main(String[] str){ 24 new Exam(1); 25 new Exam(2,3); 26 new Exam(4,5,6); 27 } 28 }

  10. Exam3_19 예문) 1 class Exam{ 2 int bit = 3; 3 } 4 class Exam3_19{ 5 public static void main(String str[]){ 6 Exam data = new Exam(); 7 int system = data.bit; 8 System.out.println(system); 9 } 10 } Exam3_20 예문) 1 class Exam{ 2 int bit = 3; 3 Exam(){ } 4 } 5 class Exam3_20{ 6 public static void main(String str[]){ 7 Exam data = new Exam(); 8 int system = data.bit; 9 System.out.println(system); 10 } 11 }

More Related