ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 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 ν΄λž˜μŠ€κ°€ κ·ΈλŒ€λ‘œ 가져와 λ‚΄μš©μ„ μΆ”κ°€ν•˜κ³  μž¬μ •μ˜ ν•΄μ„œ μ‚¬μš©ν•˜κ³  μžˆλ‹€. 

     

     

    μ˜€λ²„λ‘œλ”©κ³Ό μ˜€λ²„λΌμ΄λ”©

     

    μ˜€λ²„λ‘œλ”©μ€ ν•˜λ‚˜μ˜ λ©”μ†Œλ“œ μ΄λ¦„μœΌλ‘œ μ„œλ‘œλ‹€λ₯Έ 인자리슀트 가진 λ©”μ†Œλ“œλ₯Ό λ§Œλ“œλŠ” 방법이고, μ˜€λ²„λΌμ΄λ”©μ€ 상속관계가 μžˆμ–΄μ•Ό ν•˜λ©° λΆ€λͺ¨μ˜ ν•¨μˆ˜κ»λ°κΈ°λ₯Ό κ·ΈλŒ€λ‘œ μ‚¬μš©ν•΄μ•Ό ν•œλ‹€.

     

     

    이클립슀 도ꡬλ₯Ό μ‚¬μš©ν•΄ μ˜€λ²„λΌμ΄λ”© μ‰½κ²Œ λ§Œλ“€κΈ°

    1. μ½”λ“œμ˜μ—­μ—μ„œ 였λ₯Έμͺ½ 마우슀 - source - Override/Implement Methods...

     

    μ˜€λ²„λΌμ΄λ”©μœΌλ‘œ μƒμ„±ν•˜κ³  싢은 λ©”μ†Œλ“œλ₯Ό μ„ νƒν•œλ‹€.

     

    μ˜€λ²„λΌμ΄λ”©μ΄ μžλ™μœΌλ‘œ μƒμ„±λ˜μ—ˆλ‹€.

     

     

     

    λ‹€ν˜•μ„±

    상속 κ΄€κ³„μ—μ„œ ν•˜λ‚˜μ˜ μƒμœ„νƒ€μž… μ°Έμ‘°λ³€μˆ˜λ‘œ μ—¬λŸ¬ λ‹€λ₯Έ ν•˜μœ„νƒ€μž…μ„  μ°Έμ‘°ν•  수 μžˆλŠ” νŠΉμ„±μ΄λ‹€. μ§€κΈˆκΉŒμ§€λŠ” 객체생성할 λ•Œ λ°μ΄ν„°νƒ€μž…μ„ μ•ž λ’€ μΌμΉ˜ν•˜κ²Œ 써쀬닀. λ‹€ν˜•μ„±μ€ νƒ€μž…μ΄ 달라도 μ°Έμ‘°λ³€μˆ˜μ— μžμ‹ν…Œμ΄λΈ”μ„ 담을 수 μžˆλŠ” νŠΉμ„±μ„ λ§ν•œλ‹€. λΆ€λͺ¨ν΄λž˜μŠ€νƒ€μž…μ˜ λ³€μˆ˜μ— μžμ‹λ“€μ˜ 객체λ₯Ό 담을 수 μžˆλ‹€.

     

    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 : μΆ”μƒν΄λž˜μŠ€, μΈν„°νŽ˜μ΄μŠ€ κ³΅λΆ€ν•˜κΈ°

    λ°˜μ‘ν˜•

    λŒ“κΈ€

κ°œλ°œκ³΅λΆ€