자바-10일차(3) 이클립스

최성현·2023년 6월 28일
0

Java

목록 보기
22/46

출력문 메서드 만들어서 메인 간략화

클래스

class Apple{
	
	private String writer;
	private String subject;
	private static String msg="Happy Day!!";
	
	//각각의 set,get
	public String getWriter() {
		return writer;
	}
	public void setWriter(String writer) {
		this.writer = writer;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public static String getMsg() {
		return msg;
	}
	public static void setMsg(String msg) {
		Apple.msg = msg;
	}
	
	
	
}

메인포함 클래스

public class ExObject_08 {
	
	//다른 클래스에서 여기 클래스로 new 생성자 없이 가져오는 법 
	
	//static은 생성안하고 그냥 출력하려고 사용
								//Apple이라는 클래스를 통째로 가져와서 apl이라하자
								//클래스명 변수이름
	public static void writeApple(Apple apl) {
		
		System.out.println("***Apple클래스 출력***");
		
		System.out.println("작성자: "+apl.getWriter());
		System.out.println("제목: "+apl.getSubject());
	}
	
	
	public static void main(String[] args) {
		
		System.out.println("Apple클래스의 메세지 출력");
		System.out.println(Apple.getMsg());
		
		//생산자로 set가져오기 때문에 new로 호출해봄
		Apple a1=new Apple();
		a1.setWriter("김은희");
		a1.setSubject("악귀 재밌더라구요");
		
		Apple a2=new Apple();
		a2.setWriter("김병건");
		a2.setSubject("초보 개밸자의 하루");
		
		
		//public static void writeApple(Apple apl)로 출력
		//writeApple 호출
		//class ExObject_08에서 출력했기 때문에 클래스명 생략 가능
		writeApple(a1);
		writeApple(a2);
		

	}

}

for문 배열 출력 문제

웹에서는 DB에서 가져오면 되서 for문 배열로 출력할 일 거의 없음

클래스

//웹에서는 DB에서 가져오면 되서 for문 배열로 출력할 일 거의 없음

class MyPersonInfo{
	
	private String name;
	private String blood;//혈액형
	private String age;
	
	//명시적 생산자
	public MyPersonInfo(String name, String blood, String age) {
		this.name=name;
		this.blood=blood;
		this.age=age;
	}
	
	//제목
	//static 변수선언 없이 사용 가능
	public static void title()
	{
		System.out.println("이름\t혈액형\t나이");
		System.out.println("-----------------------");
	}
	
	//출력문
	public void getInfo(){
		System.out.println(name+"\t"+blood+"형\t"+age+"세");
	}
}

메인클래스

public class ArrObject_10 {

	public static void main(String[] args) {
		
		
		
		//생성초기화
		MyPersonInfo arr[]= {new MyPersonInfo("최성현", "B", "27"),
							new MyPersonInfo("이민규", "O", "29"),
							new MyPersonInfo("김형준", "B", "29")};
		
		System.out.println("총: "+arr.length+"명의 정보 출력");
		//static 제목
		MyPersonInfo.title();
		
		//출력
		for(int i=0;i<arr.length;i++)
		{
			//==arr[i].getInfo();
			MyPersonInfo n=arr[i];
			n.getInfo();
		}
		
		System.out.println("-----------------------");
		
		MyPersonInfo.title();
		
		for(MyPersonInfo s:arr)
		{
			s.getInfo();
		}

	}

}
profile
백엔드 개발자로서 성장해 나가는 성현이의 블로그~

0개의 댓글