**์ ์๋๋ฌผ ํ๋งค ๋ฆฌ์คํธ๋ฅผ ์์ฑํ๋ ค๊ณ ํ ๋ ์๋ณ๋ฒํธ(), ์ข , ํฌ๊ธฐ, ์ฒด์ค, ์์ ๋ฑ์ ์์ฑ์ ์ด์ฉ์์๊ฒ ๋ณด์ฌ์ฃผ๋ ค๊ณ ํ๋ค. ํค๋ณด๋์์ ์์ ์์ฑ์ ์ ๋ ฅํ์ฌ ๊ฐ์ฒด๋ฅผ ์ด๊ธฐํํ๋ค. ์ด 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 length, float 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(1, 3000, "๋ด๊ณ ", 6);
trs[1] = new Airplane(2, 52000, "์์ด๋ฒ์ค", "ํ๋ฌผ");
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์ด ํจ๊ป ์ถ๋ ฅ๋๋ค.
๋๊ธ