์๊ฐํ 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 |