Eggs Sunny Side Up
본문 바로가기
언어/JAVA

OOP_클래스와 객체, 배열

by guswn100059 2023. 3. 9.

Class

=> field & method

package 연습장;

public class Class {
	
	String name;
	String number;
	int age;
	int scoreJava;
	int scoreWeb;
	int scoreAndroid;
	
	public Class(String name, String number, int age, int scoreJava, int scoreWEb, int scoreAndroid) {
		super();
		this.name = name;
		this.number = number;
		this.age = age;
		this.scoreJava = scoreJava;
		this.scoreWeb = scoreWEb;
		this.scoreAndroid = scoreAndroid;
	}
	
	public void show() {
		System.out.println(name+"님 안녕하세요.");
		System.out.println("["+number+", "+age+"살]");
		System.out.println(name+"님의 Java점수는 "+scoreJava+"점 입니다.");
		System.out.println(name+"님의 Web점수는 "+scoreWeb+"점 입니다.");
		System.out.println(name+"님의 Android점수는 "+scoreAndroid+"점 입니다.");
		System.out.println("===============================");
		
	}
	

}

Main

=> instances

package 연습장;

public class Main {

	public static void main(String[] args) {
		
		Class student1 = new Class("홍길동", "20220614", 20, 60, 80, 55);
		student1.show();
		
		Class student2 = new Class("김갈똥", "19991112", 27, 55, 36, 85);
		student2.show();

	}

}

반복되는 전화번호부 출력

Class

=> field & method(생성자메소드, getter/setter)

package 연습장;

public class Class {

	// 필드
	// 이름, 나이, 전화번호, 주소
	private String name;
	private int age;
	private String tel;
	private String address;
	
	//생성자 메소드
	public Class(String name, int age, String tel, String address) {
		super();
		this.name = name;
		this.age = age;
		this.tel = tel;
		this.address = address;
	}
	
	//getters/setters
	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 getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

}

Main

=> instances

package 연습장;

public class Main {

	public static void main(String[] args) {

		// Class 데이터 타입을 넣는 5칸짜리 배열
		// 레퍼런스 타입이기에 null값이 출력 가능
		
		Class[] contacts = new Class[5];
		
		contacts[0] = new Class("one", 20, "010-0000-0000", "NewYork");
		contacts[1] = new Class("two", 21, "010-1111-1111", "Seoul");
		contacts[2] = new Class("three", 22, "010-2222-2222", "Busan");
		contacts[3] = new Class("four", 23, "010-3333-3333", "LA");
		contacts[4] = new Class("five", 24, "010-4444-4444", "Dajeon");

		
		for(int i = 0; i < contacts.length; i++) {
			String name = contacts[i].getName();
			int age = contacts[i].getAge();
			String tel = contacts[i].getTel();
			String address = contacts[i].getAddress();
			System.out.printf("%s\t(%d세)\t%s\t%s",name, age, tel, address);
			System.out.println();
		}
		
		
	}

}

포켓몬들 스텍 출력하기

Class

=> field & method

package 연습장;

public class Class {

	// 필드
	private String name; // 이름
	private String type; // 기술
	private int hp; // hp
	private int atk; // 공격력
	private String skill; // 스킬
	
	public Class(String name, String type, int hp, int atk, String skill) {
		super();
		this.name = name;
		this.type = type;
		this.hp = hp;
		this.atk = atk;
		this.skill = skill;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int getHp() {
		return hp;
	}

	public void setHp(int hp) {
		this.hp = hp;
	}

	public int getAtk() {
		return atk;
	}

	public void setAtk(int atk) {
		this.atk = atk;
	}

	public String getSkill() {
		return skill;
	}

	public void setSkill(String skill) {
		this.skill = skill;
	}

}

Main

=> instances

package 연습장;

public class Main {

	public static void main(String[] args) {

		// 1. 포켓몬을 담을 가방(배열) 생성하기
		// 자료형[] 배열명 = new 자료형[배열크기];
		Class[] bag = new Class[3];
		
		bag[0] = new Class("피카츄", "전기", 100, 1000, "백만볼트");
		bag[1] = new Class("꼬부기", "물", 100, 1000, "물대포");
	    bag[2] = new Class("파이리", "불", 100, 1000, "불꽃세례");
	    
	    System.out.println("===================포켓몬=================");
	    System.out.printf("\t"+"name"+"\t"+"type"+"\t"+"hp"+"\t"+"atk"+"\t"+"skill"+"\t");
	    System.out.println();
	    
	    for(int i = 0; i < bag.length; i++) {
	    	String name = bag[i].getName();
	    	String type = bag[i].getType();
	    	int hp = bag[i].getHp();
	    	int atk = bag[i].getAtk();
	    	String skill = bag[i].getSkill();
	    	
	    	System.out.printf("%d\t%s\t%s\t%d\t%d\t%s", (i+1), name, type, hp, atk, skill);
	    	System.out.println();
	    }

	}

}

댓글