01. 클래스 만들기
º 클래스 A에는 정수 2개가 있고, 이 정수들에 값을 대입해 주는 setA() 메소드와 그 값들을 출력해 주는 showA() 메소드가 있다. 클래스 A를 작성하시오. (생성자는 없음)
º main()에서 클래스 A의 객체 ob를 생성하고, 값을 적절하게 대입한 후 showA()를 사용하여 출력해 보시오.
<1-1> 슈퍼클래스(A)
public class A {
int a1, a2;
public void setA(int a1, int a2) {
this.a1 = a1;
this.a2 = a2;
}
public void showA() {
System.out.println(a1 + "," + a2);
}
}
<1-2> 실행 클래스(AEx)
public class AEx {
public static void main(String[] args) {
A ob = new A();
ob.setA(1, 2);
ob.showA();
}
}
02. 클래스 상속
º 클래스 B는 클래스 A를 상속한다. 클래스 A와 유사하게 클래스 B를 선언하시오. 생성자는 없음. main()에서 클래스 B의 객체를 생성하고 변수 값을 적절하게 대입한 후 출력해 보시오.
<2-1> 서브 클래스(B)
public class B extends A {
int b1, b2;
public void setB(int a1, int a2, int b1, int b2) {
this.a1 = a1;
this.a2 = a2;
this.b1 = b1;
this.b2 = b2;
}
public void showB() {
showA();
System.out.println(b1 + "," + b2);
}
}
<2-2> 실행 클래스(BEx)
public class BEx {
public static void main(String[] args) {
A ob = new A(); //컴파일러가 생성해 주는 기본생성자가 호출
ob.setA(1,2);
ob.showA();
B ob2 = new B(); //컴파일러가 생성해 주는 기본생성자가 호출
ob2.setB(1, 2, 3, 4);
ob2.showB();
}
}
03. 클래스 상속과 객체
º (x, y)의 한 점을 표현하는 Point 클래스와 이를 상속받아 점을 표현하는 ColorPoint 클래스를 만들고 활용해보자.
<3-1> 슈퍼클래스(Point)
public class Point {
int x, y; //한 점을 구성하는 x, y 좌표
void set(int x, int y) {
this.x = x;
this.y = y;
}
void showPoint() { //점의 좌표 출력
System.out.println("(" + x + "," + y + ")");
}
}
<3-2> 서브클래스(ColorPoint)
public class ColorPoint extends Point { //Point를 상속받은 ColorPoint 선언
String color; //점의 색
void setColor(String color) {
this.color = color;
}
void showColorPoint() { //컬러 점의 좌표 출력
System.out.print(color);
showPoint(); //Point 클래스의 showPoint() 호출
}
}
<3-3> 실행클래스(ColorPointEx)
public class ColorPointEx {
public static void main(String[] args) {
Point p = new Point(); //Point 객체 생성
p.set(1, 2); //Point 클래스의 set() 호출
p.showPoint();
ColorPoint cp = new ColorPoint(); //ColorPoint 객체 생성
cp.set(3, 4); //Point 클래스의 set() 호출
cp.setColor("blue"); //ColorPoint 클래스의 setColor() 호출
cp.showColorPoint(); //컬러와 좌표 출력
04. 상속 관계에 있는 클래스 멤버 접근
º 클래스 Person을 아래와 같은 멤버 필드를 갖도록 선언하고 클래스 Student는 클래스 Person을 상속받아 각 멤버 필드에 값을 저장하시오.
- 이 예제에서 Person 클래스의 private 필드인 weight는 Student 클래스에서는 접근이 불가능하여 슈퍼 클래스인 Person의 get, set 메소드를 통해서만 조작이 가능하다.
- private int weight;
- int age;
- protected int height;
- public String name;
<4-1> 슈퍼클래스(Person)
public class Person {
int age;
public String name;
protected int height;
private int weight;
public void setWeight(int weight) {
this.weight = weight;
}
public int getWeight() {
return weight;
}
}
<4-2> 서브클래스(Student)
public class Student extends Person {
void set() {
age = 22;
name = "김이화";
height = 167;
setWeight(45);
}
void showStudent() {
System.out.println("age : " + age);
System.out.println("name : " + name);
System.out.println("height : " + height);
System.out.println("weight : " + getWeight());
}
}
<4-2> 실행클래스(StudentEx)
public class StudentEx {
public static void main(String[] args) {
Student s = new Student();
s.set();
s.showStudent();
}
}
05. 상속과 생성자
º (실습 1, 2)에서 작성한 클래스 A와 클래스 B에 기본(default) 생성자를 추가해 보시오.
- 생성자들이 어떻게 호출되는지 알아보기 위해 각 생성자 안에 자신이 호출되었음을 알리는 출력문을 넣어 보시오.
- main()에서 클래스 B의 객체를 한 개 생성시켜 보시오.
<5-1> 슈퍼클래스(A)
public class A {
int a1, a2;
public A() {
System.out.println("A");
}
public void setA(int a1, int a2) {
this.a1 = a1;
this.a2 = a2;
}
public void showA() {
System.out.println(a1 + "," + a2);
}
}
<5-2> 서브클래스(B)
public class B extends A {
int b1, b2;
public B() {
System.out.println("B");
}
public void setB(int a1, int a2, int b1, int b2) {
//this.a1 = a1;
//this.a2 = a2;
setA(a1, a2);
this.b1 = b1;
this.b2 = b2;
}
public void showB() {
showA();
System.out.println(b1 + "," + b2);
}
}
<5-3> 실행클래스(BEx)
public class BEx {
public static void main(String[] args) {
B ob = new B();
ob.setB(1, 2, 3, 4);
ob.showB();
B ob1 = new B();
ob1.setB(5, 6, 7, 8);
ob1.showB();
}
}
06. super()를 활용한 ColorPoint 클래스 작성
º super()를 이용하여 ColorPoint 클래스의 생성자에서 슈퍼 클래스 Point 생성자를 호출하는 프로그램을 작성하시오.
<6-1> 슈퍼클래스(Point)
public class Point {
int x, y; //한 점을 구성하는 x, y 좌표
public Point() { //기본 생성자
this.x = this.y = 0;
}
public Point(int x, int y) { //매개변수를 가진 생성자
this.x = x;
this.y = y;
}
void showPoint() { //점의 좌표 출력
System.out.println("(" + x + "," + y + ")");
}
}
<6-2> 서브클래스(ColorPoint)
public class ColorPoint extends Point { //Point를 상속받은 ColorPoint 선언
String color; //점의 색
//void setColor(String color) {
// this.color = color;
// }
public ColorPoint(int x, int y, String color) { //매개변수를 가진 생성자
super(x, y);
this.color = color;
}
void showColorPoint() { //컬러 점의 좌표 출력
System.out.print(color);
showPoint(); //Point 클래스의 showPoint() 호출
}
}
<6-3> 실행클래스(ColorPointEx)
public class ColorPointEx {
public static void main(String[] args) {
Point p = new Point(1 ,2); //Point 객체 생성
//p.set(1, 2); //Point 클래스의 set() 호출
p.showPoint();
ColorPoint cp = new ColorPoint(3, 4, "blue"); //ColorPoint 객체 생성
//cp.set(3, 4); //Point 클래스의 set() 호출
//cp.setColor("blue"); //ColorPoint 클래스의 setColor() 호출
cp.showColorPoint(); //컬러와 좌표 출력
}
}
07. super() 활용
º (실습2)에서 작성한 setA(), setB()를 없애고, 대신 이들의 역할을 대신하도록 각 클래스에 생성자들을 추가하시오.
- setA(int a1, int a2) -> public A(int a1, int a2)로 바꾸고,
- setB(int a1, int a2, int b1, int b2) -> public B(int a1, int a2, int b1, int b2)로 바꾼다.
<7-1> 수퍼클래스(A)
public class A {
int a1, a2;
public A() {
System.out.println("A");
}
public A(int a1, int a2) { //추가된 생성자
this.a1 = a1;
this.a2 = a2;
System.out.println("A1");
}
public void showA() {
System.out.println(a1 + "," + a2);
}
}
<7-2> 서브클래스(B)
public class B extends A {
int b1, b2;
public B() {
System.out.println("B");
}
public B(int a1, int a2, int b1, int b2) { //추가된 생성자
super(a1, a2);
this.b1 = b1;
this.b2 = b2;
System.out.println("B1");
}
public void showB() {
showA();
System.out.println(b1 + "," + b2);
}
}
<7-3> 실행클래스(BEx)
public class BEx {
public static void main(String[] args) {
B ob = new B(1, 2, 3, 4);
ob.showB();
B ob1 = new B(5, 6, 7, 8);
ob1.showB();
}
}
'JAVA > 자바 실습' 카테고리의 다른 글
Java | Lab10 (0) | 2021.06.09 |
---|---|
Java | Lab9 (0) | 2021.06.08 |
Java | Lab6 (0) | 2021.04.16 |
Java | Lab5 (0) | 2021.04.15 |
Java | Lab4_Ex (0) | 2021.04.12 |