๋ฐ์ํ
์ค์ต) get,set ๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ ๋ฐ์ ๋ด์ฉ์ฒ๋ผ ์ถ๋ ฅ๋๊ฒ ํ์์ค
์กฐ๊ฑด : ๋ฉ์๋ ์ฌ์ ์, ArrayList ์ฌ์ฉํด์ ํ์ ์ ๋ ฅ |
โ
<๋ด๊ฐ ํ ๊ฒ>
โ
MemberData.java
package quiz;
public class MemberData {
private String memberID;
private String name;
private String tel;
private String gender;
public MemberData(String memberID, String name, String tel, String gender) {
this.memberID = memberID;
this.name = name;
this.tel = tel;
this.gender = gender;
}
public MemberData() {
}
public String getMemberID() {
return memberID;
}
public void setMemberID(String memberID) {
this.memberID = memberID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String showInfo() {
return null;
}
}
MemberDataGrade.java
package quiz;
public class MemberDataGrade {
public static void main(String[] args) {
MemberData member = new MemberData("test123", "ํ๊ธธ๋", "010-111-1111", "๋จ์");
System.out.println(member.getName() + "ํ์ ์ ๋ณด:");
System.out.println(
"์์ด๋:" + member.getMemberID() + ", ์ฑ๋ณ:" + member.getGender()
+ ", ์ ํ๋ฒํธ:" + member.getTel());
}
}
<๊ฒฐ๊ณผ๊ฐ>
-showInfo() ๋ฉ์๋์ ๋ฆฌํด๊ฐ์ด ์๋ค๋ ๋ป์ด ๋ญ์ง ๋ชฐ๋ผ์ null ๊ฐ์ผ๋ก ์ ์ํ๋ค.
ํ์ง๋ง ๊ทธ๊ฒ ์๋์๋ค...ใ ใ
๋๋ณด๊ธฐ
<์ ์๋์ด ํ ๊ฒ> + grade ์ถ๊ฐโ
MemberData.java
package quiz;
public class MemberData {
private String memberID;
private String name;
private String tel;
private String gender;
public MemberData(String memberID, String name, String tel, String gender) {
this.memberID = memberID;
this.name = name;
this.tel = tel;
this.gender = gender;
}
public MemberData() {
}
public String getMemberID() {
return memberID;
}
public void setMemberID(String memberID) {
this.memberID = memberID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
void showInfo() {
System.out.println(name + "ํ์ ์ ๋ณด :");
System.out.println("์์ด๋:" + memberID + ", ์ฑ๋ณ:" + gender + ", ์ ํ๋ฒํธ:" + tel);
}
}
MemberDataGrade.java
package quiz;
public class MemberDataGrade extends MemberData {
String grade;
public MemberDataGrade(String memberID, String name, String tel, String gender, String grade) {
super(memberID, name, tel, gender); // ์์ ํด๋์ค
this.grade = grade;
}
@Override
void showInfo() {
super.showInfo();
System.out.println("๋ฑ๊ธ:" + grade);
System.out.println(getName() + "ํ์ ์ ๋ณด:");
System.out.println("์์ด๋:" + getMemberID() + ", ์ฑ๋ณ:" + getGender()
+ ", ์ ํ๋ฒํธ:" + getTel() + ", ๋ฑ๊ธ:" + grade);
}
}
MemberDataTest.java
package quiz;
import java.util.ArrayList;
public class MemberDataTest {
public static void main(String[] args) {
ArrayList<MemberData> member = new ArrayList<MemberData>();
member.add(new MemberData("test123", "ํ๊ธธ๋", "๋จ์", "010-111-1111"));
member.add(new MemberData("test456", "์ ์ฌ์๋น", "์ฌ์", "010-222-2222"));
member.add(new MemberDataGrade("test789", "์ด์์ ", "๋จ์", "010-1233-1000", "Gold"));
for (int i = 0; i < member.size(); i++) {
member.get(i).showInfo();
System.out.println();
}
}
}
โ<๊ฒฐ๊ณผ๊ฐ>
โ์์ ํด๋์ค ๋ฉ์๋ ํธ์ถํ๊ธฐ,
๋ฉ์๋ ์ฌ์ ์ํ๊ธฐ,
ArrayList๋ก ์ธ์คํด์ค ์ถ๊ฐํ๊ธฐ ๋ฑ๋ฑ์ ๋ณต์ตํ๋ค.
๋ฐ์ํ
'Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java-๊ธฐ์ด] ์ค์ต (ํฉ๊ณ, ํ๊ท , ์์ฐจ ์ฐ์ถ ํ๋ก๊ทธ๋จ) (0) | 2021.02.24 |
---|---|
[Java-๊ธฐ์ด] ์ค์ต (์ฃผ๋ฌธ๋ฒํธ, ์ํ์ด๋ฆ, ์ํ๊ฐ๊ฒฉ, ๊ฐฏ์) (0) | 2021.02.24 |
[Java-๊ธฐ์ด] ์ค์ต (๋ฉ์๋๋ก ๊ตฌ๊ตฌ๋จ ํธ์ถํ๊ธฐ) (0) | 2021.02.24 |
[Java-๊ธฐ์ด] ์ค์ต (๋ก๋ ์ถ์ฒจ ๋ฒํธ) (0) | 2021.02.24 |
[Java-๊ธฐ์ด] ์๊ณ ๋ฆฌ์ฆ ์ค์ต (๋ง๋ฐฉ์ง) (0) | 2021.02.24 |
๋๊ธ