1번 - 택시
package sevenClass;
public class Test0509_01 {
public static void main(String[] args) {
Taxi mytaxi = new Taxi("1");
}
}
class Car{
Car(){}
Car(String name){
System.out.println("이름이 있는 카 생성자");
}
}
class Taxi extends Car{
Taxi(){
super("택시");
System.out.println("택시 생성자 인증");
}
Taxi(String name){
super(name);
System.out.println(name + " 생성자");
}
}
Car Class
기본 생성자, 이름을 매개인자로 받는 생성자
Taxi Class
Car Class를 상속받는 클래스 생성
기본 생성자, 이름을 매개인자로 받는 생성자
Car Class에 이름 전달
Main
이름을 1로 가지는 Taxi 객체 생성
결과
이름이 있는 카 생성자
1 생성자
2번 - 인사
package sevenClass;
public class Test0509_02 {
public static void main(String[] args) {
EngHello eng = new EngHello();
eng.hello();
eng.test();
}
}
class Greeting{
String name = "홍길동";
void hello() {
System.out.println("씨 안녕하세요");
}
}
class EngHello extends Greeting{
String name = "Mr. Hong";
void hello() {
System.out.println(name + " Nice to meet you");
}
void test() {
System.out.print(super.name);
super.hello();
}
}
Greeting Class
이름을 지정
return 값이 없는 메소드 hello 생성
EngHello Class
Greeting Class를 상속받는 클래스 생성
return 값이 없는 메소드 hello 생성
return 값이 없는 메소드 test 생성
- 부모 클래스의 메소드 hello 호출
Main
EngHello 객체 eng 생성
hello, test 호출
결과
Mr. Hong Nice to meet you
홍길동씨 안녕하세요
3번 - 할아버지
package sevenClass;
public class Test0509_03 {
public static void main(String[] args) {
Father f = new Father("홍길동");
}
}
class GrandFather{
private String name;
GrandFather(String n){
this.name = n;
System.out.println("할아버지 생성자");
}
}
class Father extends GrandFather{
Father(String n){
super(n);
System.out.println("아빠 생성자");
}
}
GrandFather Class
접근불가 name 생성
이름을 매개인자로 받는 생성자 생성
Father Class
GrandFather Class를 상속받는 클래스 생성
이름을 매개인자로 받는 생성자 생성
Main
이름을 '홍길동'으로 주는 Father 객체 f 생성
결과
할아버지 생성자
아빠 생성자
4번 - 자전거
package sevenClass;
public class Test0509_04 {
public static void main(String[] args) {
MountainBike m = new MountainBike(200);
}
}
class Bike{
Bike(){}
Bike(int g){
this.gear = g;
}
void setGear(int g){
this.gear = g;
}
int getGear(){
return this.gear;
}
private int gear;
int speed;
}
class MountainBike extends Bike{
int seatHeight;
MountainBike(int g) {
super.setGear(g);
System.out.println(super.getGear() + " 성공");
}
}
Bike Class
매개인자가 없는, g가 있는 생성자 생성
접근불가한 변수를 지정하기 위한 setGear 메소드 생성
접근불가한 변수에 접근하기 위한 getGear 메소드 생성
MountainBike Class
Bike를 상속받는 클래스 생성
매개인자를 필요로하는 생성자 생성 - 부모 클래스에 setGear를 호출 후 get을 이용하여 가져옴
Main
MountainBike 객체 m 생성
결과
200 성공
5번 - 포인트
package sevenClass;
public class Test0509_05 {
public static void main(String[] args) {
Point p = new Point();
p.set(2, 3);
p.showPoint();
ColorPoint c = new ColorPoint();
c.set("red");
c.set(4, 5);
c.showColorPoint();
ColorPoint c2 = new ColorPoint(5, 6, "blue");
c2.showColorPoint();
}
}
class Point{
private int x, y;
void set(int x, int y) {
this.x = x;
this.y = y;
}
void showPoint() {
System.out.printf("(%d, %d)\n", this.x, this.y);
}
}
class ColorPoint extends Point{
private String color;
void set(String c) {
this.color = c;
}
ColorPoint(){}
ColorPoint(int x, int y, String c){
super.set(x, y);
this.color = c;
}
void showColorPoint() {
System.out.printf("%s\n", this.color);
showPoint();
}
}
Point Class
접근불가한 x, y변수 생성
set 메소드 생성
show 메소드 생성
ColorPoint Class
Point를 상속받는 클래스 생성
접근불가한 color 변수 생성
set 메소드 생성
매개인자가 필요한, 필요없는 생성자 생성
- 부모 클래스에 set 호출
show를 통해 부모 클래스의 요소, 클래스의 요소 출력
Main
Point p 객체 생성
p의 정보 지정
출력
ColorPoint c 객체 생성
c의 정보 지정
출력
ColorPoint c2 정보를 지정하고 객체 생성
출력
결과
(2, 3)
red
(4, 5)
blue
(5, 6)
6번 - 사람
package sevenClass;
public class Test0509_06 {
public static void main(String[] args) {
Person one = new Person();
one.setName("홍길동");
one.setAddress("은평구 통일로 92가길 33");
one.setTel("010-1234-5678");
one.showInfo();
Person two = new Person("이순신", "불광동", "010-2222-3333");
two.showInfo();
Customer three = new Customer("강감찬", "연신내", "010-3333-4444");
three.setNumber("2024-001");
three.setMiles(1234);
three.customerInfo();
}
}
class Person{
private String name;
private String address;
private String tel;
Person(){}
Person(String n, String a, String t){
this.name = n;
this.address = a;
this.tel = t;
}
void setName(String n) {
this.name = n;
}
void setAddress(String a) {
this.address = a;
}
void setTel(String t) {
this.tel = t;
}
void showInfo() {
System.out.printf("사용자의 정보는: %s %s %s 입니다.\n", this.name, this.address, this.tel);
}
}
class Customer extends Person{
private String coNumber;
private int mileage;
Customer(){}
Customer(String n, String a, String t){
super.setName(n);
super.setAddress(a);
super.setTel(t);
}
void setNumber(String c) {
this.coNumber = c;
}
void setMiles(int m) {
this.mileage = m;
}
void customerInfo() {
System.out.printf("사용자의 정보: %s %s\n", this.coNumber, this.mileage);
super.showInfo();
}
}
Person Class
접근불가한 name, address, tel 변수 생성
매개인자가 필요한, 필요없는 생성자 생성
set 메소드 생성
show 메소드 생성
Customer Class
Person을 상속받는 클래스 생성
접근불가한 coNumber, mileage 변수 생성
매개인자가 필요한, 필요없는 생성자 생성
set 메소드 생성
customerInfo 메소드 생성
Main
Person 객체 one 생성
정보 지정
정보 출력
미리 정보를 지정한 Person 객체 two 생성
정보 출력
미리 Person 정보를 지정한 Customer 객체 three 생성
Customer 정보 지정
전체 정보 출력
결과
사용자의 정보는: 홍길동 은평구 통일로 92가길 33 010-1234-5678 입니다.
사용자의 정보는: 이순신 불광동 010-2222-3333 입니다.
사용자의 정보: 2024-001 1234
사용자의 정보는: 강감찬 연신내 010-3333-4444 입니다.
JAVA-0502
상속상속이란? - 기존에 있는 클래스의 맴버 변수나 메소드를 물려받아 새로운 클래스를 만드는 것 - 상속을 통하여 객체지향 프로그램의 주요 특징인 코드의 재사용성을 구현할 수 있다. 상
conewbie.tistory.com
'자바' 카테고리의 다른 글
JAVA - 0516 (0) | 2024.05.16 |
---|---|
JAVA-0502 (0) | 2024.05.02 |
JAVA - 0422 (0) | 2024.04.22 |
JAVA - 0415 (0) | 2024.04.15 |
JAVA - 0411 (0) | 2024.04.11 |