๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๊ณต๋ถ€ ๊ธฐ๋ก/JAVA

์ž๊ฐํ”„ week3 ํด๋ž˜์Šค์™€ ๊ฐ์ฒด

์ž๊ฐํ”„ week3 ํด๋ž˜์Šค์™€ ๊ฐ์ฒด_์‹ค์Šต

class Dog{
	private int size;
	private String name;
	
	public void bark() {
		if(size>40) 
			System.out.println("Woof! Woof!");
		else if (size>20)
			System.out.println("Ruff! Ruff!");
		else
			System.out.println("Yip! Yip!");
	}
	
	public void run() {
		System.out.println(name + " is running");
	}
	
	public void setSize(int sz) {
		if(sz>10) size = sz;
	}
	
	public void setName(String name) {
		this.name=name;	//์ง€์—ญ๋ณ€์ˆ˜ name์€ ์ธ์Šคํ„ด์Šค๋ณ€์ˆ˜ name๊ณผ ๊ด€๊ณ„์—†์Œ. this.name์€ ์ธ์Šคํ„ด์Šค๋ณ€์ˆ˜ name์„ ๋ถˆ๋Ÿฌ์˜ด.
						//(์ธ์Šคํ„ด์Šค๋ณ€์ˆ˜ name์— ์ง€์—ญ๋ณ€์ˆ˜ name์„ ์ €์žฅํ•œ ๊ฒƒ.
	}
	
	public String getName() {
		return name;
	}
	
	public void tryToChange(int intSize) {
		System.out.println("\n--- tryToChange() start ---");
		System.out.println("  Before Assign : intSize = " + intSize);
		intSize = this.size;
		System.out.println("  After Assign : intSize = " + intSize);
		System.out.println("--- tryToChange() end ---\n");
	}
	
	public void tryToReplace (Dog otherDog) {
		System.out.println("\n--- tryToReplace() start ---");
		System.out.println("Before Assign otherDog run ");
		otherDog.run();
		
		otherDog = this;
		System.out.println("After Assign otherDog run ");
		otherDog.run();
		System.out.println("--- tryToReplace() end ---\n");
	}
	
	public Dog bornNewDog (Dog father) { // ๋ฐ˜ํ™˜ํƒ€์ž…์ด ๊ฐ์ฒด์ธ ๊ฒฝ์šฐ??
		System.out.println("\n--- bornNewDog() start ---");
		Dog babyDog = new Dog();
		String babyName = father.getName() + "_" + this.name + "_Baby";
		int babySize = 10;
		babyDog.setName(babyName);
		babyDog.setSize(babySize);
		babyDog.run();
		babyDog.bark();
		System.out.println("\n--- bornNewDog() end ---");
		return (babyDog);
	}
	
}

public class lab3_3 {

	public static void main(String[] args) {
		
		int intSize = 50;
		
		Dog d1 = new Dog();  
		Dog d2 = new Dog();
		d1.setName("Happy"); 
		d1.setSize(25); // Dog์˜ ์ธ์Šคํ„ด์Šค๋ณ€์ˆ˜ size์— 25๊ฐ€ ์ €์žฅ๋จ
		
		d1.tryToChange(intSize); 
		
		System.out.println("\n--- Result In Main ---");
		System.out.println("   intSize = " + intSize); //์ด intSize๋Š” lab3_3ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ณ€์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— 25๋Š” ์˜ํ–ฅX
		
		System.out.println("\n=======================================");
		
		d1.tryToReplace(d2);
		
		System.out.println("\n--- Result In Main ---");
		System.out.print("d1.run() =>  ");
		d1.run();
		System.out.print("d2.run() =>  ");
		d2.run(); //null is running : tryToReplace๋ฉ”์†Œ๋“œ์—์„œ๋งŒ d2๊ฐ€ d1์œผ๋กœ ๋ฐ”๋€Œ๊ณ  ์›๋ž˜์˜ d2๋Š” lab3_3 ํด๋ž˜์Šค์—์„œ ๊ทธ๋Œ€๋กœ.
		
		System.out.println("\n=======================================\n");
		
		Dog fatherDog = new Dog();
		Dog motherDog = new Dog();
		Dog puppy;
		
		fatherDog.setName("Happy"); fatherDog.setSize(50);
		motherDog.setName("Dona"); motherDog.setSize(30);
		fatherDog.run(); 
		motherDog.run();
		puppy = motherDog.bornNewDog(fatherDog);
		puppy.bark(); 
		puppy.run();

	}

}

์•„์˜ค ์ •์‹ ์—†์–ด..

 

**๋ฉ”์†Œ๋“œ ๋‚ด์—์„œ ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค ์ƒ์„ฑํ•˜๊ธฐ

 

public Dog bornNewDog (Dog father) { 

    Dog babyDog = new Dog();

    String babyName = father.getName() + "_" + this.name + "_Baby";

    int babySize = 10;

    babyDog.setName(babyName);

    babyDog.setSize(babySize);

    return (babyDog);

}

 

'๊ณต๋ถ€ ๊ธฐ๋ก > JAVA' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

week3_4 ๊ณผ์ œ  (0) 2023.09.30
230927 ์ž๊ฐํ”„ week4  (0) 2023.09.29
230925 JAVA  (0) 2023.09.25
230923 JAVA  (0) 2023.09.24
230921 JAVA  (0) 2023.09.21