티스토리 뷰
1. Song
-생성자 클래스
package practice;
public class Song {
private String title;
private String artist;
private int year;
private String nation;
public Song(){ }
public Song(String title, String artist, int year, String nation){
this.title = title;
this.artist = artist;
this.year =year;
this.nation = nation;
}
public void show() {
System.out.println(year+"년 "+nation+" 국적의 "+ artist+ "가 부른 "+title);
}
}
-메인 함수 클래스
package practice;
public class SongTest {
public static void main(String[] args) {
Song s1 = new Song("Dancing Queen", "ABBA", 1978, "덴마크");
s1.show();
}
}
2. ColorTV
-부모 클래스 :TV
package practice;
public class TV {
private int size;
public TV(int size) {
this.size = size;
}
protected int getSize(){
return size;
} //자식 클래스까지 허용
public TV(){}
void printProperty() {
System.out.print(size);
}
}
└부모 클래스에 기본 생성자 생성해야 상속 오류 발생하지 않음.
-자식 클래스 : colorTV
package practice;
public class ColorTV extends TV { //부모 클래스에 기본 상속자가 없어서 그랬던 것임.
int color;
public ColorTV(){}
//생성자 안 만드냐?
public ColorTV(int size, int color) {
super(size);
this.color = color;
}
public void printProperty() {
System.out.println(getSize() + "인치"+color+"화소");
}
}
└생성자 생성하는 거 빼먹지마!!
-메인 함수 클래스_Test
package practice;
public class TVTest {
public static void main(String[] args) {
ColorTV myTV = new ColorTV(32, 1024);
myTV.printProperty();
}
}
.End
'수업 > └Java' 카테고리의 다른 글
| [CH02]변수와 자료형 (0) | 2022.01.30 |
|---|---|
| [CH00]Java 기초 (0) | 2022.01.29 |
| [실습문제4]클래스 (0) | 2022.01.28 |
| [Java]클래스와 생성자 복습_01.26 (0) | 2022.01.26 |
| [Java]반복문과 제어문 : for, if, continue, break (0) | 2022.01.25 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- selcetor
- caption-side
- text formatting
- initialized
- 기본선택자
- A%B
- html pre
- html input type
- Java
- 입력양식
- css
- empty-cell
- 미디어 태그
- 외부구성요소
- typeof
- 변수
- JavaScript
- improt
- html base tag
- CascadingStyleSheet
- ScriptTag
- html layout
- html
- 스크립태그
- scanner
- html a tag
- BAEKJOON
- input type 종류
- border-spacing
- html atrribute
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 | 31 |
글 보관함