-
JAVA μλ° μ€λ²λΌμ΄λ©, λ€νμ±JAVA 2020. 8. 6. 23:29
π― μλ° μμμ μ€λ²λΌμ΄λ©κ³Ό λ€νμ±μ μμλ³Έλ€.
μ€λ²λΌμ΄λ©
μμκ΄κ³μμ λΆλͺ¨ν΄λμ€μ λ©μλλ₯Ό μμν΄λμ€κ° μ¬μ μν΄μ μ¬μ©νλ λ°©λ²μ΄λ€. λΆλͺ¨μ λ©μλ νμμ λκ°μ΄ μ¨μΌνλ€. μ κ·Όμ§μ μλ λΆλͺ¨μ κ°κ² νκ±°λ νλ μ§μ λ§ ν μ μλ€. static, final, privateμ μ€λ²λΌμ΄λ©μ΄ λΆκ°λ₯νλ€.
class Employee{ String name; int salary; public String getEmployee() { return name +" "+ salary; } public Employee() { } public Employee(String name, int salary) { this.name = name; this.salary = salary; } } class Manager extends Employee{ String depart; @Override public String getEmployee() { return super.getEmployee()+ " "+ depart; } public Manager() { } public Manager(String name, int salary,String depart) { super(name, salary); this.depart = depart; } } public class Ex06_6 { public static void main(String[] args) { Employee emp = new Employee("νκΈΈλ", 2000); Manager man = new Manager("μ΄μμ ", 4000, "κ°λ°"); System.out.println(emp.getEmployee()); System.out.println(man.getEmployee()); } }
λΆλͺ¨ν΄λμ€μΈ Employeeμ getEmployee λ©μλλ₯Ό μμν΄λμ€μΈ Manager ν΄λμ€κ° κ·Έλλ‘ κ°μ Έμ λ΄μ©μ μΆκ°νκ³ μ¬μ μ ν΄μ μ¬μ©νκ³ μλ€.
μ€λ²λ‘λ©κ³Ό μ€λ²λΌμ΄λ©
μ€λ²λ‘λ©μ νλμ λ©μλ μ΄λ¦μΌλ‘ μλ‘λ€λ₯Έ μΈμ리μ€νΈ κ°μ§ λ©μλλ₯Ό λ§λλ λ°©λ²μ΄κ³ , μ€λ²λΌμ΄λ©μ μμκ΄κ³κ° μμ΄μΌ νλ©° λΆλͺ¨μ ν¨μκ»λ°κΈ°λ₯Ό κ·Έλλ‘ μ¬μ©ν΄μΌ νλ€.
μ΄ν΄λ¦½μ€ λꡬλ₯Ό μ¬μ©ν΄ μ€λ²λΌμ΄λ© μ½κ² λ§λ€κΈ°
λ€νμ±
μμ κ΄κ³μμ νλμ μμνμ μ°Έμ‘°λ³μλ‘ μ¬λ¬ λ€λ₯Έ νμνμ μ μ°Έμ‘°ν μ μλ νΉμ±μ΄λ€. μ§κΈκΉμ§λ κ°μ²΄μμ±ν λ λ°μ΄ν°νμ μ μ λ€ μΌμΉνκ² μ¨μ€¬λ€. λ€νμ±μ νμ μ΄ λ¬λΌλ μ°Έμ‘°λ³μμ μμν μ΄λΈμ λ΄μ μ μλ νΉμ±μ λ§νλ€. λΆλͺ¨ν΄λμ€νμ μ λ³μμ μμλ€μ κ°μ²΄λ₯Ό λ΄μ μ μλ€.
Pet c = new Cat("μΌμΉμ΄", 3, "μλ"); System.out.println(c.printInfo()); //λμ λ°μΈλ©
λΆλͺ¨νμ PetμΌλ‘ μ°Έμ‘°λ³μ cμ Cat μμκ°μ²΄λ₯Ό μ μ₯νλ€. νΈμΆλ printInfo() ν¨μλ Catμ μ 보λ₯Ό μΆλ ₯ν κΉ, Petμ μ 보λ₯Ό μΆλ ₯ν κΉ? νμ μ Petμ΄μ§λ§ μ€μ μ€λ²λΌμ΄λ© λμ΄μλ Catμ μ 보λ₯Ό μΆλ ₯νλ€. λμ λ°μΈλ©μ μ€νν λΉμμ κ°μ²΄ νμ μ κΈ°μ€μΌλ‘ μ€μ μ€νν λ©μλλ₯Ό νΈμΆνλ κ²μ λ§νλ€.
Pet c = new Cat("μΌμΉμ΄", 3, "μλ"); //λΆλͺ¨νμ λ³μμ μμκ°μ²΄ μ μ₯ κ°λ₯ System.out.println(c.printInfo()); //λΆλͺ¨νμ λ³μλ‘ μ€λ²λΌμ΄λ©λ μμ κ°μ²΄ ν¨μ νΈμΆλ¨ - λμ λ°μΈλ© // System.out.println(c.getGender()); λ κΉ? // λΆλͺ¨νμ λ³μλ‘ μ€μ μμ κ°μ²΄λ₯Ό κ°μ§κ³ μμ΄λ μμκ°μ²΄μ μ μΌν ν¨μ,λ³μ μ κ·Όμ΄ μλ¨ Cat cat = (Cat)c; // μ€μ κ°μ²΄μ μ μΌ λ³μ, ν¨μ μ κ·Όμ μν΄ μ€μ κ°μ²΄ νμ μΌλ‘ λ³ν(νμ μΊμ€ν ) System.out.println(cat.getGender(); // νλ³νλ λ³μλ‘ ν¨μ, λ³μ μ¬μ©
Cat ν΄λμ€λ genderλΌλ μ μΌν λ³μλ₯Ό κ°κ³ μλ€. λΆλͺ¨νμ μΌλ‘ μ μΈλ μ°Έμ‘°λ³μ cλ‘ μμκ°μ²΄μ μ μΌν λ³μ, ν¨μμ μ κ·Όνλ 건 λΆκ°λ₯νλ€. μ κ·ΌνκΈ° μν΄μ μ°Ύκ³ μνλ μ 보λ₯Ό κ°κ³ μλ μμκ°μ²΄ νμ μΌλ‘ λ³ννλ κ³Όμ μ΄ νμνλ€. μ΄κ²μ νμ μΊμ€ν μ΄λΌκ³ λΆλ₯Έλ€.
νμ μΊμ€ν
public class TestPet { public static void main(String[] args) { // κ³ μμ΄ 2λ§λ¦¬,κ°μμ§ 1λ§λ¦¬ ,λ¬Όκ³ κΈ° 1λ§λ¦¬ Pet c = new Cat("μΌμΉμ΄", 3, "μλ"); System.out.println(c.printInfo()); Pet d = new Dog("κ°μμ§", 10, "μλ", "κ²μ "); System.out.println(d.printInfo()); Pet f = new Fish("λΆμ΄", 10, "μλ", 2.5); System.out.println(f.printInfo()); Cat c1 = (Cat) c; System.out.println(c1.getGender()); Dog d1 = (Dog) d; System.out.println(d1.getColor()); Fish f1 = (Fish)f; System.out.println(f1.getGender()); } }
νμ μΊμ€ν μμ±νμμ `ν΄λμ€νμ λ³μλͺ = (νλ³νν΄μ€ νμ ) μλλ³μλͺ ` μ΄λ€. νμ μΊμ€ν ν c1, d1, f1μ μμ μ ν΄λμ€μ λΆλͺ¨ν΄λμ€μμ λͺ¨λ μ¬μ©κ°λ₯ν΄μ§λ€.
λ€νμ±μ μ΄μ©ν κ°μ²΄μμ± μ€μ΅
class Employee{ String name; int salary; public String getEmployee() { return name+ " "+ salary; } public Employee() {} public Employee(String name, int salary) { this.name = name; this.salary = salary; } } class Manager extends Employee{ String depart; @Override public String getEmployee() { return super.getEmployee()+ " "+ depart; } public Manager() {} public Manager(String name, int salary, String depart) { super(name, salary); this.depart=depart; } } public class Ex06_7 { public static void main(String[] args) { Employee emp = new Employee("νκΈΈλ",2000); System.out.println(emp.getEmployee()); emp = new Manager ("μ΄μμ ",4000,"κ°λ°"); System.out.println(emp.getEmployee()); } }
Manager ν΄λμ€λ₯Ό λΆλͺ¨ν΄λμ€ νμ emp λ³μμ κ°μ²΄μμ± ν emp.getEmployee() ν¨μλ₯Ό νΈμΆνλ©΄ μ€λ²λΌμ΄λ©λ λ©μλλ₯Ό μ€νν΄μ depart μ 보κΉμ§ μΆλ ₯νλ€.
instanceof λ κ°μ²΄κ° μ΄λ€ νμ μΈμ§ κ²μ¬ν λ μ¬μ©νλ ν€μλμ΄λ€. 리ν΄κ°μ΄ λ Όλ¦¬μ°μ°μ true / false μ΄λ©° μ¬μ©μ κ³μΈ΅κ΅¬μ‘° νμλΆν° λΉκ΅ν΄μΌ νλ€.
λ€νμ±μ μ΄μ©ν λ°°μ΄ κ΄λ¦¬ μ°μ΅
// Pet public class Pet { String name; int age; public Pet() {} public Pet(String name, int age) { this.name = name; this.age = age; } 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 printInfo() { System.out.println("pet printInfo()νΈμΆ=============="); return name+"\t"+age; } }
// Cat public class Cat extends Pet{ private String gender; //μ μΌμμ± public Cat(String name, int age,String gender) { super(name, age); //λΆλͺ¨μμ±μ νΈμΆ this.gender = gender; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } @Override public String printInfo() { System.out.println("cat printInfo()νΈμΆ=============="); return name+"\t"+age+"\t"+gender; } }
// Dog public class Dog extends Pet{ private String gender; //μ μΌμμ± private String color; //μ μΌμμ± public Dog(String name, int age,String gender, String color) { super(name, age); this.gender = gender; this.color = color; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String printInfo() { System.out.println("dog printInfo()νΈμΆ=============="); return name+"\t"+age+"\t"+gender+ "\t"+color; } }
// Fish public class Fish extends Pet{ private String gender; //μ μΌμμ± private double tailSize; //Fishμλ§ μ‘΄μ¬νλ μ μΌμμ± public Fish(String name, int age, String gender, double tailSize) { super(name, age); this.gender = gender; this.tailSize = tailSize; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public double getTailSize() { return tailSize; } public void setTailSize(double tailSize) { this.tailSize = tailSize; } public String printInfo() { System.out.println("fish printInfo()νΈμΆ=============="); return name+"\t"+age+"\t"+gender+ "\t"+tailSize; } }
Petμ μμνλ νμ ν΄λμ€ Cat, Dog, Fishλ₯Ό λ§λ λ€. κ°κ°μ ν΄λμ€λ μ μΌμμ±μ κ°μ‘κ³ , κ³΅ν΅ μμ±μ λΆλͺ¨ν΄λμ€μμ μμλ°λλ€. μμλ°μ λ³μμ μ μΌλ³μλ₯Ό printInfo ν¨μλ₯Ό ν΅ν΄ λͺ¨λ λ½μλ³Ό μ μλ€.
// TestPet1 public class TestPet { public Pet petPrint(Pet p) { Cat c = null; if( p instanceof Cat) { c = (Cat)p; System.out.println(c.printInfo()); System.out.println(c.getGender()); }else if(p instanceof Dog){ Dog d = (Dog) p; System.out.println(d.getColor()); }else if( p instanceof Fish) { Fish f = (Fish)p; System.out.println(f.getTailSize()); } else if( p instanceof Pet) { System.out.println(p.printInfo()); }else { System.out.println("μλͺ»λ λ°μ΄ν°"); }return c; } public static void main(String[] args) { Pet c= new Cat("μΌμΉμ΄", 3, "μλ"); Pet d= new Dog("κ°μμ§", 10, "μλ","κ²μ "); Pet f= new Fish("λΆμ΄", 10, "μλ",2.5); TestPet test= new TestPet(); Pet p = test.petPrint(c); Cat cat = (Cat)p; } }
Petνμ μ°Έμ‘°λ³μ pκ° Cat νμ μ΄λΌλ©΄ νμ μΊμ€ν νλ³νμ ν΅ν΄ μΊ£ ν΄λμ€μ μ μΌν¨μμ μ κ·Όν μ μλ€. λ§μ°¬κ°μ§λ‘ λλ¨Έμ§ ν΄λμ€λ€μ νμ κΉμ§ λͺ¨λ λΉκ΅ νμ λ§μ§λ§μΌλ‘ μμκ³μΈ΅ Petνμ μ λΉκ΅ν μ μλ€.
//TestPet2 public class TestPet { public void arrPrint(Pet[] pets) { for (Pet p : pets) { if (p instanceof Cat) { Cat c = (Cat) p; System.out.println(c.getGender()); } else if (p instanceof Dog) { Dog d = (Dog) p; System.out.println(d.getColor()); } else if (p instanceof Fish) { Fish f = (Fish) p; System.out.println(f.getTailSize()); } else if (p instanceof Pet) { System.out.println(p.printInfo()); } else { System.out.println("μλͺ»λ λ°μ΄ν°"); } } } public static void main(String[] args) { Pet c = new Cat("μΌμΉμ΄", 3, "μλ"); Pet d = new Dog("κ°μμ§", 10, "μλ", "κ²μ "); Pet f = new Fish("λΆμ΄", 10, "μλ", 2.5); Pet[] pets = new Pet[3]; pets[0] = c; pets[1] = d; pets[2] = f; TestPet test = new TestPet(); test.arrPrint(pets); for (Pet pet : pets){ System.out.println(pet.printInfo()); } } }
μ΄λ²μ λλ¬Ό κ°μ²΄λ₯Ό λ°°μ΄μ μ μ₯νκ³ arrPrint ν¨μλ₯Ό νΈμΆν΄ μΈμλ‘ λ°°μ΄μ μ λ¬νλ€. arrPrint ν¨μλ μ λ¬λ°μ λ°°μ΄μ forEach λ°λ³΅λ¬Έμ ν΅ν΄ λͺ¨λ μ΄ν΄λ³΄κ³ , instanceofλ‘ νμ μ κ²μ¬ν λ€, νμ μΊμ€ν μ κ±°μ³ κ° κ°μ²΄κ° κ°μ§ μ μΌ μμ±μ νλ¦°νΈνκ³ μλ€. mainν¨μμ λ§μ§λ§μ€ forEachν¨μλ λ°°μ΄μ pet μ΄λΌλ λ³μλ‘ μ€μ νκ³ λ°°μ΄μ λͺ¨λ μ 보λ₯Ό μΆλ ₯νλ printInfo() ν¨μλ₯Ό νΈμΆνκ³ μλ€.
π NEXT : μΆμν΄λμ€, μΈν°νμ΄μ€ 곡λΆνκΈ°
λ°μν'JAVA' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
JAVA μλ° μΈν°νμ΄μ€, λ컀νλ§ (0) 2020.08.08 JAVA μλ° μΆμν΄λμ€ abstract (0) 2020.08.07 JAVA μλ° μμ, μ κ·Όμ§μ μ (0) 2020.08.06 JAVA μλ° μ€λ²λ‘λ©, this, final, static, varags (0) 2020.08.04 JAVA μλ° ν΄λμ€ λͺ¨λν μ°μ΅νκΈ° (0) 2020.08.03