๊ด€๋ฆฌ ๋ฉ”๋‰ด

โœ๐Ÿป๊ธฐ๋กํ•˜๋Š” ๋ธ”๋กœ๊ทธ

์ƒ์†์—์„œ ํด๋ž˜์Šค ์ƒ์„ฑ ๊ณผ์ •๊ณผ ํ˜• ๋ณ€ํ™˜ ๋ณธ๋ฌธ

Language/Java

์ƒ์†์—์„œ ํด๋ž˜์Šค ์ƒ์„ฑ ๊ณผ์ •๊ณผ ํ˜• ๋ณ€ํ™˜

์ฉ์‹œํ‚ด 2020. 11. 29. 23:07
728x90

ํ•˜์œ„ ํด๋ž˜์Šค๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๊ณผ์ •

ํ•˜์œ„ ํด๋ž˜์Šค๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ์ƒ์œ„ ํด๋ž˜์Šค๊ฐ€ ๋จผ์ € ์ƒ์„ฑ๋จ

์ƒ์œ„ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๊ฐ€ ํ˜ธ์ถœ๋˜๊ณ  ํ•˜์œ„ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๊ฐ€ ํ˜ธ์ถœ๋จ

ํ•˜์œ„ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž์—์„œ๋Š” ๋ฌด์กฐ๊ฑด ์ƒ์œ„ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๊ฐ€ ํ˜ธ์ถœ๋˜์–ด์•ผ ํ•จ

ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ์ƒ์œ„ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์ƒ์œ„ ํด๋ž˜์Šค ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•œ super()๋ฅผ ์ถ”๊ฐ€ํ•จ

super()๋กœ ํ˜ธ์ถœ๋˜๋Š” ์ƒ์„ฑ์ž๋Š” ์ƒ์œ„ ํด๋ž˜์Šค์˜ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž

๋งŒ์•ฝ ์ƒ์œ„ ํด๋ž˜์Šค์˜ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ( ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋Š” ์ƒ์„ฑ์ž๋งŒ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ )

ํ•˜์œ„ ํด๋ž˜์Šค๋Š” ๋ช…์‹œ์ ์œผ๋กœ ์ƒ์œ„ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ํ•จ

 

์ƒ์†์—์„œ ๋ฉ”๋ชจ๋ฆฌ ์ƒํƒœ

์ƒ์œ„ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋จผ์ € ์ƒ์„ฑ๋˜๊ณ , ํ•˜์œ„ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋จ

 

 

์ƒ์œ„ ํด๋ž˜์Šค๋กœ์˜ ๋ฌต์‹œ์  ํ˜• ๋ณ€ํ™˜ ( ์—… ์บ์ŠคํŒ… )

ํ•˜์œ„ ํด๋ž˜์Šค๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ์ƒ์œ„ ํด๋ž˜์Šค์˜ ํƒ€์ž…์„ ๋‚ดํฌํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ ์ƒ์œ„ ํด๋ž˜์Šค๋กœ ๋ฌต์‹œ์  ํ˜• ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅ

์ƒ์œ„ ํด๋ž˜์Šค ํ˜•์œผ๋กœ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ  ํ•˜์œ„ ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Œ

ํ•˜์ง€๋งŒ ๋ฐ˜๋Œ€๋Š” ์„ฑ๋ฆฝ๋˜์ง€ ์•Š์Œ

 

 

1. ๋ถ€๋ชจ ํด๋ž˜์Šค

package inheritance;

public class Customer {

    protected int customerID;
    protected String customerName;
    protected String customerGrade;
    int bonusPoint;
    double bonusRatio;

   /* public Customer(){
        customerGrade = "SILVER";
        bonusRatio = 0.01;
        System.out.println("Customer ์ƒ์„ฑ์ž ํ˜ธ์ถœ");
    }*/

    public Customer(int customerID, String customerName) {
        this.customerID = customerID;
        this.customerName = customerName;

    }    
}

2. ๋ถ€๋ชจ ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์€ ์ž์‹ ํด๋ž˜์Šค

package inheritance;

public class VIPCustomer extends Customer {
    double salesRatio;
    private int agentId;

    /*public VIPCustomer(){

        //super(); //์ƒ์œ„ ํด๋ž˜์Šค์˜ ๋ฉ”๋ชจ๋ฆฌ ์œ„์น˜๋ฅผ ๊ฐ€๋ฅดํ‚ด  super ์„ ์–ธ ์‹œ ์ƒ์œ„ ํด๋ž˜์Šค์˜ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๊ฐ€ ํ˜ธ์ถœ๋จ , ํ”„๋ฆฌ์ปดํŒŒ์ผ ๋‹จ๊ณ„์—์„œ ๋“ค์–ด๊ฐ€๊ฒŒ๋จ
        customerGrade = "VIP";//์ƒ์†๋ฐ›์€ customer ํด๋ž˜์Šค์˜ ๋ณ€์ˆ˜๊ฐ€ private( ์™ธ๋ถ€ ํด๋ž˜์Šค์—์„œ ์ ‘๊ทผ ๋ถˆ๊ฐ€ ) customerGrade
        //protected ๋กœ ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋‚˜ ๋ฉ”์„œ๋“œ๋Š” ๋‹ค๋ฅธ ์™ธ๋ถ€ ํด๋ž˜์Šค์—์„œ๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์ง€๋งŒ ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ๋Š” ์‚ฌ์šฉ ๊ฐ€
        bonusRatio = 0.05;

        System.out.println("VIP Customer () ์ƒ์„ฑ์ž ํ˜ธ์ถœ");
    }*/

    public VIPCustomer(int customerID, String customerName){
        super(customerID, customerName);

        customerGrade = "VIP";
        bonusRatio = 0.05;
        salesRatio = 0.1;

        System.out.println("VIPCustomer (int , String) ์ƒ์„ฑ์ž ํ˜ธ์ถœ ");


    }
}

 

package inheritance;

public class CustomerTest {
    public static void main(String[] args) {
        VIPCustomer customerKim = new VIPCustomer(1020,"๊น€์œ ");
        customerKim.

    }

}

VIPCustomer() ์ƒ์„ฑ์ž์˜ ํ˜ธ์ถœ๋กœ ์ธ์Šคํ„ด์Šค๋Š” ๋ชจ๋‘ ์ƒ์„ฑ๋˜์—ˆ์ง€๋งŒ ์ฐธ์กฐ ๋ณ€์ˆ˜์˜ ํƒ€์ž…์ด Customer์ด๋ฏ€๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋‚˜ ๋ณ€์ˆ˜๋Š” Customerํด๋ž˜์Šค์˜ ๋ณ€์ˆ˜์™€ ๋ฉ”์„œ๋“œ๋งŒ ๊ฐ€๋Šฅ

package inheritance;

public class CustomerTest {
    public static void main(String[] args) {
        Customer customerKim = new VIPCustomer(1020,"๊น€์œ ");
        
        customerKim.

    }

}

728x90
๋ฐ˜์‘ํ˜•

'Language > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[ JAVA ] ๊ธฐ๋ณธํด๋ž˜์Šค ( Object, String, Wrapper)  (0) 2020.12.13
[ JAVA ] ๋‹คํ˜•์„ฑ polymorphism  (0) 2020.12.06
[JAVA] ์˜ˆ์™ธ์ฒ˜๋ฆฌ  (0) 2020.11.22
[ JAVA ] ์ƒ์†  (0) 2020.11.15
[ JAVA ] static ์‘์šฉ - singleton pattern  (0) 2020.11.01