카테고리 없음
오브젝트(클래스) 배열
장꾸꾸
2020. 10. 12. 15:53
16. 오브젝트(클래스) 배열
<형식> 클래스명 []object명 = new 클래스명[첨자];
object명[0] = new 클래스명(인자,인자..);
package objectArray;
//기본 생성자, 인자2개받는 생성자, getter & setter
public class User {
private String name;
private String phone;
public User() {
super();
}
public User(String name, String phone) {
super();
this.name = name;
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
package objectArray;
//오브젝트 변수
public class UserEx1 {
public static void main(String[] args) {
User ob1=new User("kim","010-1111-1111");
User ob2=new User("lee","010-2222-2222");
User ob3=new User("park","010-3333-3333");
System.out.println(ob1.getName()+"\t"+ob1.getPhone());
System.out.println(ob2.getName()+"\t"+ob2.getPhone());
System.out.println(ob3.getName()+"\t"+ob3.getPhone());
}
}
package objectArray;
//오브젝트 배열-1
public class UserEx2 {
public static void main(String[] args) {
User[] ob=new User[3];
ob[0]=new User("kim","010-1111-1111");
ob[1]=new User("lee","010-2222-2222");
ob[2]=new User("park","010-3333-3333");
for(int i=0; i<ob.length; i++) {
System.out.println(ob[i].getName()+"\t"+ob[i].getPhone());
}
}
}
package objectArray;
//오브젝트 배열-2
public class UserEx3 {
public static void main(String[] args) {
User[] ob=new User[] {new User("kim","010-1111-1111"),
new User("lee","010-2222-2222"),
new User("park","010-3333-3333")};
for(User m :ob) {
System.out.println(m.getName()+"\t"+m.getPhone());
}
}
}