Ex06_상속 : 복습편

혜미·2020년 11월 30일
0

자바 복습

목록 보기
1/3

코드입력 Ex05_상속 복습편
class Sports { // 부모클래스상속
String name;
int inwon;

Sports(String name, int inwon) {
	this.name = name;
	this.inwon = inwon;
}

void display() {
	System.out.println("종목명:" + name);
	System.out.println("인원수:" + inwon);
}// display

}

class Baseball extends Sports { // 상속받는다

String name;
int inwon;
double ta;

// 생성자를 작성하지 않아도 이러한 생성자가 이미 있음 //default 생성자
Baseball(String name, int inwon, double ta) {
	super(name, inwon);
	this.name = name;
	this.inwon = inwon;
	this.ta = ta;
}

void display() {
	System.out.println("종목명:" + name);
	System.out.println("인원수:" + inwon);
	System.out.println("타율:" + ta);
}// display

}

class Football extends Sports { // 상속받는다
String name;
int inwon;
int goal;

Football(String name, int inwon, int goal) {
	super(name, inwon);
	this.name = name;
	this.inwon = inwon;
	this.goal = goal;
}

void display() {
	System.out.println("종목명:" + name);
	System.out.println("인원수:" + inwon);
	super.display();
	System.out.println("골수:" + goal);

}

}

public class Ex05_상속 {
public static void main(String[] args) {

	Baseball bb = new Baseball("야구", 9, 0.345);
	bb.display();

	Football fb = new Football("축구", 11, 6);
	fb.display();
}

}

profile
Memory is the driving force of my life.

0개의 댓글