할 수 있다! 백준 &문제 풀기
(문제 풀이)get(), 필드, 생성자, 메소드, 클래스
오래걸려도괜찮아
2021. 12. 15. 21:27
아래 실행 결과와 같이 출력하는 다음 main()을 가진 Song 클래스를 작성하라. Song 클래스는 노래 제목 title필드, 생성자, getTitle()메소드로 구현된다.
1. 메인 메소드
public class Main {
public static void main(String[] args) {
Song mySong= new Song("Nessun Dorma");
Song yourSong= new Song("공주는 잠 못 이루고 ");
System.out.println("내 노래는 "+mySong.getTitle());
System.out.println("너 노래는" +yourSong.getTitle());
}
}
2. Song 클래스
class Song
private String title;
public Song(String title) {
this.title=title; }
public String getTitle(){
return title;}
3. 최종
class Song{
private String title;
public Song(string title){
this.title=title ;}
public getTitle(){
ruturn title;}
public class Main {
public static void main(String[] args) {
Song mySong= new Song("Nessun Dorma");
Song yourSong= new Song("공주는 잠 못 이루고 ");
System.out.println("내 노래는 "+mySong.getTitle());
System.out.println("너 노래는" +yourSong.getTitle());
}
}