IT/JAVA

생성자

_KH_ 2025. 2. 17. 00:25
package class03;

class Animal {
	private String type;
	private String name;
	Animal(String type){
		this(type,type);
	}
	Animal(String type,String name){
		this.type=type;
		this.name=name;
	}
	void eat() {
		System.out.println("밥먹기");
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}
class Dog extends Animal {
	private int hp;
	Dog(){
		this("강아지");
	}
	Dog(String name){
		super("강아지",name);
		this.hp=100;
	}
	void play() {
		System.out.println("!@#!@#!#@!@#");
		this.hp--;
	}
	@Override
	void eat() {
		super.eat();
		this.hp++;
	}
	public int getHp() {
		return hp;
	}
	public void setHp(int hp) {
		this.hp = hp;
	}	
}
class Cat extends Animal {
	Cat(){
		super("고양이");
	}
	Cat(String name){
		super("고양이",name);
	}
	void sleep() {
		System.out.println("zzz.......");
	}
}

public class Test03 {

}​
class Point {
	int x;
	int y;
	
	Point(int x, int y) {
		// 부모에 생성자 추가하면 자식에게 에러 발생 -> 기본 생성자 때문에 생기는 에러
		this.x = x;
		this.y = y;
	}
}

class ColorPoint extends Point{
	String color; // 이미 개발된 Point를 가져오면 x,y 생성 필요 없음
	
	ColorPoint() { // 기본 생성자. 에러의 원인
		// -> 부모의 기본 생성자를 호출해야하는데 부모의 기본 생성자가 없기 떄문이다.
		super(10,20); // super로 가져와주기!
		
	}
}
package pokemon;

import java.util.Random;

class Pokemon {
	private String name;
	private String nick;
	private String type;
	private int level;
	private int exp;
	static Random random=new Random();
	Pokemon(String name,String type) {
		this(name,type,name);
	}
	Pokemon(String name,String type,String nick) {
		this.name=name;
		this.type=type;
		this.nick=nick;
		this.level=5;
		this.exp=0;
	}
	void play() {
		int randNum=random.nextInt(2);
		if(randNum<=0) {
			return;
		}
		int exp=random.nextInt(451)+50; // 획득한 경험치
		this.exp+=exp;
		
		if(this.exp>=100) {
			this.levelUp();
		}
	}
	void levelUp() {
		while(true) {
			this.exp-=100;
			this.level++;
			this.hello();
			if(this.exp<100) {
				break;
			}
		}
	}
	void hello() {
		System.out.println("울음소리");
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getNick() {
		return nick;
	}
	public void setNick(String nick) {
		this.nick = nick;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public int getLevel() {
		return level;
	}
	public void setLevel(int level) {
		this.level = level;
	}
	public int getExp() {
		return exp;
	}
	public void setExp(int exp) {
		this.exp = exp;
	}
	@Override
	public String toString() {
		return "Pokemon [name=" + name + ", nick=" + nick + ", type=" + type + ", level=" + level + ", exp=" + exp
				+ "]";
	}
}
class Pikachu extends Pokemon {
	Pikachu() {
		super("피카츄","전기");
	}
	Pikachu(String nick) {
		super("피카츄","전기",nick);
	}
	@Override
	void hello() {
		System.out.println("피카피카");
	}
	
}

public class Test {

}

'IT > JAVA' 카테고리의 다른 글

JSP  (0) 2025.02.21
능력단위평가 - 자바 총 정리 테스트 오답노트  (0) 2025.02.17
MVC 프로젝트 - 도서관 프로그램(VIEW 코드)  (1) 2025.02.11
자바 복습  (0) 2025.02.11
크롤링 + MVC 패턴  (0) 2025.02.10