본문 바로가기
비트 장기/JAVA

7/18 배움(함수,overloading, 생성자 class, chaining, final, static)

by woohyun22 2018. 7. 18.


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
package practice;
class Tiger {
    int a,b,c;
    Tiger(){
        System.out.println("call");
    }
    void func01() {
        System.out.println("호랑이");
    }
    void func02(int a) {
        System.out.println("코끼리");
    }
    int func03() {
        System.out.println("앵무새");
        return 1;
    }
    int func04(int a , int b) {
        System.out.print("낙타");
        return a*b;
    }
    void func05(String s) {
        System.out.println(s);
    }
}
public class practice {
    public static void main(String[] args) {
        Tiger t1 = new Tiger();
        t1.func01();
        t1.func02(3);
        t1.func03();
        System.out.println(t1.func04(100,100));
        t1.func05("호랑이");
        String s = new String("비둘기");//인수는 항상 대입연산이 나타난다. 인수전달
    }
}
 
 
cs




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
package practice;
class Apple{
    int a,b,c;
    Apple(){
        System.out.println("apple call");
    }
    void func01() {
        System.out.println("apple의 함수 1");
    }
}    
class Tiger {
    int a,b,c;
    Tiger(){
        System.out.println("tiger call");
    }
    void func01() {
        System.out.println("호랑이");
    }
    void func02(int a) {
        System.out.println("코끼리");
    }
    int func03() {
        System.out.println("앵무새");
        return 1;
    }
    int func04(int a , int b) {
        System.out.print("낙타");
        return a*b;
    }
    void func05(String s) {
        System.out.println(s);
    }
    void func06(Apple aa) {// cn a = new cn class name
        System.out.println("소나무");
        func01();
        aa.func01();//Apple class의 함수를 가져온다.
    }
}
public class practice {
    public static void main(String[] args) {
        Apple a1 = new Apple();//객체 
        Tiger t1 = new Tiger();
        t1.func01();
        t1.func02(3);
        t1.func03();
        System.out.println(t1.func04(100,100));
        t1.func05("호랑이");
        String s = new String("비둘기");//인수는 항상 대입연산이 나타난다. 인수전달 객체s
        /*String s = new int();*/
        t1.func06(a1);
        t1.func06(new Apple());
        a1.func01();
    }
}
 
 
cs


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
package practice;
class Apple{//alt shiht r
    int a,b,c;
    Apple(){
        System.out.println("call");
    }
    void func01() {
        System.out.println("맛있는 사과");
    }
}
class Banana{
    int a,b,c;
    Banana(){
        System.out.println("call");
    }
    void func01() {
        System.out.println("맛있는 바나나");
    }
}
class Orange{
    int a,b,c;
    Orange(){
        System.out.println("call");
    }
    void func01() {
        System.out.println("맛있는 오렌지");
    }
}
class Tiger{
    int a,b,c;
    Tiger(){
        System.out.println("call");
    }
    void func01(Apple mirae) {
        System.out.println("호랑이가 ");
        mirae.func01();
        System.out.println("먹습니다");
    }
    void func02(Banana mirae) {
        System.out.println("호랑이 새끼가 ");
        mirae.func01();
        System.out.println("먹습니다");
    }
    void func03(Orange mirae) {
        System.out.println("호랑이 엄마가 ");
        mirae.func01();
        System.out.println("먹습니다");
    }
}
public class practice {
    public static void main(String[] args) { 
        Apple a = new Apple();
        Banana b = new Banana();
        Orange o = new Orange();
        Tiger t = new Tiger();
        t.func01(a);
        t.func02(b);
        t.func03(o);
    }
}
 
 
cs



package practice;
import java.util.Random;
class Apple{//alt + shiht + r
int a,b,c;
Apple(){
System.out.println("call");
}
void func01() {
System.out.println("호랑이");
}
void func02() {
System.out.println("코끼리");
}
void func03() {
System.out.println("앵무새");
}
int func04() {
int a = 100;//a는 int type
return a;
}
String func05() {
String s = "낙타";
return s;
}
Apple func06() {
System.out.println("나는 6번");
return this;//나를 리턴한다 this = Apple
}
Apple func07() {
System.out.println("나는 7번");
return this;//나를 리턴한다 this = Apple
}
Apple func08() {
System.out.println("나는 8번");
return this;//나를 리턴한다 this = Apple
}
}
public class practice {
public static void main(String[] args) {
Apple a1 = new Apple();
/*int t1 = a1.func04();
System.out.println(t1);
String t2 = a1.func05();
System.out.println(t2);*/
Apple t3 = a1.func06();
/*t3.func01();//는 Apple를 return했으므로 밖에서 다쓸수있다.
t3.func06();
a1.func06().func01();*///호출후 return값이 날아가지 직전에 .찍으면 다시쓸수있다.
//a1.func06().func07().func06().func08().func01();//.이용해서 다음함수 콜하는 것을
//chaining 체이닝이라고 한다.
(new Apple()).func01();//이 코드 이후 이 객체는 사라진다.
new Random().nextInt(100);
}
}




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
package practice;
final class Apple{
    int a,b,c;
    int n1=10;
    static int n2 = 20;//new로 생성해야 객체가 만들어진다.
                       //static을 쓰면 이 변수만 실체를 만들어버린다.
                       //static 변수는 초기에 값을 넣어줘야한다.
    final int n3 =30;  //대입 받을 수 있는게 마지막이다.
    final int month = 12;//변경불가,원주율 등 정해져있는값
    //final 붙일수 있는곳 1.변수 앞 2.class 앞 3.void 앞 /공통적으로 마지막이라고 쓰임
    final static int n4 = 40;//static final 순서 상관없음
    //더 이상 대입받을수 없고 객체를 생성시키지 않고도 사용할수 있음 둘다됨
    Apple(){
        System.out.println("call");
    }
    void func01(){
        n1=100;
        n2=200;
        System.out.println("함수 1번");
    }
    static void func02(){//바로쓸수있다.static함수 안에서는 static 변수만 쓸 수 있다.
        //n1=300;
        n2=400;
        System.out.println("함수 2번");
    }
}
public class practice {
    public static void main(String[] args) { 
        
        System.out.println(Apple.n2);
        Apple.n2=30;
        System.out.println(Apple.n2); //class이름을 이용해서 static변수는 직접 사용할 수 있다.
        Apple.func02();
        //Math m = new Math();
        Math.abs(10);//입력값을 양수,음수를 넣으면 무조건 양수가 나옴 절댓값 return해준다.
        Math.abs(-10);
        System.out.println(Math.abs(10));
        System.out.println(Math.abs(-10));
        int a = Math.abs(10);
        int b = Math.abs(-10);
        System.out.println(a);
        System.out.println(b);
        /*public static int abs(int a) { 함수 함수이름 전달인수
                return (a < 0) ? -a : a;
                 }*/
        System.out.println(Math.max(10,20));
        Apple aa = new Apple();
        //aa = 50;//값을 받지 못하는 것을 상수화 되었다고 한다 반대는 변수라고함 값을 대입받을수 있다.
        //final은 변수를 상수화 시켜준다.
        System.out.println(Apple.n4);
        //Apple.n4 = 100;//대입 못받음
    }
}
 
 
cs


728x90

'비트 장기 > JAVA' 카테고리의 다른 글

7/19 배움(상속, overriding, 다형성 )  (0) 2018.07.19
7/18 복습  (0) 2018.07.19
7/17 복습(생성자 오버로딩)  (0) 2018.07.18
7/17 배움(class, 생성자)  (0) 2018.07.17
7/16 복습  (0) 2018.07.17

댓글