수업교재]05.자바 객체개념
2)객체지향의 기본원리: 객체지향 프로그램은 프로시저(함수) 추상화와 data추상화를 묶은 것
원리 | 내용 |
추상화 abstract | - 현실세계를 그대로 표현하기보다 중요한 측면에 주목, 상세내역을 없애 나가는 과정 ex) InterFace |
상속성 inheritance | - Super Class가 갖는 성질을 Sub Class에 자동으로 부여 - PGM을 쉽게 확장할 수 있게 만드는 수단 ex) Extends, Implements |
다형성 Polymorphysm | - 동일 Interface가 서로 다르게 응답할 수 있는 특성 - 연관 Class를 위한 일관된 매개체를 개발하는 수단 ex1) OverLoadint: 동일한 이름의 Operation(Method) 사용 가능 ex2) OverRiding: Super Class으 Method를 sub Class에서 재정의 |
Capsule화 | - 객체의 상세 내역을 객체외부에 철저히 숨기고, 단순히 Message만으로 객체와 상호작용 - 객체 내부 구조와 실체 분리로 내부 반경이 PGM에 미치는 영향 최소화 - 유지보수가 용이함 ex) Encapsulation |
14)인터페이스와 추상클래스
interface | abstract | |
정의 | - 추상 메소드(abstract)를 선언해 놓고 상속을 통해 자식 클래스에서 메소드를 완성하도록 유도하는 클래스 - 개발 코드와 객체가 서로 통신하는 접점 역할 |
- 하위 클래스들의 공통점(추상 메소드)만을 모아 추상화하여 만든 클래스 ↔ |
구현 | - 추상적인 interface를 implements가 구현해준다 - interface 선언은 class 키워드 대신에 interface 키워드를 사용한다 |
- - - |
특징 및 목적 | 1. ⭐⭐⭐⭐⭐표준화 가능, 즉 필드와 메서드 이름을 통일하여 유지보수성을 높이고 통일성을 유지할 수 있음 2. ⭐다중상속 지원 (극히 일부분...) 3. ⭐⭐⭐class와 class를 연결해줌 |
1. 메소드, 생성자를 선언할 때, 어떤 형태로 데이터값이 들어올지 정의해줌 2. 하나 이상의 추상 메소드 포함, 일반적인 메소드와 추상메소드 포함 가능, Abstract 클래스명으로 표기 |
변수 | 추상 메소드라 변수가 안됨 다만, Java로 넘어오면 default 메소드와 상수(Static Final) Static 메소드는 가능해짐 |
일반 변수 가능 |
추상메소드 | 보유 가능 | 보유 가능 |
일반메소드 | 보유 불가능 | 보유 가능 |
다중상속 | 가능 | 불가능 |
공통점 | 반드시 추상 메소드를 구현해야 한다 |
⭐⭐⭐⭐⭐Interface는 implement, class는 extends
Inheritance 상속: 부모 메소드로 선언하고 자식 메소드에 메모리를 입력한다
- class끼리 상속받을 때는 extends
- interface끼리 상속받을 때는 extends
↳ 같은 것 끼리 상속받을 때는 extends - interface를 구현할 때 implements
↳ interface가 class에게 상속할때 implements
⭐⭐⭐⭐⭐ 면접 때 반드시 나옴!!
비교 | implements | extends |
항목 | interface | class |
상속 | interface | class |
사용 | 표준화, 부품화 할 때 사용 | Class를 재사용할 때 |
상속의 개수 | multiple | single |
공통점 | Inheritance 상속 |
수업교재]06.자바 객체개념
7)⭐⭐⭐⭐⭐기본형 매개변수와 참조형 매개변수: 다형성10%, 라이딩 80%, 오버로딩과의 차이점 10%
변수 | 기본형 (primitive) 매개변수 | 참조형(reference) 매개변수 |
차이 | - 변수의 값을 읽기만 할 수 있다(read only) - call by value 방식(값을 전달) |
- 변수의 값을 읽고 변경할 수 있다 - call by reference 방식 |
예시 |
int, long, double, float, boolean, byte, short, char | String,Integer |
예시2 int와 Integer 비교 |
- 산술 연산 가능함 - null로 초기화 불가능 |
- Unboxing하지 않을 시 산술 연산 불가능함 - null값 처리 가능 |
10)⭐⭐⭐⭐⭐오버로딩과 오버라이딩 비교
- 오버로딩: 같은 이름의 메서드를 지원하면서 매개변수의 유형과 개수가 다르도록 하는 기술
↓↓↓ 오버로딩 예제: 이름이 cat인 메서드는 총 3개 있지만 매개변수 유형과 개수가 모두 다름을 확인 할 수 있다.
class OverloadingTest{
//이름이 cat인 메소드
void cat(){
System.out.println("매개변수 없음");
}
void cat(int a, int b){
System.out.println("매개변수:"+a+","+b);
}
void cat(String c){
System.out.println("매개변수:"+c);
}
}
public class Overtest{
public static main(String[] args){
//OverloadingTest 객체 생성
OverloadingTest ot = new OverloadingTest();
ot.cat();
ot.cat(2,3);
ot.cat("hello");
- 오버라이딩: 부모 메소드의 방식을 자식 메소드가 받고 재정의 하는 것을 오버라이딩 이라 한다
class Woman{
public String name;
public int age;
//info()메서드
public void info(){
System.out.println("여자의 이름은"+name+"이고, 나이는"+age+"입니다.");
}
}
class Job extends Woman{
String job;
public void info(){
System.out.println("여자의 직업은"+job+"입니다.");
}
}
public class OverridingTest{
public static main(String[] args){
Job job = new Job();
// 부모클래스를 상속받았으므로 자식클래스 내 별도 선언 없이 부모클래스 내 멤버변수에 접근 가능
job.name="유리";
job.age=20;
job.job="프로그래머";
// 호출
job.info();
//(중요) 실행결과 : 여자의 직업은 프로그래머입니다.
}
}
오늘의 코딩
순서
ch08_ex03>>
- CellPhone + DmbCellPhone + DmbCellPhoneEx
ch08_ex04>>
- Tire
package ch08_ex04;
public class Tire {
//필드
public int maxRotation; //최대 회전수(최대 수명)
public int accumulateRotation; //누적 회전수
public String location; //타이어의 위치
//생성자
public Tire(String location, int maxRotation) {
this.location = location;
this.maxRotation = maxRotation;
}
//메소드
public boolean roll() {
++accumulateRotation;
// 6 2 3 4
if(accumulateRotation<maxRotation) {
System.out.println(location + " Tire 수명: " + (maxRotation-accumulateRotation) + "회");
return true;
} else {
System.out.println("***" + location + "Tire 펑크 ***");
return false;
}
}
}
- HanKookTire
public class HankookTire extends Tire {
public HankookTire(String location, int maxRotation) {
super(location, maxRotation);
// TODO Auto-generated constructor stub
}
@Override
public boolean roll() {
++accumulatedRotation;
if(accumulatedRotation<maxRotation) {
System.out.println(location + " HankookTire 수명: " + (maxRotation-accumulatedRotation) + "회");
return true;
} else {
System.out.println("*** " + location + " HankookTire 펑크 ***");
return false;
}
}
}
- KumhoTire
package ch08_ex04;
public class KumhoTire extends Tire {
public KumhoTire(String location, int maxRotation) {
super(location, maxRotation);
}
@Override
public boolean roll() {
++accumulateRotation;
if(accumulateRotation<maxRotation) {
System.out.println(location+ "KumhoTire 수명" + (maxRotation-accumulateRotation) + "회");
return true;
} else {
System.out.println("*** " + location + "KumhoTire 펑크 ***");
return false;
}
}
}
- Car >>> association 정의 알아두기(키워드: 연관)
package ch08_ex04;
public class Car {
//필드
Tire frontLeftTire = new Tire("앞왼쪽", 6);
Tire frontRightTire = new Tire("앞오른쪽", 2);
Tire backLeftTire = new Tire("뒤왼쪽", 3);
Tire backRightTire = new Tire("뒤오른쪽", 4);
//생성자
//메소드
int run() {
System.out.println("[자동차가 달립니다.]");
if(frontLeftTire.roll()==false) {
stop();
return 1;
};
if(frontRightTire.roll()==false) {
stop();
return 2;
};
if(backLeftTire.roll()==false) {
stop();
return 3;
};
if(backRightTire.roll()==false) {
stop();
return 4;
};
return 0;
}
void stop() {
System.out.println("자동차가 멈춥니다");
}
}
- CarEx>> 5개 복습 필수
package ch08_ex04;
//복습!
public class CarEx {
public static void main(String[] args) {
Car car = new Car();
for(int i=1; i<=5; i++) { //6 2 3 4
int problemLocation = car.run();
switch(problemLocation) {
case 1:
System.out.println("앞왼쪽 HanKookTire로 교체");
car.frontLeftTire = new HanKookTire("앞왼쪽", 15);
break;
case 2:
System.out.println("앞오른쪽 KumhoTire로 교체");
car.frontRightTire = new HanKookTire("앞오른쪽", 13);
break;
case 3:
System.out.println("뒤왼쪽 HanKookTire로 교체");
car.backLeftTire = new HanKookTire("앞왼쪽", 14);
break;
case 4:
System.out.println("뒤오른쪽 KumhoTire로 교체");
car.backRightTire = new HanKookTire("앞왼쪽", 17);
break;
}
System.out.println("-------------------------------------");
}
}
}
ch08_ex05>>
- Person2
- Student2
- Teacher2
- Manager2
- Person2Ex
ch08_ex06>>
- DataAccessObject
- MySqlDao
- OracleDao
- DaoEx
ch08_ex07>>
- RemoteControl
- Audio
- Television
- RemoteControlEx
ch08_robot, ch08_robot.action >>
- Robot
- KnifeAction
- FireAction
- FlyAction
- FireOk
- FireNo
- FlyOk
- FlyNo
- CheapRobot
- StandardRobot
- SuperRobot
- MainClass
오늘의 과제
- ch08_ex03>> CellPhone + DmbCellPhone+ DmbCellPhoneEx
//결과화면
모델: 자바폰
색상: 검정
채널: 10
전원을 켭니다.
벨이 울립니다.
자기: 여보세요
상대방: 안녕하세요! 저는 홍길동인데요
자기: 아~ 예 반갑습니다.
전화를 끊습니다.
채널 10번 DMB 방송 수신을 시작합니다.
채널 12번으로 바꿉니다.
DMB 방송 수신을 멈춥니다.
- ch08_robot.action>> KnifeAction
//HW knifeAction Robot
SuperRobot주문. 만들어 주세요.
기본적으로 팔, 다리, 머리, 몸통 존재. 그리고 날수 있습니다.
팔, 다리, 머리, 몸통이 있죠.
날수 있어요.
대포동 미사일 발사
레이저 검을 사용.
----------------------------------------------
StandardRobot이 주문. 만들어 주세요.
기본적으로 팔, 다리, 머리, 몸통 존재. 그리고 달릴수 있습니다.
팔, 다리, 머리, 몸통이 있죠.
날수 없어요
대포동 미사일 발사
목검을 사용.
----------------------------------------------
CheapRobot이 주문. 만들어 주세요.
기본적으로 팔, 다리, 머리, 몸통 존재. 그리고 걷을수 있습니다.
팔, 다리, 머리, 몸통이 있죠.
날수 없어요
미사일 발사 기능 없습니다.
검이 없습니다.
'Java' 카테고리의 다른 글
2024_06_04_화 (0) | 2024.06.04 |
---|---|
2024_06_03_월 (0) | 2024.06.03 |
2024_05_30_목 (0) | 2024.05.30 |
2024_05_29_수 (0) | 2024.05.29 |
2024_05_28_화 (0) | 2024.05.29 |