PowerPoint Presentation

Similar documents
PowerPoint Presentation

쉽게 풀어쓴 C 프로그래밍

<4D F736F F F696E74202D20C1A63038C0E520C5ACB7A1BDBABFCD20B0B4C3BC4928B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

Microsoft PowerPoint - Lect04.pptx

Blog

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

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

JAVA PROGRAMMING 실습 08.다형성

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

PowerPoint Presentation

PowerPoint Presentation

PowerPoint Presentation

PowerPoint Presentation

PowerPoint Presentation

PowerPoint 프레젠테이션

쉽게 풀어쓴 C 프로그래밍

Design Issues

PowerPoint Presentation

PowerPoint Presentation

Microsoft PowerPoint - 2강

PowerPoint Presentation

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

Microsoft PowerPoint - C++ 5 .pptx

Microsoft PowerPoint - Java7.pptx

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

쉽게 풀어쓴 C 프로그래밍

PowerPoint Presentation

쉽게 풀어쓴 C 프로그래밍

17장 클래스와 메소드

쉽게 풀어쓴 C 프로그래밍

쉽게

쉽게 풀어쓴 C 프로그래밍

PowerPoint Presentation

JAVA PROGRAMMING 실습 05. 객체의 활용

PowerPoint 프레젠테이션

PowerPoint Presentation

TEST BANK & SOLUTION

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

PowerPoint 프레젠테이션

슬라이드 1

PowerPoint Presentation

Microsoft PowerPoint 장강의노트.ppt

Java ...

쉽게 풀어쓴 C 프로그래밍

Microsoft PowerPoint - java1-lab5-ImageProcessorTestOOP.pptx

PowerPoint 프레젠테이션

Microsoft Word - EEL2 Lab4.docx

JAVA PROGRAMMING 실습 02. 표준 입출력

교육자료

자바 프로그래밍

JAVA PROGRAMMING 실습 09. 예외처리

C++ Programming

PowerPoint Presentation

Microsoft Word - java19-1-midterm-answer.doc

05-class.key

PowerPoint 프레젠테이션

쉽게 풀어쓴 C 프로그래밍

PowerPoint 프레젠테이션

PowerPoint 프레젠테이션

PowerPoint Presentation

PowerPoint Presentation

제11장 프로세스와 쓰레드

PowerPoint 프레젠테이션

프입2-강의노트-C++배경

슬라이드 1

JAVA PROGRAMMING 실습 05. 객체의 활용

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

4장.문장

Microsoft PowerPoint - chap06-2pointer.ppt

PowerPoint 프레젠테이션

(Microsoft Word - \301\337\260\243\260\355\273\347.docx)

Microsoft PowerPoint - ch07 - 포인터 pm0415

JAVA PROGRAMMING 실습 02. 표준 입출력

PowerPoint Presentation

<4D F736F F F696E74202D2036C0CFC2B05FB0B4C3BCC1F6C7E2C7C1B7CEB1D7B7A1B9D62E707074>

No Slide Title

JAVA PROGRAMMING 실습 02. 표준 입출력

유니티 변수-함수.key

PowerPoint Presentation

C++ Programming

비긴쿡-자바 00앞부속

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µå

예제 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 Template

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

No Slide Title

슬라이드 1

5장.key

쉽게 풀어쓴 C 프로그래밍

쉽게 풀어쓴 C 프로그래밊

PowerPoint 프레젠테이션

슬라이드 1

PowerPoint Presentation

Cluster management software

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

금오공대 컴퓨터공학전공 강의자료

Microsoft PowerPoint - chap11

슬라이드 1

gnu-lee-oop-kor-lec11-1-chap15

C++ Programming

Transcription:

객체지향프로그래밍 클래스, 객체, 메소드 손시운 ssw5176@kangwon.ac.kr

실제세계는객체로이루어진다. 2

객체와메시지 3

객체지향이란? 실제세계를모델링하여소프트웨어를개발하는방법 4

객체 5

객체란? 객체 (Object) 는상태와동작을가지고있다. 객체의상태 (state) 는객체의특징값 ( 속성 ) 이다. 객체의동작 (behavior) 또는행동은객체가취할수있는동작 6

필드와메소드 7

중간점검문제 8

메시지 소프트웨어객체는메시지 (message) 를통해다른소프트웨어객체와통신 하고서로상호작용한다. 9

절차지향과객체지향 절차지향프로그래밍 (procedural programming): 문제를해결하는절차를 중요하게생각하는방법 객체지향프로그래밍 (Object-Oriented Programming): 데이터와절차를하 나의덩어리 ( 객체 ) 로묶어서생각하는방법이다. 10

절차지향과객체지향 11

객체지향방법 객체지향으로소프트웨어를작성하는것은컴퓨터하드웨어부품을구입하 여서컴퓨터를조립하는것과비슷하다. 12

중간점검문제 객체들은 전달을통해서서로간에상호작용을한다. 자동차객체에서생각할수있는메시지와매개변수에대하여나열하여보 라. 13

객체지향의 3 대특징 캡슐화 상속 다형성 14

캡슐화 캡슐화 (encapsulation): 관련된데이터와알고리즘 ( 코드 ) 이하나의묶음으로 정리되어있는것 15

캡슐화와정보은닉 정보은닉 (information hiding) 은객체를캡슐로싸서객체의내부를보호하 는하는것이다. 즉객체의실제구현내용을외부에감추는것이다. 16

캡슐화와정보은닉 17

업그레이드가쉽다. 라이브러리가업그레이드되면쉽게바꿀수있다. 정보은닉이가능하기때문에업그레이드가능 18

상속 상속 (inheritance): 이미작성된클래스 ( 부모클래스 ) 를이어받아서새로운클 래스 ( 자식클래스 ) 를생성하는기법 기존의코드를재활용하기위한기법 19

다형성 하나의이름 ( 방법 ) 으로많은상황에대처하는기법 개념적으로동일한작업을하는멤버함수들에똑같은이름을부여할수있 으므로코드가더간단해진다 20

추상화 21

객체지향의장점 신뢰성있는소프트웨어를쉽게작성할수있다. 코드를재사용하기쉽다. 업그레이드가쉽다. 디버깅이쉽다. 22

중간점검문제 자바에서코드재사용이쉬운이유는관련된 와 이하나의덩어리로묶여있기때문이다. 정보은닉이란 을외부로부터보호하는것이다. 정보를은닉하면발생하는장점은무엇인가? 23

클래스 클래스 (class): 객체를만드는설계도 클래스로부터만들어지는각각의객체를특별히그클래스의인스턴스 (instance) 라고도한다. 24

클래스의구조 25

클래스의예 텔레비젼 26

예제 : 객체생성하기 27

28

변수의종류 기초변수 (primitive variable) 에는실제데이터값이저장된다. 참조변수 (reference variable) 는참조변수는객체를참조할때사용되는변 수로서여기에는객체의참조값이저장된다. 29

객체의필드와메소드사용 도트 (.) 연산자사용! 30

여러개의객체생성하기 31

32

실행결과 33

메소드 메소드는입력을받아서처리를하고결과를반환하는가상적인상자와같 다. 34

메소드의구조 35

예제 public class Television { int channel; // 채널번호 int volume; // 볼륨 boolean onoff; // 전원상태 void print() { System.out.println(" 채널은 " + channel + " 이고볼륨은 " + volume + " 입니다."); 36

예제 public class TelevisionTest { public static void main(string[] args) { Television mytv = new Television(); mytv.channel = 7; mytv.volume = 10; mytv.onoff = true; mytv.print(); Television yourtv = new Television(); yourtv.channel = 9; yourtv.volume = 12; yourtv.onoff = true; yourtv.print(); 채널은 7 이고볼륨은 10 입니다. 채널은 9 이고볼륨은 12 입니다. 37

예제설명 38

메소드의종료 return 을사용한다. void mymethod() { for( int i=0; i<10; i++ ) { if( i == 7 ) return; 39

메소드의반환값 40

예제 public class Television { int channel; // 채널번호 int volume; // 볼륨 boolean onoff; // 전원상태 void print() { System.out.println(" 채널은 " + channel + " 이고볼륨은 " + volume + " 입니다."); int getchannel() { return channel; 41

예제 public class TelevisionTest { public static void main(string[] args) { Television mytv = new Television(); mytv.channel = 7; mytv.volume = 9; mytv.onoff = true; int ch = mytv.getchannel(); System.out.println(" 현재채널은 " + ch + " 입니다."); 현재채널은 7 입니다. 42

예제설명 43

인수와매개변수 메소드호출시전달하는값을인수 (argument) 메소드에서값을받을때사용하는변수를매개변수 (parameter) 44

예제 public class Math { int add(int x, int y) { return x + y; public class MathTest { public static void main(string[] args) { int sum; Math obj = new Math(); sum = obj.add(2, 3); System.out.println("2와 3의합은 " + sum); sum = obj.add(7, 8); System.out.println("7와 8의합은 " + sum); 2 와 3 의합은 5 7 와 8 의합은 15 45

예제설명 46

예제 public class Television { int channel; // 채널번호 int volume; // 볼륨 boolean onoff; // 전원상태 void print() { System.out.println(" 채널은 " + channel + " 이고볼륨은 " + volume + " 입니다."); int getchannel() { return channel; void setchannel(int ch) { channel = ch; 47

예제 public class TelevisionTest { public static void main(string[] args) { Television mytv = new Television(); mytv.setchannel(11); int ch = mytv.getchannel(); System.out.println(" 현재채널은 " + ch + " 입니다."); 현재채널은 11 입니다. 48

예제설명 49

Lab: 자동차클래스작성 50

예제 public class Car { String color; // 색상 int speed; // 속도 int gear; // 기어 @Override public String tostring() { return "Car [color=" + color + ", speed=" + speed + ", gear=" + gear + "]"; void changegear(int g) { gear = g; void speedup() { speed = speed + 10; void speeddown() { speed = speed - 10; 51

예제 public class CarTest { public static void main(string[] args) { Car mycar = new Car(); mycar.changegear(1); mycar.speedup(); System.out.println(myCar); Car [color=null, speed=10, gear=1] 52

메소드오버로딩 자바에서는같은이름의메소드가여러개존재할수있다. 이것을메소드 오버로딩 (method overloading) 이라고한다. 53

예제 public class MyMath { // 정수값을제곱하는메소드 int square(int i) { return i * i; // 실수값을제곱하는메소드 double square(double i) { return i * i; 54

예제 public class MyMathTest { public static void main(string args[]) { MyMath obj = new MyMath(); System.out.println(obj.square(10)); System.out.println(obj.square(3.14)); 100 9.8596 55

예제설명 56

String 클래스 문자열은자바에서기초자료형이아니다. 그러나문자열을저장하고처리하는 String 이라고하는클래스가존재한다 57

String 클래스의객체생성 String s = new String("Hello World!"); // 선언과동시에초기화 58

String 클래스의메소드 59

String 클래스사용하기 public class StringTest { public static void main (String[] args) { String proverb = "A barking dog"; // new 연산자생략 String s1, s2, s3, s4; // 참조변수로서메소드에서반환된참조값을받는다. System.out.println (" 문자열의길이 =" + proverb.length()); s1 = proverb.concat (" never Bites!"); // 문자열결합 s2 = proverb.replace ('B', 'b'); // 문자교환 s3 = proverb.substring (2, 5); // 부분문자열추출 s4 = proverb.touppercase(); // 대문자로변환 System.out.println(s1); System.out.println(s2); System.out.println(s3); System.out.println(s4); 문자열의길이 =13 A barking dog never Bites! A barking dog bar A BARKING DOG 60

문자열의결합 + 연산자를사용한다. String subject = "Money"; String other = " has no value if it is not used"; String sentence = subject + other; // "Money has no value if it is not used" 61

수치값 -> 문자열 자바에서는문자열과기초자료형변수를결합하게되면자동적으로기초 자료형을문자열로변환한다. int x = 20; System.out.println(" 결과값은 " + x); // 결과값은 20 이출력된다. String answer = "The answer is " + 100; // The answer is 100" 62

문자열 -> 수치값 즉문자열 123 을숫자 123 으로변환하려면어떻게하여야하는가? 자바에 는이것을전문으로해주는클래스가있다. 바로랩퍼클래스인 Integer 클 래스이다. 63

예제 문자열을기초자료형으로변환하려면각랩퍼클래스의 parsexxx() 메소드 를사용한다. int i = Integer.parseInt("123"); // 변수 i 에정수 123 이저장된다. double d = Double.parseDouble("3.141592"); // 변수 d 에실수 3.141592 가저장된다. 64

LAB: 사용자에게문자열을받아서문자열이 www 로시작하는지를검사하는프 로그램을작성해보자. 사용자가 quit 를입력하면프로그램을종료한다. 문자열을입력하세요 > www.google.com www.google.com 은 'www' 로시작합니다. 문자열을입력하세요 > naver.com naver.com 은 'www' 로시작하지않습니다. 문자열을입력하세요 > quit 65

String 클래스사용하기 import java.util.scanner; public class StringTest { public static void main(string a[]) { String str; Scanner sc = new Scanner(System.in); while (true) { System.out.print(" 문자열을입력하세요 > "); str = sc.next(); if (str.equals("quit") == true) break; if (str.matches("^www\\.(.+)")) { System.out.println(str + " 은 'www' 로시작합니다."); else { System.out.println(str + " 은 'www' 로시작하지않습니다."); 66