카테고리 없음

국비 지원 학원 day14 java(mp3 만들기)/상속

인생진리 2022. 11. 15. 19:16

이렇게 이론을 바탕으로 오늘은 mp3플레이어를 만들어보았다

package 객체_Arraylist;

public class MP3 {

	private String title;
	private String singger;
	private String path;
	
	public MP3(String title, String singger, String path) {
		this.title = title;
		this.singger = singger;
		this.path = path;
	}

	public String getTitle() {
		return title;
	}

	public String getSingger() {
		return singger;
	}
	
	public String getpath() {
		return path;
	}

이제 까지 배운 방법을 활용하여 private 을 만듬

그후에

mp3 플레이어 기능이 들어 있는 외부 라이브러리 가져옴

package 객체_Arraylist;

import java.util.ArrayList;
import java.util.Scanner;

import javazoom.jl.player.MP3Player;

public class MP3Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		MP3Player mp3 = new MP3Player();

		MP3 music1 = new MP3("2002", "Anne Marie", ".\\music\\Anne Marie - 2002.mp3");
		MP3 music2 = new MP3("깡", "Rain", ".\\music\\Rain - 깡.mp3");
		MP3 music3 = new MP3("Ring Ding Dong", "SHINee", ".\\music\\SHINee - Ring Ding Dong.mp3");

		ArrayList<MP3> ml = new ArrayList<MP3>();

		ml.add(music1);
		ml.add(music2);
		ml.add(music3);

		// 틀 만들기
		// 1번 - 재생
		// 2번 - 정지
		// 3번 - 이전곡
		// 4번 - 다음곡
		// 5번 - 종료
		int index = 0;
		while (true) {
			System.out.print("[1]재생  [2]정지  [3]이전곡  [4]다음곡  [5]종료 >>");
			int menu = sc.nextInt();
			if (menu == 1) {
				// 재생
				// 제목 - 가수

				String title = ml.get(index).getTitle();
				String singer = ml.get(index).getSingger();
				String path = ml.get(index).getpath();

				System.out.printf("%s - %s%n", title, singer);

				mp3.play(path);
			} else if (menu == 2) {
				// 정지
				if (mp3.isPlaying() == true) {
					mp3.stop();
				}
			} else if (menu == 3) {
				// 이전곡
				if (mp3.isPlaying() == true) {
					mp3.stop();
					index--;
					if (index < 0) {
						index = ml.size() - 1;
					}
					String title = ml.get(index).getTitle();
					String singer = ml.get(index).getSingger();
					String path = ml.get(index).getpath();

					System.out.printf("%s - %s", title, singer);
					System.out.println();
					mp3.play(path);
				}
			} else if (menu == 4) {
				// 다음곡
				if (mp3.isPlaying() == true) {
					mp3.stop();
					index++;
					if (index >= ml.size()) {
						index = 0;
					}
					String title = ml.get(index).getTitle();
					String singer = ml.get(index).getSingger();
					String path = ml.get(index).getpath();

					System.out.printf("%s - %s", title, singer);
					System.out.println();
					mp3.play(path);

				}
			} else if (menu == 5) {
				// 종료
				mp3.stop();
				System.out.println("프로그램 종료");
				break;
			} else {
				System.out.println("다시 입력해 주세요");
			}

		}

	}

}

여기서 mp3.stop(); 을 작성 하지 않을 경우 노래가 중복되어서 나옴


오후에는 상속 수업을 진행함

 수업중에 String 을 Int로 강제 형변환도 배움

package 상속2;

public class 전화 {

	public void call() {
		System.out.println("전화걸기");
	};
	public void get() {
		System.out.println("전화받기");
	};
	
	
}
package 상속2;

public class 스마트폰 extends 전화 {
	public void music() {
		System.out.println("음악재생~~~(;´д`)ゞ(;´д`)ゞ");
	}

	public void internet() {
		System.out.println("인터넷하기");
	}

	public void clup() {
		System.out.println("(>人<;)ヽ(*。>Д<)o゜");
	}
}
package 상속2;

public class 갤럭시 extends 스마트폰 {

	public void 애국가() {
		System.out.println("동해 물과 백두산이 마르고 닳도록\r\n" + 
							"동해 물과 백두산이 마르고 닳도록\r\n" + 
							"하느님이 보우하사 우리나라 만세\r\n"
							+ "무궁화 삼천리 화려 강산\r\n" + 
							"대한 사람 대한으로 길이 보전하세");
	}

// 오버라이딩(재정의)
	@Override // >>주석이지만 검사 기능
	public void call() {
		System.out.println("특별한 전화걸기~");

	}

}
package 상속2;

public class 전화main {

	public static void main(String[] args) {
		전화 call =new 전화();
		스마트폰 smart = new 스마트폰();
		갤럭시 galaxy = new 갤럭시();
		call.call();
		call.get();
		System.out.println("==========================");
		smart.call();
		smart.get();
		smart.internet();
		smart.music();
		smart.clup();
		smart.clup();
		smart.music();
		smart.clup();
		smart.music();
		smart.clup();
		
		galaxy.call();
		galaxy.clup();
		galaxy.애국가();
	}

}

 

여기까지는 Ez (>人<;)(>人<;)

문제는 여기서 부터

package 상속4;

public class Honey extends Snack{
	public void yab() {
		System.out.println("허니칩을 먹다");
	}
}


package 상속4;

public class SunChip extends Snack{
	public void yab() {
		System.out.println("썬칩을 먹다");
	}
	
}



package 상속4;

public class YacheTime extends Snack{
	public void yab() {
		System.out.println("야채타임을 먹다");
	}
	public void ketchup() {
		System.out.println("케찹에 찍어 먹다");
	}
}


package 상속4;

public class Snack {
	public void yab() {
		System.out.println("과자를 먹다");
	}
}
package 상속4;

import 상속3.piri;

public class snackmain {

	public static void main(String[] args) {
		Snack s = new Snack();

		SunChip s1 = new SunChip();
		eat(s1);

		YacheTime s2 = new YacheTime();
		eat(s2);

		Honey s3 = new Honey();
		eat(s3);

	}

	public static void eat(Snack snack) {
		snack.yab();
		// 만약 먹는 과자가 야채타임 이라면
		// ---> 야채타임으로 부터 업캐스팅 된 객체라면! =>
		// 케찹을 찍어 먹고 싶어요!!
		if (snack instanceof YacheTime) {
			// 다운캐스팅을해서
			// 하위클래스명 객체명 = (하위클래스명) 업케스팅된객체명
			YacheTime ya = (YacheTime) snack;
			// 케찹 찍기
			ya.ketchup();
		}
	}

}

업케스팅을 많이 헬갈렸다 기존 상속에서 반대로 가는개념이여서 이해하기 힘들었다 

업케스팅을 사용을 하위 클래스에 꼭 상위클래스 확장을 시킨후 

메인에서 다시불러와서 사용하여야함