티스토리 뷰
1. instanceof 연산자에 대하여 설명하시오.
if(ref instanceof ClassName)
왼쪽에는 객체명, 오른쪽에는 클래스명이 온다.
→ ref가 ClassName 클래스의 인스턴스를 참조하면 true 반환
→ ref가 ClassName를 상속하는 클래스의 인스턴스이면 true 반환
package edu.kosmo.ex.practices;
class Box {
public void simpleWrap() {
System.out.println("Simple Wrapping");
}
}
class PaperBox extends Box {
public void paperWrap() {
System.out.println("Paper Wrapping");
}
}
class GoldPaperBox extends PaperBox {
public void goldWrap() {
System.out.println("Gold Wrapping");
}
}
public class BoxTest {
public static void main(String[] args) {
Box box1 = new Box();
PaperBox box2 = new PaperBox();
GoldPaperBox box3 = new GoldPaperBox();
wrapBox(box1); // Simple Wrapping
wrapBox(box2); // Paper Wrapping
wrapBox(box3); // Gold Wrapping
}
public static void wrapBox(Box box) {
if (box instanceof GoldPaperBox) {
((GoldPaperBox) box).goldWrap();
}
else if (box instanceof PaperBox) {
((PaperBox) box).paperWrap();
}
else {
box.simpleWrap();
}
}
}
즉, instanceof가 된다는 것은 강제 형변환이 가능하다는 것이다.(참조형도 강제 형변환이 가능하다.)
PaperBox pb = (PaperBox)box;
pb.paperWrap();
↓ 같다.
((PaperBox)box).paperWrap();
------------------------------------------------------------------
자료형 강제 형 변환을 생각해보면 쉽다.
double d = 100;
int aa = (int)d;
2. 아래를 프로그래밍 하시오.
다음을 만족하는 클래스 Employee를 작성하시오.
- 클래스 Employee(직원)은
//클래스 Regular(정규직)와 Temporary(비정규직)의 상위 클래스
- 필드: 이름, 나이, 주소, 부서, 월급 정보를 필드로 선언
- 생성자 : 이름, 나이, 주소, 부서를 지정하는 생성자 정의
- 메소드 printInfo() : 인자는 없고 자신의 필드 이름, 나이, 주소, 부서를 출력
=======================================
다음을 만족하는 클래스 Regular를 작성하시오.
- 클래스 Regular는 위에서 구현된 클래스 Employee의 하위 클래스
- 생성자 : 이름, 나이, 주소, 부서를 지정하는 상위 생성자 호출
- Setter : 월급 정보 필드를 지정
- 메소드 printInfo() : 인자는 없고 기본적인 이름, 나이, 주소, 부서 를 출력후
"정규직 월급" 이라는 이름으로 월급출력
-상위 클래스에서 접근제한자 수정이 필요 하면 수정 할것.
package edu.kosmo.ex.practices;
class Employee{
private String name, address, dept;
private int age;
protected int salary;
public Employee(String name, int age, String address, String dept) {
this.name = name;
this.age = age;
this.address = address;
this.dept = dept;
}
public void printInfo() {
System.out.println("이름: " + name);
System.out.println("나이: " + age);
System.out.println("주소: " + address);
System.out.println("부서: " + dept);
}
}
class Regular extends Employee{
public Regular(String name, int age, String address, String dept) {
super(name, age, address, dept);
}
public void setSalary(int salary) {
super.salary = salary;
}
public void printInfo() {
super.printInfo();
System.out.println("정규직 월급: " + super.salary);
}
}
public class EmployeeTest {
public static void main(String[] args) {
Employee employee = new Employee("홍길동", 30, "New York", "IT");
employee.printInfo();
Regular r = new Regular("홍길동", 30, "New York", "IT");
r.setSalary(1000);
r.printInfo();
}
}
접근제한자를 private에서 상속받으면 사용 가능한 protected로 수정해주기!
3.Point 를 상속받아
3차원의 점을 나타내는 Point3D 클래스를 작성하라.
다음 main() 메소드를 포함하고 실행 결과와 같이 출력되게 하라.
==================================================
public static void main(String[] args) {
Point3D p = new Point3D(1,2,3); // 1,2,3은 각각 x, y, z축의 값.
System.out.println(p.toString()+"입니다.");
p.moveUp(); // z 축으로 위쪽 이동
System.out.println(p.toString()+"입니다.");
p.moveDown(); // z 축으로 아래쪽 이동
p.move(10, 10); // x, y 축으로 이동
System.out.println(p.toString()+"입니다.");
p.move(100, 200, 300); // x, y, z축으로 이동
System.out.println(p.toString()+"입니다.");
}
=====================================
class Point {
private int x, y;
public Point() {
}
public Point(int x, int y) {
this.move(x, y);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
protected void move(int x, int y) {
this.x =x;
this.y = y;
}
}
class ColorPoint extends Point{
private String color = "BLACK";
public ColorPoint() {
}
public ColorPoint(int x, int y, String color) {
super(x, y);
this.color = color;
}
public ColorPoint(int x, int y) {
super(x, y);
}
public void setXY(int x, int y) {
super.move(x, y);
}
public void setColor(String color) {
this.color = color;
}
public String toString() {
return color + "색의 ("+ super.getX()+", "+ super.getY()+")의 점";
}
}
출력
(1,2,3) 의 점입니다.
(1,2,4) 의 점입니다.
(10,10,3) 의 점입니다.
(100,200,300) 의 점입니다.
class Point {
private int x, y;
public Point() {
}
public Point(int x, int y) {
this.move(x, y);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
protected void move(int x, int y) {
this.x =x;
this.y = y;
}
}
class Point3D extends Point{
private int z;
public Point3D(int x, int y, int z) {
super(x, y);
this.z = z;
}
public String toString() {
return "(" + super.getX() + "," + super.getY() + "," + z +") 의 점";
}
public void moveUp() {
z++; // z += 1;
}
public void moveDown() {
z--; // z -= 1;
}
public void move(int x, int y, int z) {
super.move(x, y);
this.z = z;
}
}
public class Point3DTest {
public static void main(String[] args) {
Point3D p = new Point3D(1, 2, 3); // 1,2,3은 각각 x, y, z축의 값.
System.out.println(p.toString() + "입니다.");
p.moveUp(); // z 축으로 위쪽 이동
System.out.println(p.toString() + "입니다.");
p.moveDown(); // z 축으로 아래쪽 이동
p.move(10, 10); // x, y 축으로 이동
System.out.println(p.toString() + "입니다.");
p.move(100, 200, 300); // x, y, z축으로 이동
System.out.println(p.toString() + "입니다.");
}
}
4.자바의 정석 56Page 다시 한번 정리한후 개별 진척도 업데이트 해주세요.
'수업문제' 카테고리의 다른 글
[문제] 10월 27일 (Marker 인터페이스, 추상 클래스, 예외) (0) | 2021.10.27 |
---|---|
[문제] 10월 26일 (Object, interface, abstract) (0) | 2021.10.26 |
[문제] 10월 22일 (다형성, polymorphism, 오버라이딩) (0) | 2021.10.22 |
[문제] 10월 21일 (상속) (0) | 2021.10.21 |
- Total
- Today
- Yesterday
- SOCKET
- string
- hashset
- Request
- 쿠키
- toString
- 참조형
- el
- Session
- 프로토콜
- JSP
- exception
- 세션
- 제네릭
- TreeSet
- equals
- 예외처리
- 진척도 70번
- 래퍼 클래스
- Servlet
- 쓰레드
- 사칙연산 계산기
- 입출력
- response
- 채팅
- object
- Generic
- abstract
- 부트스트랩
- compareTo
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |