Java programming

(22.10.17)Java ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ฐ์ฒด์ง€ํ–ฅ ์–ธ์–ด์™€ ์ƒ์†

ํ”„๋กœ๊ทธ๋ž˜๋จธ ์˜ค์›” 2022. 10. 18.

**์• ์™„๋™๋ฌผ ํŒ๋งค ๋ฆฌ์ŠคํŠธ๋ฅผ ์ž‘์„ฑํ•˜๋ ค๊ณ  ํ• ๋•Œ ์‹๋ณ„๋ฒˆํ˜ธ(), ์ข…, ํฌ๊ธฐ, ์ฒด์ค‘, ์ƒ‰์ƒ ๋“ฑ์˜ ์†์„ฑ์„ ์ด์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์ฃผ๋ ค๊ณ  ํ•œ๋‹ค. ํ‚ค๋ณด๋“œ์—์„œ ์œ„์˜ ์†์„ฑ์„ ์ž…๋ ฅํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. ์ด 3๊ฐœ์˜ ์• ์™„๋™๋ฌผ์„ ํ™”๋ฉด์— ํ‘œ์‹œํ•˜๊ณ (pet์ด๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ํ•˜๋‚˜๋งŒ ๋งŒ๋“ค๊ณ  new๋กœ ๋ถˆ๋Ÿฌ์˜ค๋ฉด 3๊ฐ€์ง€ ์ •๋ณด๋ฅผ ๋‹ด์„ ์ˆ˜ ์žˆ๋‹ค.) ์ด์šฉ์ž๊ฐ€ ๊ฒ€์ƒ‰๋ฉ”๋‰ด๋ฅผ ํ†ตํ•ด ์‹๋ณ„ ๋ฒˆํ˜ธ()๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ํ•ด๋‹น ์• ์™„๋™๋ฌผ์˜ ์ •๋ณด๋งŒ ํ™”๋ฉด์— ๋ณด์—ฌ์ค€๋‹ค.

 

Pet Class ์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
public class Pet 
{
    private int id;
    private String species;
    private float length;
    private float weight;
    private String color;
    
    public Pet() {}
    public Pet(int id, String species, float lengthfloat weight, String color) {
        this.setId(id);
        this.setSpecies(species);
        this.setLength(length);
        this.setWeight(weight);
        this.setColor(color);
    }
    public void Petlist() {
        String line = String.format("%d\t%s\t%f\t%f\t%s\n",id, species,length, weight, color);
        System.out.println(line);
    }
    @Override
    public String toString() {
        return String.format("%d\t%s\t%f\t%f\t%s\n",
                id, species,length, weight, color);
    }
    @Override
    public boolean equals(Object obj) {
        Pet other = (Pet) obj;
        if(this.id==other.id)return true;
        return false;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getSpecies() {
        return species;
    }
    public void setSpecies(String species) {
        this.species = species;
    }
    public float getLength() {
        return length;
    }
    public void setLength(float length) {
        this.length = length;
    }
    public float getWeight() {
        return weight;
    }
    public void setWeight(float weight) {
        this.weight = weight;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
}
cs

Main Class ์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.Scanner;
import com.ezen.javaoop.Pet;
 
public class Main 
{
    
    public static void main(String[] args) 
    {
        Pet[] pett = new Pet[3];
        Scanner kbd = new Scanner(System.in);
        for(int i=0;i<pett.length;i++
        {
            System.out.println("์‹๋ณ„ ๋ฒˆํ˜ธ(id):");
            int id = kbd.nextInt();
            kbd.nextLine();
            
            System.out.println("์ข…(species)");
            String species = kbd.nextLine().strip();
            
            System.out.println("ํฌ๊ธฐ(length):");
            float length = kbd.nextFloat();
            kbd.nextLine();
            
            System.out.println("๋ฌด๊ฒŒ(weight):");
            float weight = kbd.nextFloat();
            kbd.nextLine();
            
            System.out.println("์ข…(species)");
            String color = kbd.nextLine().strip();
            
            pett[i]= new Pet (id, species, length, weight, color);
        }
        for(int i =0;i<pett.length;i++
        {
            System.out.println(pett[i]);
        }
        System.out.println("์• ์™„๋™๋ฌผ์„ ์ฐพ์œผ๋ ค๋ฉด ์‹๋ณ„ ๋ฒˆํ˜ธ๋ฅผ ์ ์œผ์„ธ์š”.");
        int input = kbd.nextInt();
        //pett[0].getId()==input  ์ •์ˆ˜ ํ‘œํ˜„์‹ ๋ผ๋ฆฌ ๋น„๊ต
        Pet p =new Pet();
        p.setId(input);
        //ํ‚ค๋ณด๋“œ๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ๋œ Pet ์•„์ด๋””๋ฅผ Pet ์ธ์Šคํ„ฐ์Šค์— ํ• ๋‹นํ•œ๋‹ค.
        
        boolean found = false;
        for(int i =0;i<pett.length;i++
        {
            if(pett[i].equals(p)) {
                System.out.println(pett[i]);
                found = true;
                break;
            }
        }
        if(!found) System.err.println("๊ฒ€์ƒ‰ ์‹คํŒจ");
    }
}
cs

 

equals ์˜ค๋ฒ„๋ผ์ด๋“œ ํ• ๋•Œ (pet)ํ˜•์œผ๋กœ ๋ฐ”๊พธ๋Š” ์ด์œ 

๋ถ€๋ชจ์˜ ์ฐธ์กฐ๋ณ€์ˆ˜ ์•ˆ์œผ๋กœ ์ž์‹์˜ ์ฐธ์กฐ๋ณ€์ˆ˜๊ฐ€ ๋“ค์–ด์˜ค๋ฉด obj๋Š” ์ž์‹์˜ ๊ธฐ๋Šฅ์€ ๊ฐ–์ง€ ๋ชปํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ๋‹ค์‹œํŽซ ํ˜•์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๊ธฐ ์œ„ํ•ด์„œ ํ˜•๋ณ€ํ™˜ ์—ฐ์‚ฐ์ž๋ฅผ ์“ด๋‹ค. (์ž์‹์€ ๋ถ€๋ชจ์˜ ๊ธฐ๋Šฅ์„ ์“ธ์ˆ˜ ์žˆ์ง€๋งŒ ๋ถ€๋ชจ๋Š” ์ž์‹์˜ ๊ธฐ๋Šฅ์„ ์“ธ ์ˆ˜๊ฐ€ ์—†๋‹ค.)

 

 

p.setId(input);

//ํ‚ค๋ณด๋“œ๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ๋œ Pet ์•„์ด๋””๋ฅผ Pet ์ธ์Šคํ„ฐ์Šค์— ํ• ๋‹นํ•œ๋‹ค.

pett[i].equals(p) p๋Š” Pet class์— equals ๋ฉ”์†Œ๋“œ์— obj ํŒŒ๋ผ๋ฏธํ„ฐ ์•ˆ์œผ๋กœ ๋“ค์–ด๊ฐ„๋‹ค.

ํด๋ž˜์Šค๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ๋กœ๋“œ ์‹œํ‚ค๋ฉด ๊ฑฐ๊ธฐ์— ์žˆ๋Š” ์ •๋ณด๋“ค๋„ ๋ชจ๋‘ ํ•จ๊ป˜ ๋กœ๋“œ๋œ๋‹ค.


 

 

์ƒ์†

ํ•˜์œ„ํด๋ž˜์Šค์—์„œ ์ƒ์œ„ํด๋ž˜์Šค์˜ ์†์„ฑ๊ณผ ๊ธฐ๋Šฅ๋“ค์„ ๋Œ์—ฌ๋‹ค์“ด๋‹ค.

"is a" ๊ด€๊ณ„์ผ๋•Œ ์ƒ์†ํ•ด์•ผํ•œ๋‹ค.

์ƒ์†์˜ ์ด์ 

1.์‰ฝ๊ฒŒ ํด๋ž˜์Šค๋ฅผ ํŒŒ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

2. ์ฝ”๋“œ ์ค‘๋ณต์„ ํ”ผํ•  ์ˆ˜ ์žˆ๋‹ค.

3. ์„œ๋กœ ๋‹ค๋ฅธ ํด๋ž˜์Šค์˜ ์ž๋ฃŒํ˜•์„ ๋ถ€๋ชจ ๋ณ€์ˆ˜์— ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.(๋‹คํ˜•์„ฑ ํ™œ์šฉ๊ฐ€๋Šฅ)

*๊ธฐ๋Šฅ๊ณผ ์†์„ฑ์„ ๋ฐ›์•„์˜ค์ง€๋งŒ ์ƒ์„ฑ์ž๋Š” ๋ฐ›์•„์˜ค์ง€ ์•Š๋Š”๋‹ค.


**์šด์†กํšŒ์‚ฌ์— ์‚ฌ์šฉ๋˜๋Š” ์šด์†ก์ˆ˜๋‹จ(Transport)์—๋Š” ํŠธ๋Ÿญ, ๋น„ํ–‰๊ธฐ๋ฅผ ํ•œ ๋ชฉ๋ก์— ๋‚˜ํƒ€๋‚ด๋ผ(์ƒ์†๊ณผ ์˜ค๋ฒ„๋ผ์ด๋“œ ์˜ค๋ฒ„๋กœ๋“œ ์‚ฌ์šฉ)

๊ณ ์œ  ์†์„ฑ๋„ ๊ฐ๊ฐ ํ‘œ์‹œํ•˜๊ณ  ๊ณตํ†ต ์†์„ฑ๋„ ํ‘œ์‹œ

 

Main ์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import com.ezen.javaoop.Airplane;
import com.ezen.javaoop.Transport;
import com.ezen.javaoop.Truck;
 
public class InheritanceMain 
{
    public static void main(String[] args) 
    {
        Transport[] trs = new Transport[2];
        trs[0= new Truck(13000"๋ด‰๊ณ "6);
        trs[1= new Airplane(252000"์—์–ด๋ฒ„์Šค""ํ™”๋ฌผ");
        
        System.out.printf("%s\t%s\t%s\t%s\t%s\n",
                "๋ฒˆํ˜ธ","๊ฐ€๊ฒฉ","์ข…๋ฅ˜","์ธ์›","์ ์žฌ๋ฌผ๊ฑด");
        for(int i = 0; i<trs.length;i++) {
            trs[i].Translist();
        }
    }
}
cs

๋ถ€๋ชจ๊ฐ€ ๋˜๋Š” Transport ์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class Transport 
{
    private int num;
    private int price;
    private String kinds;
    public Transport() {}
    public Transport(int num, int price, String kinds) {
        this.setNum(num);
        this.setPrice(price);
        this.setKinds(kinds);
    }
    public void Translist() {
        String list = String.format("%s\t%d\t%s",num, price, kinds);
        System.out.print(list);
    }
    public int getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public String getKinds() {
        return kinds;
    }
    public void setKinds(String kinds) {
        this.kinds = kinds;
    }    
}
cs

์ž์‹ Truck ์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Truck extends Transport 
{
    private int people;
    public Truck() {}
    public Truck(int num, int price, String kinds, int people) {
        super(num, price, kinds);
        this.setPeople(people);
    }
    @Override
    public void Translist() {
        super.Translist();
        System.out.printf("\t%d\n", people);
    }
    public int getPeople() {
        return people;
    }
    public void setPeople(int people) {
        this.people = people;
    }    
}
cs

์ž์‹ Airplane ์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Airplane extends Transport 
{
    private String item;
    public Airplane() {}
    public Airplane(int num, int price, String kinds, String item) {
        super(num, price, kinds);
        this.setItem(item);
    }
    @Override
    public void Translist() {
        super.Translist();
        System.out.printf("\t\t%s\n", item);
    }
    public String getItem() {
        return item;
    }
    public void setItem(String item) {
        this.item = item;
    }    
}
cs

์ฝ˜์†” ๊ฐ’:


trs[i].Translist();

 

- ๊ฐ€์ƒ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ- (Virtual Method Invocation)-

๋ถ€๋ชจ ๋ณ€์ˆ˜์— ์ž์‹ ๋ณ€์ˆ˜๋ฅผ ๋„ฃ์–ด๋„ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•ด์ฃผ๋ฉด ๋ถ€๋ชจํ˜• ์ฐธ์กฐ๋กœ ์‹คํ–‰ํ•˜๋”๋ผ๋„ ์ž์‹๋Œ€์—์„œ ์˜ค๋ฒ„๋ผ์ด๋“œ๋œ ๋ฉ”์†Œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.

์ฆ‰ ๋ถ€๋ชจํ˜•์—๋Š” ์—†๋Š” ์˜ค๋ฒ„๋ผ์ด๋“œ๋œ ์ž์‹์˜ ๋ฉ”์†Œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๊ณ  ์žˆ๋‹ค.

๋ถ€๋ชจ ํด๋ž˜์Šค์—์„œ ๊ทœ์ •ํ•œ ์ข…๋ฅ˜(kinds)๊นŒ์ง€๋งŒ ๋‚˜์™€์•ผํ•˜์ง€๋งŒ ์ž์‹ ํด๋ž˜์Šค์—์„œ ์˜ค๋ฒ„๋ผ์ด๋“œํ•˜์—ฌ ์ถ”๊ฐ€ํ•œ people๊ณผ item์ด ํ•จ๊ป˜ ์ถœ๋ ฅ๋œ๋‹ค.

 

๋Œ“๊ธ€