쉽게 풀어쓴 C 프로그래밍

Similar documents
gnu-lee-oop-kor-lec06-3-chap7

PowerPoint Presentation

쉽게 풀어쓴 C 프로그래밍

<4D F736F F F696E74202D20C1A63038C0E520C5ACB7A1BDBABFCD20B0B4C3BC4928B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

PowerPoint Presentation

JAVA PROGRAMMING 실습 08.다형성

PowerPoint Presentation

PowerPoint Presentation

Microsoft PowerPoint - 2강

PowerPoint Presentation

예제 2) Test.java class A intvar= 10; void method() class B extends A intvar= 20; 1"); void method() 2"); void method1() public class Test 3"); args) A

PowerPoint Presentation

5장.key

PowerPoint Presentation

Microsoft PowerPoint - Lect04.pptx

q 이장에서다룰내용 1 객체지향프로그래밍의이해 2 객체지향언어 : 자바 2

쉽게 풀어쓴 C 프로그래밍

Design Issues

쉽게 풀어쓴 C 프로그래밍

No Slide Title

PowerPoint Presentation

비긴쿡-자바 00앞부속

PowerPoint Presentation

PowerPoint Presentation

PowerPoint 프레젠테이션

PowerPoint Presentation

Microsoft PowerPoint - java1-lecture5.ppt [호환 모드]

JAVA PROGRAMMING 실습 02. 표준 입출력

PowerPoint 프레젠테이션

자바 프로그래밍

PowerPoint 프레젠테이션

윤성우의 열혈 TCP/IP 소켓 프로그래밍

01-OOPConcepts(2).PDF

쉽게 풀어쓴 C 프로그래밍

PowerPoint 프레젠테이션

JAVA PROGRAMMING 실습 07. 상속

JAVA PROGRAMMING 실습 05. 객체의 활용

Microsoft PowerPoint - java1-lecture5.ppt [호환 모드]

쉽게 풀어쓴 C 프로그래밍

Microsoft Word - EEL2 Lab4.docx

Java ~ Java program: main() class class» public static void main(string args[])» First.java (main class ) /* The first simple program */ public class

쉽게

PowerPoint Presentation

(8) getpi() 함수는정적함수이므로 main() 에서호출할수있다. (9) class Circle private double radius; static final double PI= ; // PI 이름으로 로초기화된정적상수 public

09-interface.key

JAVA 프로그래밍실습 실습 1) 실습목표 - 메소드개념이해하기 - 매개변수이해하기 - 새메소드만들기 - Math 클래스의기존메소드이용하기 ( ) 문제 - 직사각형모양의땅이있다. 이땅의둘레, 면적과대각

Microsoft PowerPoint - chap11

Microsoft PowerPoint - lec7_package [호환 모드]

Microsoft PowerPoint - 04-UDP Programming.ppt

Microsoft PowerPoint - java2 [호환 모드]

Microsoft PowerPoint 자바-기본문법(Ch2).pptx

Microsoft PowerPoint - java1-lab5-ImageProcessorTestOOP.pptx

(Microsoft PowerPoint - java1-lecture11.ppt [\310\243\310\257 \270\360\265\345])

교육자료

PowerPoint Presentation

Microsoft PowerPoint - Java7.pptx

제11장 프로세스와 쓰레드

슬라이드 1

PowerPoint Presentation

<4D F736F F F696E74202D20C1A63233C0E520B1D7B7A1C7C820C7C1B7CEB1D7B7A1B9D628B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

PowerPoint 프레젠테이션

PowerPoint Presentation

Microsoft PowerPoint - lec12 [호환 모드]

(Microsoft PowerPoint - java2-lecture3.ppt [\310\243\310\257 \270\360\265\345])

PowerPoint Presentation

슬라이드 1

PowerPoint Template

02 C h a p t e r Java

PowerPoint 프레젠테이션

Spring Data JPA Many To Many 양방향 관계 예제

JAVA PROGRAMMING 실습 09. 예외처리

Cluster management software

Network Programming

Microsoft PowerPoint - additional08.ppt [호환 모드]

PowerPoint 프레젠테이션

05-class.key

10.0pt1height.7depth.3width±â10.0pt1height.7depth.3widthÃÊ10.0pt1height.7depth.3widthÅë10.0pt1height.7depth.3width°è10.0pt1height.7depth.3widthÇÁ10.0pt1height.7depth.3width·Î10.0pt1height.7depth.3width±×10.0pt1height.7depth.3width·¡10.0pt1height.7depth.3width¹Ö pt1height.7depth.3widthŬ10.0pt1height.7depth.3width·¡10.0pt1height.7depth.3width½º, 10.0pt1height.7depth.3width°´10.0pt1height.7depth.3widthü, 10.0pt1height.7depth.3widthº¯10.0pt1height.7depth.3width¼ö, 10.0pt1height.7depth.3width¸Þ10.0pt1height.7depth.3width¼Ò10.0pt1height.7depth.3widthµå

쉽게 풀어쓴 C 프로그래밍

PowerPoint Presentation

rmi_박준용_final.PDF

슬라이드 1

Spring Boot/JDBC JdbcTemplate/CRUD 예제

오버라이딩 (Overriding)

쉽게 풀어쓴 C 프로그래밍

Microsoft PowerPoint - Supplement-03-TCP Programming.ppt [호환 모드]

유니티 변수-함수.key

자바GUI실전프로그래밍2_장대원.PDF

12-file.key

설계란 무엇인가?

JVM 메모리구조

gnu-lee-oop-kor-lec10-1-chap10

17장 클래스와 메소드

슬라이드 1

ThisJava ..

쉽게 풀어쓴 C 프로그래밊

A Tour of Java V

07 자바의 다양한 클래스.key

JUNIT 실습및발표

1. 객체의생성과대입 int 형변수 : 선언과동시에초기화하는방법 (C++) int a = 3; int a(3); // 기본타입역시클래스와같이처리가능 객체의생성 ( 복습 ) class CPoint private : int x, y; public : CPoint(int a

Microsoft Word - PJ_scjp_9_0_1-lang.doc

Transcription:

Power Java 제 11 장상속

이번장에서학습할내용 상속이란? 상속의사용 메소드재정의 접근지정자 상속과생성자 Object 클래스 종단클래스 상속을코드를재사용하기위한중요한기법입니다.

상속이란? 상속의개념은현실세계에도존재한다.

상속의장점 상속의장점 상속을통하여기존클래스의필드와메소드를재사용 기존클래스의일부변경도가능 상속을이용하게되면복잡한 GUI 프로그램을순식간에작성 상속은이미작성된검증된소프트웨어를재사용 신뢰성있는소프트웨어를손쉽게개발, 유지보수 코드의중복을줄일수있다.

상속 class Car { int speed; class SportsCar extends Car { int turbo; 상속한다는의미

수퍼클래스는서브클래스를포함

상속의예 수퍼클래스 서브클래스 Animal( 동물 ) Lion( 사자 ), Dog( 개 ), Cat( 고양이 ) Bike( 자전거 ) MountainBike( 산악자전거 ) Vehicle( 탈것 ) Car( 자동차 ), Bus( 버스 ), Truck( 트럭 ), Boat( 보트 ), Motocycle( 오토바이 ), Bicycle( 자전거 ) Student( 학생 ) GraduateStudent( 대학원생 ), UnderGraduate( 학부생 ) Employee( 직원 ) Manager( 관리자 ) Shape( 도형 ) Rectangle( 사각형 ), Triangle( 삼각형 ), Circle( 원 )

상속의예 public class Car { // 3 개의필드선언 int speed; // 속도 int gear; // 기어 public String color; // 색상, 테스트를위하여공용필드로만들자. public void speedup(int increment) { // 속도증가메소드 speed += increment; public void speeddown(int decrement) { // 속도감소메소드 speed -= decrement; class SportsCar extends Car { // Car 를상속받는다. ; boolean turbo; public void setturbo(boolean newvalue) { // 터보모드설정메소드 turbo = newvalue;

Car 와 SportsCar

상속예제 public class Test { public static void main(string[] args) { SportsCar c = new SportsCar(); c.color = "Red"; c.speedup(100); c.speeddown(30); c.setturbo(true); 서브클래스는수퍼클래스의필드와메소드를마치자기것처럼사용할수있다.

상속의계층구조

상속은중복을줄인다. 상속

접근지정자

예제 class Employee { public String name; // 이름 : 공용멤버 String address; // 주소 : 패키지멤버 protected int salary; // 월급 : : 보호멤버 private int RRN; // 주민등록번호 : 전용멤버 public String tostring() { return name + ", " + address + "," + RRN + ", " + salary; Manager Employee

예제 class Manager extends Employee { private int bonus; public void printsalary() { System.out.println(name + "(" + address + "):" + (salary + bonus)); public void printrrn() { System.out.println(RRN); public class ManagerTest { public static void main(string[] args) { Manager m = new Manager(); 오류 m.printrrn();

예제

메소드재정의 메소드재정의 (method overriding): 서브클래스가필요에따라상속된메소드를다시정의하는것

메소드재정의의예 class Animal { public void sound() { ; class Dog extends Animal { public void sound() { System.out.println(" 멍멍!"); ; public class DogTest { public static void main(string[] args) { Dog d = new Dog(); d.sound(); ; Animal 1부터멍멍! 10까지의정수의합 = 55

메소드를재정의하려면 메소드의이름, 반환형, 매개변수의개수와데이터타입이일치하여야한다. public class Animal { public void sound() { ; 재정의가아님 public class Dog extends Animal { public int sound() { ;

어노테이션 public class Animal { public void sound() { ; 재정의가아님 class Dog extends Animal { @Override void saund() { // 오류발생! System.out.println(" 멍멍!"); 1 부터 10 까지의정수의합 = 55 The method saund() of type Dog must override or implement a supertype method

중복정의와재정의

super 키워드 class ParentClass { int data=100; public void print() { System.out.println(" 수퍼클래스의 print() 메소드 "); public class ChildClass extends ParentClass { int data=200; public void print() { super.print(); System.out.println(" 서브클래스의 print() 메소드 "); System.out.println(this.data); System.out.println(super.data); public static void main(string[] args) { ChildClass obj = new ChildClass(); 수퍼클래스의 print() 메소드 obj.print(); 서브 1부터클래스의 10까지의 print() 정수의메소드합 = 55 200 100

상속과생성자 서브클래스의객체가생성될때, 서브클래스의생성자만호출될까? 아니면수퍼클래스의생성자도호출되는가? class Shape { public Shape(String msg) {... ; class Rectangle extends Shape { public Rectangle() {... ; public class RectangleTest public static void main(string[] args) { Rectangle r = new Rectangle(); ;

명시적인생성자호출 super 를이용하여서명시적으로수퍼클래스의생성자호출 class Shape { public Shape(String msg) { System.out.println("Shape 생성자 () " + msg); ; public class Rectangle extends Shape { public Rectangle(){ super("from Rectangle"); // 명시적인호출 System.out.println("Rectangle 생성자 ()"); ;

묵시적인생성자호출 class Shape { public Shape(String msg) { System.out.println("Shape 생성자 ()"); ; class Rectangle extends Shape { public Rectangle() { ; System.out.println("Rectangle 생성자 ()"); 1 부터 10 까지의정수의합 = 55 Shape 생성자 Rectangle 생성자

Object 클래스 Object 클래스는 java.lang 패키지에들어있으며자바클래스계층구조에서맨위에위치하는클래스

Object 의메소드 protected Object clone() 객체자신의복사본을생성하여반환한다. public boolean equals(object obj) obj 가이객체와같은지를나타낸다. protected void finalize() 가비지콜렉터에의하여호출된다. public final Class getclass() 객체를생성한클래스정보를반환한다. public int hashcode() 객체에대한해쉬코드를반환한다. public String tostring() 객체의문자열표현을반환한다.

getclass() class Car {... public class CarTest { public static void main(string[] args) { Car obj = new Car(); System.out.println("obj is of type " + obj.getclass().getname()); 1obj 부터 is 10까지의 of type 정수의 Car 합 = 55

equals() 메소드 class Car { private String model; public Car(String model) { this.model= model; public boolean equals(object obj) { if (obj instanceof Car) return model.equals(((car) obj).model); else return false; public class CarTest { public static void main(string[] args) { Car firstcar = new Car("HMW520"); Car secondcar = new Car("HMW520"); Object 의 equals() 를재정의 if (firstcar.equals(secondcar)) { System.out.println(" 동일한종류의자동차입니다."); else { System.out.println(" 동일한종류의자동차가아닙니다."); 동일한 1부터 10종류의까지의자동차입니다정수의합 = 55.

tostring() Object 클래스의 tostring() 메소드는객체의문자열표현을반환 public class Car { private String model; public Car(String model) { this.model = model; public String tostring() { return 모델 : + model; Object 의 tostring() 를재정의

종단클래스와종단메소드 키워드 final 을붙이면상속이나재정의할수없다. class Baduk { enum BadukPlayer { WHITE, BLACK... final BadukPlayer getfirstplayer() { return BadukPlayer.BLACK; 재정의할수없도록한다.

Q & A