728x90
반응형
💾 소스코드 파일
📃 예제 1) static
Product, ProductMain 클래스 작성
📂product 패키지
🚩 출력
p1의 제품번호(serial no)는 1
p1의 제품번호(serial no)는 2
p1의 제품번호(serial no)는 3
생산된 제품의 수는 모두 3개 입니다.
📝 Product 클래스
package product;
class Product {
static int count = 0; // 생성된 인스턴스 수를 저장하기 위한 변수
int serialNo; // 인스턴스 고유의 번호
// 기본 생성자, 생략 가능
public Product() {
++count; // 인스턴스 생성할 때마다 count 증가!
serialNo = count;
}
}
📝 ProductMain 클래스
package product;
public class ProductMain {
public static void main(String[] args) {
Product p1 = new Product();
Product p2 = new Product();
Product p3 = new Product();
System.out.println("p1의 제품번호(serial no)는 " + p1.serialNo); // 1 저장
System.out.println("p1의 제품번호(serial no)는 " + p2.serialNo); // 2 저장
System.out.println("p1의 제품번호(serial no)는 " + p3.serialNo); // 3 저장
System.out.println("생산된 제품의 수는 모두 " + Product.count + "개 입니다."); // 총 3개
}
}
📃 예제 2) static - 예제 1 활용
Student, StudentMain 클래스 작성
📂student 패키지
📝 Student 클래스 - 본인 작성
package student;
public class Student {
static int count = 0; // 총 인원 수
static int num = 0; // 학번
String getId() {
num++; // 학번이 증가하도록 하기
++count; // 호출할 때마다 총 인원 수 +1
return "22010" + num;
}
}
📝 Stduent 클래스
💬 내 코드와 다른 점?
- 생성자 사용
- 학번을 String으로 선언
package student;
public class Student {
static int count = 0; // 총 인원 수
String str_num; // 학번
// 생성자
Student() {
++count;
str_num = "22010" + count;
}
String getId() {
return str_num;
}
}
📝 StduentMain 클래스
⚡ 알아낸 점?
- println()의 괄호 안은 값이 있어야한다.
- 따라서 getID() 메소드의 반환 값이 void일 경우 오류!
- 해결책 : String으로 반환 타입을 지정해 값이 출력되게 한다.
package student;
public class StudentMain {
public static void main(String[] args) {
Student p1 = new Student();
Student p2 = new Student();
Student p3 = new Student();
// println()의 괄호 안은 값이 있어야한다.
// getID() 메소드의 반환 값이 void일 경우 오류!
// String으로 반환 타입을 지정해 값이 출력되게 한다.
System.out.println(p1.getId()); // 220101
System.out.println(p2.getId()); // 220102
System.out.println(p3.getId()); // 220103
System.out.print("전체 학생 수는 " + Student.count + "명 입니다.");
}
}
📃 예제 3) 상속
MusicInfo, MusicInfoEx, MusicMain 클래스 작성
📂 music 패키지
📝 MusicMain 클래스
⚡ 알아낸 점?
- 시, 분, 초로 이루어질 때 시간이 출력되게 할 수 있다.
- 분, 초로만 이루어질 때 시간이 출력되지 않게 할 수 있다.
package music;
public class MusicMain {
public static void main(String[] args) {
MusicInfo ms = new MusicInfo();
ms.setMusic("신호등", 232, "이무진");
System.out.println(ms.info());
MusicInfoEx ms2 = new MusicInfoEx();
ms2.setMusic("신호등", 232, "이무진", "신호등");
System.out.println(ms2.info());
}
}
📝 MusicInfo 클래스
◾ 변수
- 곡명
- 재생 시간을 초단위로 저장
- 가수 명
package music;
class MusicInfo {
String title; // 곡명
long playtime; // 재생 시간을 초단위로 저장
String artist; // 가수 명
// 기본 생성자
MusicInfo() {
this("", 0, "");
}
// 생성자 - 매개변수 3개
MusicInfo(String title, long playtime, String artist) {
this.title = title;
this.playtime = playtime;
this.artist = artist;
}
// 메소드(1) - 매개변수로 멤버변수 초기화
void setMusic(String title, long playtime, String artist) {
this.title = title;
this.playtime = playtime;
this.artist = artist;
}
// 메소드(2) - 멤버변수를 문자열로 반환
String info() {
return title + ", " + formatTime(playtime) + ", " + artist;
}
// 메소드(3) - 재생시간을 시, 분, 초로 나눠서 확인
String formatTime(long playtime) {
this.playtime = playtime;
long min, hour; // 분, 시
String time; // 시 String타입으로 저장
min = playtime / 60; // ex. min = 4000 / 60 = 66분
playtime %= 60; // ex. playtime = 4000 % 60 = 1초
hour = min / 60; // ex. hour = 66 / 60 = 1시간
min %= 60; // ex. min = 66 % 60 = 6분
time = (hour > 0) ? hour + "시간 " : ""; // ex. hour가 1이므로 1시간 출력
return time + min + "분 " + playtime + "초";
}
}
📝 MusicInfoEx 클래스
◾ 변수
- 곡명
- 재생 시간을 초단위로 저장
- 가수 명
- 수록된 앨범명
package music;
class MusicInfoEx extends MusicInfo{
String album_title; // 수록된 앨범명
// 기본 생성자
MusicInfoEx() {
}
// 생성자 - 매개변수 4개
MusicInfoEx(String title, long playtime, String artist, String album_title) {
super(title, playtime, artist);
this.album_title = album_title;
}
// 메소드(1) - 매개변수로 멤버변수 초기화
void setMusic(String title, long playtime, String artist, String album_title) {
this.title = title;
this.playtime = playtime;
this.artist = artist;
this.album_title = album_title;
}
// 메소드(2) - 멤버변수를 문자열로 저장, 메소드 오버라이딩
String info() {
return super.info() + ", " + album_title;
}
}
📃 예제 4) 상속
MemberMain, Member, SwimMember, GymMember, GofMember 클래스 작성
📂member 패키지
📝 MemberMain 클래스
package member;
public class MemberMain {
public static void main(String[] args) {
Member m1 = new SwimMember("수영");
Member m2 = new GymMember("헬스");
Member m3 = new GolfMember("골프");
System.out.println(m1.name + "의 가격 : " + m1.fee); // 수영의 가격 : 50,000
System.out.println(m2.name + "의 가격 : " + m2.fee); // 헬스의 가격 : 70,000
System.out.print(m3.name + "의 가격 : " + m3.fee); // 골프의 가격 : 100,000
}
}
📝 Member 클래스
package member;
public class Member {
String name; // 회원 이름
int id; // 회원 아이디
static int cnt; // 회원 수를 저장
protected int fee; // 회비
// 기본 생성자
Member() {
}
// 생성자 - 매개변수 1개
Member(String name) {
this.name = name; // name 초기화
this.id = 0; // id 초기화
this.cnt = 0; // cnt 초기화
}
}
📝 SwimMember 클래스
package member;
public class SwimMember extends Member{
// 생성자 - 매개변수 1개. name 초기화, fee = 50,000 초기화
SwimMember(String name) {
super(name);
super.fee = 50000; // 50,000원
}
}
📝 GymMember 클래스
package member;
public class GymMember extends Member{
// 생성자 - 매개변수 1개. name 초기화, fee = 70,000 초기화
GymMember(String name) {
super(name);
super.fee = 70000; // 70,000원
}
}
📝 GolfMember 클래스
package member;
public class GolfMember extends Member{
// 생성자 - 매개변수 1개. name 초기화, fee = 100,000 초기화
GolfMember(String name) {
super(name);
super.fee = 100000; // 100,000원
}
}
728x90
반응형
'Language > JAVA' 카테고리의 다른 글
JAVA 9일차 - Abstract(추상화), Interface(인터페이스) (0) | 2022.06.30 |
---|---|
JAVA 8일차 - 다형성 (0) | 2022.06.29 |
JAVA 7일차 - 접근 제어자, 다형성 (0) | 2022.06.28 |
JAVA 6일차 - 오버로딩, 오버라이딩, 상속 예제 (0) | 2022.06.27 |
JAVA 6일차 - 제어자, 추상클래스, 추상메소드 (0) | 2022.06.27 |
댓글