κ°μ²΄ μ§ν₯ μΈμ΄
κ°μ²΄μ§ν₯ μΈμ΄ 3λ νΉμ§ : μμμ±, λ€νμ±, μλμ±
μμμ±(Ingeritance)
λ€νμ±(Polymorphism)
μλμ±(Encapsulation) - μ κ·Ό μ νμ private μ΄μ©
**getMaxInt()λΌλ μ΄λ¦μ λ©μλλ₯Ό μ μν λ,
0~20 μ¬μ΄μ μμμ μ μλ₯Ό 5κ° μΆμΆνμ¬ λ°°μ΄μ μ μ₯νκ³ νλ©΄μ νμνλ©° κ·Έμ€ κ°μ₯ ν° μλ₯Ό 리ν΄νλ ν΄λμ€ λ©μλλ₯Ό μμ±νκ³ νΈμΆνμ¬ λ¦¬ν΄λ κ°μ νλ©΄μ νμν΄λ³΄μΈμ.
λ°°μ΄ μ΅λκ° λ½κΈ° ( λλ€ μλ‘ λ°°μ΄μ μ±μμ)
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
|
import java.util.Arrays;
import java.util.Random;
public class MethodTest
{
public static void main(String[] args)
{
System.out.println("리ν΄λ μ΅λ κ°"+ getMaxInt());
}
public static int getMaxInt() {
Random rd = new Random();
int[] intarray = new int[5];
for(int i=0 ; i<intarray.length ; i++){
intarray[i] = rd.nextInt(21);
}
System.out.println(Arrays.toString(intarray));
int max = -1;
for(int i=0 ; i<intarray.length ; i++) {
if(intarray[i]>max) {
max = intarray[i];
}
}
return max ;
}
}
|
cs |
μ½μ κ° :
[1, 7, 13, 2, 19]
리ν΄λ μ΅λ κ°19
return : λμ¨ 00 κ°μ 리ν΄ν΄λΌ
main λ©μλμμ " getMaxInt() " : getMaxInt() λ©μλμμ 리ν΄λ κ° νΈμΆ(λ°κΈ°) -> μ¦ max κ°μ ννμμ΄λ€.
getMaxInt() μ () μ λ΄μ©μ λ©μλ νλΌλ―Έν° 리μ€νΈ λΌκ³ νλ€.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import java.util.Arrays;
public class MethodTest
{
public static void main(String[] args)
{
int[] a = getMinMax(7,3);
System.out.println(Arrays.toString(a));
}
public static int[] getMinMax(int a, int b)
{ // λ©μλ νλΌλ―Έν° λ³μ
int max = a>b ? a : b;
int min = a<b ? a : b;
int[] result = new int[] {min,max};
return result;
}
}
|
cs |
μ½μ κ°: [3, 7]
getMinMax() λ©μλ μμ μλ int a, int bλ₯Ό λ©μλ νλΌλ―Έν° λ³μλΌκ³ νλ€.
λ°°μ΄ μ€λ¦μ°¨μ μ λ ¬
**0~20κΉμ§ 무μμ μ μ 10κ°λ₯Ό μΆμΆνμ¬ λ°°μ΄μ μ μ₯νλ€. μμ±λ λ°°μ΄μ λ©μλμ νλΌλ―Έν°μ μ λ¬νλ€. λ°°μ΄μ νλΌλ―Έν°λ‘ λ°μ λ©μλλ κ·Έ λ°°μ΄μ μμλ₯Ό ν¬κΈ° μμΌλ‘ μ λ ¬νμ¬ λ€μ 리ν΄νλ€. μμ±λ λ©μλλ₯Ό νΈμΆνμ¬ λ¦¬ν΄λ λ°°μ΄μ νμνμ λ μ€λ¦μ°¨μμΌλ‘ μ λ ¬λ λ°μ΄ν°λ₯Ό νμΈνλ€.
μ νμ λ ¬(Selection sort)
μμ νλμ©μ μ νμ¬ μλ°°μ΄ μμμ λΉκ΅νμ¬ μ‘°κ±΄κ³Ό λ§λ€λ©΄ λ§λ°κΎΌλ€.
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
|
import java.util.Arrays;
import java.util.Random;
public class MethodTest
{
public static void main(String[] args)
{
Random rd = new Random();
int[] a = new int[10];
for(int i =0;i<a.length;i++) {
a[i] = rd.nextInt(21);
}
int[] b = selectionsort(a);
System.out.println(Arrays.toString(b));
}
public static int[] selectionsort(int[] a)
{
int tmp = 0;
for(int sel =0;sel<a.length-1;sel++) {
for(int comp =sel+1;comp<a.length;comp++) {
if(a[sel] > a[comp]) {
tmp = a[sel];
a[sel]=a[comp];
a[comp]= tmp;
}
}
}
return a;
}
}
|
cs |
μ½μ κ°: [1, 2, 3, 6, 9, 10, 12, 17, 20, 20]
swap algorithm
λ¨Όμ λΉμ΄μλ κ³΅κ° νλλ₯Ό μ μΈν΄μ£Όκ³ int tmp = 0;
μ¬κΈ°μ κ°μ μ μ₯ν΄μ£Όκ³ λ€μ κ°μ λΉμ΄μλ κ³³μ μ μ₯ν΄μ€μ μλ€.
**10κ° μμλ₯Ό κ°λλ°°μ΄μ μ λ¬ λ°μμ, λ°°μ΄μ ν¬ν¨λ 1~20κΉμ§ λλ€ μ μλ₯Ό λͺ¨λ ν©νμ¬ κ·Έ κ²°κ³Όλ₯Ό 리ν΄νλ λ©μλ μμ±νκ³ μμ±λ λ©μλλ₯Ό νΈμΆνμ¬ κ·Έ 리ν΄κ°μ νμΈνλ€.
λ°°μ΄μ μμ±νλ λ©μλμ λ°°μ΄μ ν©μ ꡬνλ λ©μλλ₯Ό λ°λ‘ λ§λ€μ΄μ νν
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
|
import java.util.Arrays;
import java.util.Random;
public class MethodTest
{
public static void main(String[] args)
{
int[] a = createArray();
int sum =getsumOfArray(createArray());
System.out.println(Arrays.toString(a));
System.out.println(sum);
}
static int[] createArray() {
int[] arr = new int[10];
Random rd = new Random();
for(int i=0;i<arr.length;i++) {
arr[i] = rd.nextInt(20)+1;
}
return arr;
}
static int getsumOfArray(int[] a) {
int sum = 0;
for(int i =0;i<a.length;i++) {
sum += a[i];
}
return sum;
}
}
|
cs |
μ½μ κ°:
[16, 16, 1, 19, 4, 16, 3, 8, 20, 17]
132
λ μ μλ₯Ό λ°μμ κ·Έ κ²°κ³Όκ°μ λ§μ μ λ¬Έμμ΄λ‘ 리ν΄νλ λ©μλλ₯Ό μμ±νμ¬ λ¦¬ν΄ κ°μ νμνμμ€
1
2
3
4
5
6
7
8
9
10
11
12
|
public class MethodTest
{
public static void main(String[] args)
{
String l = sum(5, 6);
System.out.println(l);
}
static String sum(int x, int y) {
String z = "λ§μ
μ κ²°κ³Όλ "+(x+y)+"μ΄κ³ λ μλ "+x+", "+y;
return z;
}
}
|
cs |
μ½μ κ° : λ§μ μ κ²°κ³Όλ 11μ΄κ³ λ μλ 5, 6
κ°μ²΄μ§ν₯ μΈμ΄λ μ 보μ κΈ°λ₯λ€μ΄ ν΄λμ€ μμ μμ΄μΌνλ€.
ν΄λμ€λ
μ€λΈμ νΈ ν΄λμ€λ₯Ό μμλ°μμ Dog ν΄λμ€κ° λλ€.
μλ‘μ΄ Dogν΄λμ€λ μλ°μμ κΈ°λ³ΈμΌλ‘ μ 곡ν΄μ£Όλ μ€λΈμ νΈ ν΄λμ€λ₯Ό μμνκΈ° λλ¬Έμ μ€λΈμ νΈ ν΄λμ€μ κΈ°λ₯μ κΈ°λ³Έμ μΌλ‘ μ¬μ©ν μ μλ€.
Dog - age, species, size, color
Dog λ₯Ό ν΄λμ€ κ΄μ μΌλ‘ 보면
ν΄λμ€ - λ³μ μ΄κ³
Dog λ₯Ό κ°μ²΄ κ΄μ μΌλ‘ 보면
κ°μ²΄ - μμ± μ΄λ€.
ν¨ν€μ§κ° λ€λ₯Έ κ³³μμ μ 보λ₯Ό λΆλ¬μ€κΈ° μν΄μ μν¬νΈλ₯Ό ν΄μ€μΌνλ€.
μ€μ κ°μ²΄ μ μΆμν μ μμ±μ
μ λ ₯λ μ λ³΄κ° μ΄λλλ μμλ₯Ό λμν νμλ€.
λ¨Όμ javatest ν¨ν€μ§μ μλ λ©μΈ ν΄λμ€μμ 1λ² κ°μ΄ μ 보λ₯Ό μ£Όλ©΄ νμ Dogκ°μ²΄κ° λ‘λλλ€.
2λ²μ νλΌλ―Έν° λ³μλ‘ λ€μ΄μ€κ² λκ³ 3λ²μΌλ‘ μΈμ€ν΄μ€ λ³μμ λ΄μ©μμ νκ³ (4) κ·Έ κ°μ μΈμ€ν΄μ€ λ³μ(5)μ μ ν΄μ§ κ°μ λ€μ 6λ²μΌλ‘ λμκ°μ λμ€κ² λλ€.
Dog() <-κΈ°λ³Έ μμ±μ
Dog(int age,String species,float size, String color) <-νλΌλ―Έν°λ₯Ό κ°μ§ μμ±μ
^Arguments
μμ±μ(ν΄λμ€λͺ κ³Ό λμΌνκ² μ μΈ)λ λ¦¬ν΄ νμ μ΄ μμ΄μΌνμ§λ§ λ©μλ(κΈ°λ₯μ μμ μλ λ©μλλͺ )λΌλ©΄ λ¦¬ν΄ νμ μ΄ μμ΄μΌνλ€.
νλ μμκ°μ΄ νλ¦°νΈ κΈ°λ₯μ λ©μλλ₯Ό λ§λ€μ΄ μ£Όκ³
λ©μΈ λ©μλμμ μ½κ² νλ¦°νΈ ν μ μλ€.
**κ²μνμ νλ©΄μ νμνκΈ° μν΄ , κΈμ λͺ©, λ μ§, μμ±μ, κΈλ΄μ©, ννΈμ, 4κ°μ§ νλͺ©μ΄ λν λλ 5κ°μ κΈμ board κ°μ²΄λ₯Ό μ΄μ©νμ¬ μ μ₯νκ³ λͺ©λ‘μ νλ©΄μ νμνμμ€.
javatest ν¨ν€μ§μ Main class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package com.ezen.javatest;
import com.ezen.javaoop.Dog;
public class Main
{
public static void main(String[] args)
{
Dog[] dogs = new Dog[5];
dogs[0] = new Dog("μ½λ©","22.01.03","smith", 12);
dogs[1] = new Dog("νκΆλ","22.01.12", "jun", 45);
dogs[2] = new Dog("μ리λ²","22.04.01", "Choi", 0);
dogs[3] = new Dog("μλ° μ½λ©","22.10.12", "May", 34561654);
dogs[4] = new Dog("μ°μ λΉλ²","22.05.03", "Bob", 8465);
System.out.println("κΈμ λͺ©\t"+"λ μ§\t\t"+"μμ±μ\t"+"ννΈμ");
for(int i =0;i<dogs.length;i++) {
dogs[i].board();
}
}
}
|
cs |
javaoop ν¨ν€μ§μ Dog class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package com.ezen.javaoop;
public class Dog
{
public String title;
public String date;
public String writer;
public int hit;
public Dog(String title,String date ,String writer ,int hit)
{
this.title = title;
this.date = date;
this.writer = writer;
this.hit = hit;
}
public void board() {
System.out.printf("%s\t%s\t%s\t%d \n", title,date,writer, hit);
}
}
|
cs |
μ½μ κ°:
λκΈ