JAVA

[자바/JAVA] 프로그래밍 - 클래스(Class)

21종 2023. 6. 5. 18:52
예제) 아래의 클래스 다이어그램을 보고 클래스를 작성하세요.


Product Class

package com.kh.chap03_class.model.vo;

public class Product {
	// 클래스 선언 구문에 작성가능한 접근제한자 (public, default)
	// default 라고 쓰는게 아니고 접근제한자를 지우면 default가 되는거임
	// default로 하면 같은 패키지 내에서만 사용가능 / 다른 패키지에서는 해당 파일 사용 불가 => 못찾음
	
	
	/*
	 * * 필드(field)
	 * 
	 * 필드 == 멤버변수 == 인스턴스 변수
	 * 
	 * [표현법]
	 * 접근제한자 [예약어] 자료형 변수명;
	 */

	private String pName;
	private int price;
	private String brand;
	
	/*
	 * * 생성자
	 * - 객체를 생성하기 위한 일종의 메소드
	 * 
	 * [표현법]
	 * 접근제한자 클래스명([매개변수, 매개변수, ...]){
	 * 
	 * 
	 * }
	 */
	
	// 기본생성자
	public Product(){
		
	}
	
	/*
	 * * 메소드
	 * - 기능을 처리하는 담당
	 * 
	 * [표현법]
	 * 접근제한자 [예약어] 반환형 메소드명([매개변수]){
	 *	// 기능구현
	 * }
	 * 
	 */
	
	public void setpName(String pName) {
		this.pName = pName;
	}
	
	public void setPrice(int price) {
		this.price = price;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}
	
	public String getpName() {
		return pName;
	}
	
	public int getPrice() {
		return price;
	}
	
	public String getBrand() {
		return brand;
	}
	
	// 내가 생각할 때 유용할 것 같은 메소드도 자유롭게 생성 가능!!
	
	// 모든 필드값들 다 합친 한 문자열을 반환해주는 메소드
	public String information() {
//		return pName, price, brand; // return 하나만 됨. 그리고 반환형을 정할 수가 없음...
		return "pName : " + pName + ", price : " + price + ", brand : " + brand;
	}
}

실행 Class

package com.kh.chap03_class.run;

import com.kh.chap03_class.model.vo.Person;
import com.kh.chap03_class.model.vo.Product;

public class ClassRun {
	public static void main(String[] args) {
		
		Product iPhone = new Product();
		iPhone.setpName("iPhone13");
		iPhone.setPrice(800000);
		iPhone.setBrand("apple");
		
		Product galaxy = new Product();
		galaxy.setpName("note20");
		galaxy.setPrice(500000);
		galaxy.setBrand("samsung");
		
		Product vega = new Product();
		vega.setpName("vegaRacer");
		vega.setPrice(200000);
		vega.setBrand("pantech");
		
		System.out.println("===상품1===");
		System.out.println("상품명 : " + iPhone.getpName());
		System.out.println("가격 : " + iPhone.getPrice() + "원");
		System.out.println("브랜드 : " + iPhone.getBrand());
		
		System.out.println("\n===상품2===");
		System.out.println("상품명 : " + galaxy.getpName());
		System.out.println("가격 : " + galaxy.getPrice() + "원");
		System.out.println("브랜드 : " + galaxy.getBrand());
		
		System.out.println("\n===상품3===");
		System.out.println("상품명 : " + vega.getpName());
		System.out.println("가격 : " + vega.getPrice() + "원");
		System.out.println("브랜드 : " + vega.getBrand());
		
		System.out.println();
		System.out.println("=================");
		System.out.println();
		
		// pName : xxx, price : xxx, brand : xxx로 출력하고싶다
		System.out.println("pName : " + iPhone.getpName() + ", price : " + iPhone.getPrice() + ", brand : " + iPhone.getBrand());
		System.out.println("pName : " + galaxy.getpName() + ", price : " + galaxy.getPrice() + ", brand : " + galaxy.getBrand());
		System.out.println("pName : " + vega.getpName() + ", price : " + vega.getPrice() + ", brand : " + vega.getBrand());
	
		System.out.println("=== 메소드 만들고 난 후 ===");
		System.out.println(iPhone.information());
		System.out.println(galaxy.information());
		System.out.println(vega.information());
	}

}

컴파일