[Java]Class 예제코드

수경·2023년 8월 7일
0
post-thumbnail

Main, Person

Main

package com.test.java.obj;

public class Ex40_Class {
	
	public static void main(String[] args) {
		
		//Person 클래스
		
		Person hong = new Person();
		
		hong.setName("홍길동");
		hong.setAge(20);
		hong.setGender("남자");
		
		Person father = new Person();
		
		father.setName("홍아빠");
		father.setAge(50);
		father.setGender("남자");
		
		Person mother = new Person();
		
		mother.setName("최엄마");
		mother.setAge(48);
		mother.setGender("여자");
		
		
		hong.setFather(father);
		hong.setMother(mother);
		
		System.out.println(hong.info());
		
		//java.lang.NullPointerException
		//*******에러 랭킹 1위(빈도수)
		// - 변수가 메소드의 주소값을 가지고 있지않음 혹은 변수가 null인 상황
//		System.out.println(father.info());
	}
}

Person

package com.test.java.obj;

public class Person {

	private String name;
	private int age;
	private String gender;
	
	private Person father;
	private Person mother;
	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public Person getFather() {
		return father;
	}
	public void setFather(Person father) {
		this.father = father;
	}
	public Person getMother() {
		return mother;
	}
	public void setMother(Person mother) {
		this.mother = mother;
	}
	
	public String info() {
		
		String temp = "";
		
		temp += "아빠: " + this.father.getName() + "\n";
		temp += "엄마: " + this.mother.getName() + "\n";
		temp += "본인: " + this.name + "\n";
		
		return temp;
		
	}
}

Main, Pencil, Pencilcase

Main

package com.test.java.obj;

public class Ex41_Class {

	public static void main(String[] args) {
		
		//Pencil, PencilCase
		
		PencilCase pcase = new PencilCase();
		pcase.setColor("파란색");
		
		for (int i=0; i<5; i++) {
			Pencil p = new Pencil();
			p.setHardness("B");
			p.setColor("하늘색");
			pcase.add(p);
			
		}
		System.out.println(pcase.info());
		
		Pencil p2 = pcase.get(2);
		
		System.out.println(pcase.info());
	}
}

Pencil

package com.test.java.obj;

public class Pencil {
	
	private String hardness;	//hb, h, b, h2...
	private String color;
	
	
	public String getHardness() {
		return hardness;
	}
	
	public void setHardness(String hardness) {
		this.hardness = hardness;
	}
	
	public String getColor() {
		return color;
	}
	
	public void setColor(String color) {
		this.color = color;
	}
	
	public String info() {
		return String.format("Pencil[%s, %s]", this.hardness, this.color);
	}
}

PencilCase

package com.test.java.obj;

import java.util.Arrays;

public class PencilCase {

	private String color;
	private Pencil[] pencil = new Pencil[5];	//******
	private int index =0;
	
	
	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

//	public Pencil[] getPencil() {
//		return pencil;
//	}
//
//	public void setPencil(Pencil[] pencil) {
//		this.pencil = pencil;
//	}
	
	//연필을 필통안에 넣기(*****)
	public void add(Pencil p) {
		
		if (this.index < this.pencil.length) {
			this.pencil[this.index] = p;
			this.index++;
			
		} else {
			System.out.println("필통에 연필이 꽉 찼습니다.");
		}
	}
	
	//필통에서 연필을 꺼내기
	public Pencil get(int index) {
		
		Pencil p = this.pencil[index];
		
		//Left Shift
		for (int i=index; i<this.pencil.length-1; i++) {
			this.pencil[i] = this.pencil[i+1];
			
		}
		
		//마지막 공간 비우기
		this.pencil[this.pencil.length-1] = null;
		
		//인덱스 
		this.index--;
		
		return p;
		
	}
	
	public String info() {
		return Arrays.toString(this.pencil);
	}
}
profile
웹백엔드개발자를 꿈꾸는

0개의 댓글