5장.key

Similar documents
PowerPoint 프레젠테이션

Microsoft PowerPoint - 5장

JAVA PROGRAMMING 실습 08.다형성

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

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

비긴쿡-자바 00앞부속

자바 프로그래밍

PowerPoint 프레젠테이션

01-OOPConcepts(2).PDF

09-interface.key

JAVA PROGRAMMING 실습 07. 상속

PowerPoint Presentation

쉽게 풀어쓴 C 프로그래밍

Microsoft PowerPoint - 6주차.pptx

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

PowerPoint 프레젠테이션

rmi_박준용_final.PDF

02 C h a p t e r Java

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

PowerPoint 프레젠테이션

1

PowerPoint Presentation

Microsoft PowerPoint - 2강

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

PowerPoint 프레젠테이션

PowerPoint Presentation

PowerPoint Presentation

PowerPoint Presentation

JMF3_심빈구.PDF

쉽게 풀어쓴 C 프로그래밍

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

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

Microsoft Word - java18-1-final-answer.doc

6장.key

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

Modern Javascript

교육자료

Microsoft PowerPoint - Java7.pptx

12-file.key

<4D F736F F F696E74202D20C1A63038C0E520C5ACB7A1BDBABFCD20B0B4C3BC4928B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

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

Microsoft PowerPoint - java1-lab5-ImageProcessorTestOOP.pptx

PowerPoint Presentation

쉽게 풀어쓴 C 프로그래밍

untitled

Spring Boot/JDBC JdbcTemplate/CRUD 예제

Microsoft PowerPoint - 04-UDP Programming.ppt

@OneToOne(cascade = = "addr_id") private Addr addr; public Emp(String ename, Addr addr) { this.ename = ename; this.a

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

chap10.PDF

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

PowerPoint Presentation

JAVA PROGRAMMING 실습 05. 객체의 활용

JAVA PROGRAMMING 실습 02. 표준 입출력

신림프로그래머_클린코드.key

ilist.add(new Integer(1))과 같이 사용하지 않고 ilist.add(1)과 같이 사용한 것은 자바 5.0에 추가된 기본 자료형과 해당 객체 자료 형과의 오토박싱/언박싱 기능을 사용한 것으로 오토박싱이란 자바 컴파일러가 객체를 요구하는 곳에 기본 자료형

어댑터뷰

Something that can be seen, touched or otherwise sensed

PowerPoint Presentation

PowerPoint Presentation

PowerPoint Presentation

Microsoft Word - EEL2 Lab4.docx

Microsoft PowerPoint - Lect04.pptx

PowerPoint 프레젠테이션

OOP 소개

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

Week3

제11장 프로세스와 쓰레드

PowerPoint Presentation

<4D F736F F F696E74202D20C1A63233C0E520B1D7B7A1C7C820C7C1B7CEB1D7B7A1B9D628B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

쉽게 풀어쓴 C 프로그래밍

PowerPoint 프레젠테이션

Java

* Factory class for query and DML clause creation * tiwe * */ public class JPAQueryFactory implements JPQLQueryFactory private f

mytalk

JavaGeneralProgramming.PDF

A Tour of Java IV

Interstage5 SOAP서비스 설정 가이드

歯JavaExceptionHandling.PDF

03-JAVA Syntax(2).PDF

Microsoft PowerPoint - 14주차 강의자료

11 템플릿적용 - Java Program Performance Tuning (김명호기술이사)

Transcription:

JAVA Programming 1

(inheritance) 2!,!!

4 3

4!!!!

5 public class Person {... public class Student extends Person { // Person Student... public class StudentWorker extends Student { // Student StudentWorker...! -> (super class)! -> (sub class)! extends

5-1 : - Point ColorPoint 6 (x, y) Point ColorPoint. class Point { private int x, y; // x, y public void set(int x, int y) { this.x = x; this.y = y; public void showpoint() { // System.out.println("(" + x + "," + y + ")"); // Point ColorPoint class ColorPoint extends Point { private String color; // public void setcolor(string color) { this.color = color; public void showcolorpoint() { // System.out.print(color); showpoint(); // Point showpoint() public class ColorPointEx { public static void main(string [] args) { Point p = new Point(); // Point p.set(1, 2); // Point set() p.showpoint(); (1,2) red(3,4) ColorPoint cp = new ColorPoint(); // ColorPoint cp.set(3, 4); // Point set() cp.setcolor("red"); // ColorPoint setcolor() cp.showcolorpoint(); //

5-1 7 * new ColorPoint()

8

9 java.lang.object java.lang.object

10 4! public, protected,, private private protected private! private!! public! public protected!! protected

11

5-2: 12 Person Student Person. Person private weight Student Person getxxx, setxxx. int age; public String name; protected int height; private int weight; class Person { private int weight; int age; protected int height; public String name; public void setweight(int weight) { this.weight = weight; public int getweight() { return weight; class Student extends Person { public void set() { age = 30; // name = ""; // public height = 175; // protected // weight = 99; //. private setweight(99); // private weight setweight() public class InheritanceEx { public static void main(string[] args) { Student s = new Student(); s.set();

/ 13 new!!,!

14

15!!!! super()

16

17 Implicit super constructor A() is undefined. Must explicitly invoke another constructor

18

super() 19 super()! super(parameter);

20 super()

5-3 : super() ColorPoint 21 super() ColorPoint Point. x=5, y=6 class Point { private int x, y; // x, y public Point() { this.x = this.y = 0; public Point(int x, int y) { this.x = x; this.y = y; public void showpoint() { // System.out.println("(" + x + "," + y + ")"); class ColorPoint extends Point { private String color; // public ColorPoint(int x, int y, String color) { super(x, y); // Point Point(x, y) this.color = color; public void showcolorpoint() { // System.out.print(color); showpoint(); // Point showpoint() public class SuperEx { public static void main(string[] args) { ColorPoint cp = new ColorPoint(5, 6, "blue"); cp.showcolorpoint(); x=5, y=6, color = "blue" blue(5,6)

. 22.,.

(upcasting) 23!!?! class Person { class Student extends Person { Student s = new Student(); Person p = s; //,!

24

(downcasting) 25?!! class Person { class Student extends Person {... Person p = new Student(""); // Student s = (Student)p; //, (Student)

26

instanceof 27! ) '' (), instanceof!! instanceof : true/false

? 28 1. 2. Person p = new Person(); Person p = new Student(); // Person p = new Researcher(); // Person p = new Professor(); // 3. print(p);, print() person void print(person person) { // person Person, // Student, Researcher, Professor....

29 Person person

30 instanceof

5-4 : instanceof 31 instanceof.? class Person { class Student extends Person { class Researcher extends Person { class Professor extends Researcher { public class InstanceOfEx { static void print(person p) { if(p instanceof Person) System.out.print("Person "); if(p instanceof Student) System.out.print("Student "); if(p instanceof Researcher) System.out.print("Researcher "); if(p instanceof Professor) System.out.print("Professor "); System.out.println(); public static void main(string[] args) { System.out.print("new Student() ->\t"); print(new Student()); System.out.print("new Researcher() ->\t"); print(new Researcher()); System.out.print("new Professor() ->\t"); print(new Professor()); new Professor() Person, Researcher, Professor new Student() -> Person Student new Researcher() -> Person Researcher new Professor() -> Person Researcher Professor

32 (Method Overriding)!,,!,!

33 Shape draw() Line, Rect, Circle

34

, 35!,! ' ( ) ' Line draw() Circle draw() Rect draw()!

5-5 : 36 class Shape { // public Shape next; public Shape() { next = null; public void draw() { System.out.println("Shape"); class Line extends Shape { public void draw() { // System.out.println("Line"); class Rect extends Shape { public void draw() { // System.out.println("Rect"); class Circle extends Shape { public void draw() { // System.out.println("Circle"); public class MethodOverridingEx { static void paint(shape p) { p.draw(); // p draw(). // public static void main(string[] args) { Line line = new Line(); paint(line); paint(new Shape()); paint(new Line()); paint(new Rect()); paint(new Circle()); Line Shape Line Rect Circle

37

// 5-5 Shape, Line, Rect, Circle public class UsingOverride { public static void main(string [] args) { Shape start, last, obj; // start = new Line(); // Line last = start; obj = new Rect(); last.next = obj; // Rect last = obj; obj = new Line(); // Line last.next = obj; last = obj; obj = new Circle(); // Circle last.next = obj; // Shape p = start; while(p!= null) { p.draw(); p = p.next; Line Rect Line Circle 38

(run time) 39

super 40 super super

5-6 : 41 41 Weapon fire(). fire() 1. class Weapon { protected int fire() { return 1; // Weapon Cannon. Cannon 10. fire(). main(). class Cannon extends Weapon { @Override protected int fire() { // return 10; // 10 public class Overriding { public static void main(string[] args) { Weapon weapon; weapon = new Weapon(); System.out.println(" " + weapon.fire()); weapon = new Cannon(); System.out.println(" " + weapon.fire()); 1 10

42 vs.

43 (abstract method)!, abstract public abstract String getname(); public abstract void setname(string s);! (abstract class) 2 1. abstract 2. abstract

2 44 // 1. abstract class Shape { // public Shape() { public void paint() { draw(); abstract public void draw(); // // 2. abstract class MyComponent { // String name; public void load(string name) { this.name = name;

45 abstrct class Shape { public class AbstractError { public static void main(string [] args) { Shape shape; shape = new Shape(); //. Shape....

46 2 1., abstract abstract class Shape { // public Shape() { public void paint() { draw(); abstract public void draw(); // abstract class Line extends Shape { //. draw() public String tostring() { return "Line"; 2. ()

47 drow(). draw()

48!!

5-7 : 49 Calculator GoodCalc. abstract class Calculator { public abstract int add(int a, int b); public abstract int subtract(int a, int b); public abstract double average(int[] a);

5-7 50 public class GoodCalc extends Calculator { @Override public int add(int a, int b) { // return a + b; @Override public int subtract(int a, int b) { // return a - b; @Override public double average(int[] a) { // double sum = 0; for (int i = 0; i <a.length; i++) sum += a[i]; return sum/a.length; public static void main(string [] args) { GoodCalc c = new GoodCalc(); System.out.println(c.add(2,3)); System.out.println(c.subtract(2,3)); System.out.println(c.average(new int [] { 2,3,4 )); 5-1 3.0

51 (). ()

52!! ( )! Interface! Ex) public interface SerialDriver {! : public abstract.! : public static final.!

53

54 abstract public public static final

55! extends interface MobilePhoneInterface extends PhoneInterface { void sendsms(); // void receivesms(); //! interface MusicPhoneInterface extends MobilePhoneInterface, MP3Interface { void playmp3ringtone(); //

56! implements!! class SamsungPhone implements MobilePhoneInterface { // // MobilePhoneInterface. public void sendcall() {... public void receivecall() {... public void sendsms() {... public void receivesms() {... //. public int getbuttons() {...

57, MobilePhoneInterface sendcall() sendcall() sendcall() sendcall() sendcall() IPhone SamsungPhone LGPhone

58 interface AIInterface { void recognizespeech(); // void synthesizespeech(); // class AIPhone implements MobilePhoneInterface, AIInterface { // // MobilePhoneInterface. public void sendcall() {... public void receivecall() {... public void sendsms() {... public void receivesms() {... // AIInterface. public void recognizespeech() {... // public void synthesizespeech() {... // //. public int touch() {...

5-8 : 59 interface PhoneInterface { int BUTTONS = 20; // void sendcall(); void receivecall(); interface MobilePhoneInterface extends PhoneInterface { void sendsms(); void receivesms(); interface MP3Interface { // public void play(); public void stop(); class PDA { public int calculate(int x, int y) { return x + y; class SmartPhone extends PDA implements MobilePhoneInterface, MP3Interface { @Override public void sendcall() { System.out.println(" "); @Override public void receivecall() { System.out.println(" "); @Override public void sendsms() { System.out.println("SMS "); @Override public void receivesms() { System.out.println("SMS "); // MP3Interface @Override public void play() { System.out.println(" "); @Override public void stop() { System.out.println(" "); // @Override public void schedule() { System.out.println(" "); public class InterfaceEx { public static void main(string [] args) { SmartPhone p = new SmartPhone(); p.sendcall(); p.play(); System.out.println(p.calculate(3,5)); p.schedule(); 8

60!,!

61