본문 바로가기
비트 단기/java

java class 2 17~24

by woohyun22 2018. 11. 28.

17번

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
package Pack01;
import java.util.concurrent.SynchronousQueue;
class Apple{
    //생성자
    
    
    //field int a,b;
    
    
    //int a=10,b=20;//첫번째 방법    필드변수 선언과 동시에 초기화 
    int a,b; //두번째 방법  밑에 main에서 값 초기화
    
    //method
    
    
    void func01() {
        System.out.println("함수콜");
    }
    int func02() {
        System.out.println("함수2콜");
        return 100;
    }
    void func03(int a,int b) {
        System.out.println(a+b);
    }
    void func04() {//먹다 void eat(){}
        //System.out.println(a+" "+b);//멤버끼리는 변수 공유가능
        //클래스내에서는 그냥 사용가능 외부에서는 객체. 로 써야된다.
        
        
        System.out.println(this.a+" "+this.b);
        
        
    }
}
public class Hello {
    public static void main(String[] args) {
        Apple a1 = new Apple();
        a1.a = 30;
        a1.b = 40;
        System.out.println(a1.a+" "+a1.b);
        a1.func01();
        a1.func02();
        a1.func03(1,3);
        a1.func04();
        
 
    }
}
 
 
cs



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
package Pack01;
import java.util.concurrent.SynchronousQueue;
class Apple{
    void func01() {
        System.out.println(a+" "+b);
    }
 
    Apple(){
        //class이름과 완전히 동일하게 생성되는 함수를
        //생성자 함수라고한다.
        //생성자 함수의 이름은 class함수의 이름과 반드시 동일해야한다.
        //생성자 함수는 프로그래머가 직접 호출할 수 없다.
        //생성자함수는 return값을 가질 수 없다 = void 붙일 수 없다.
 
 
        //생성자 함수 사용 목적은 filed변수 초기화 하는 것이다.
        System.out.println("호랑이");
    }
    int a=77,b=88;
}
public class Hello {
    public static void main(String[] args) {
        System.out.println("코끼리");
        Apple a1 = new Apple();
        System.out.println("앵무새");
        //생성자 함수는 객체가 생성될때 무조건 한번 자동 콜된다.
 
        Apple a2 = new Apple();//객체 생성 = 생성자 콜
        a1.func01();
        a2.func01();
    }
}
 
 
cs


19번


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
package Pack01;
import java.util.concurrent.SynchronousQueue;
class Lion{
    //일반적으로 필드를 제일 먼저 넣는다.(변수)
    int a,b;
    Lion(){//생성자 함수도 함수이므로 오버로딩 할 수 있다.
        //오버로딩
        //함수 이름은 같은데 인수 갯수가 다르거나, 타입이 다른것
        
        System.out.println("생성자콜");
        a=77;
        b=88;
    }
    Lion(int x){//생성자 두개 - 오버로딩
        System.out.println("앵무새");
        a=x;
        b=x;//던지는 값으로 초기화시키겠다.
    }
    Lion(int x,int y){
        System.out.println("독수리");
        a=x;
        b=y;
    }
    void func01() {
        System.out.println(a+" "+b);
    }
}
public class Hello {
    public static void main(String[] args) {
        Lion l1 = new Lion();//다른 비행기 l1,l2
        l1.func01();
        
        Lion l2 = new Lion(1);
        l2.func01();
        
        Lion l3 = new Lion(1,2);
        l3.func01();
    }
}
 
 
cs


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
package Pack01;
import java.util.concurrent.SynchronousQueue;
class Lion{
    int a=10,b=20;//1번이 변수생성시 초기화
    Lion(){
        System.out.println(a+" "+b);
        System.out.println("생성자콜");
        a=30;b=40;//2번이 생성자에서 갱신
        System.out.println(a+" "+b);
    }
    void func01() {
        System.out.println(a+" "+b);
    }
}
public class Hello {
    public static void main(String[] args) {
        Lion l1 = new Lion();
        System.out.println("main에서 초기화후");
        l1.a=50;//3번이 main에서 생성자 초기화
        l1.b=60;
        System.out.println(l1.a+" "+l1.b);
    }
}
 
 
cs



21번



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package Pack01;
import java.util.concurrent.SynchronousQueue;
class Lion{
    int a,b;
    Lion(){
        //생성자를 미리 만들어주는게 좋다.
    }
    //자바는  기본적으로 생성자가 없을 경우 default생성자를 만들어준다.
    Lion(int x){//인수 전달이 없는 생성자를 default생성자라 한다.
 
    }
}
public class Hello {
    public static void main(String[] args) {
        Lion l1 = new Lion();
    }
}
 
 
cs



22번


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
package Pack01;
import java.util.concurrent.SynchronousQueue;
class Orange {
    int a,b;
    Orange(){
        
    }
    void func01() {
        System.out.println("함수1번콜");
    }
}
public class Hello {
    public static void main(String[] args) {
        Orange o1 = new Orange();
        System.out.println(o1);//Pack01.Orange@15db9742 뒤에는 비행기의 시리얼 번호
        Orange o2 = new Orange();
        System.out.println(o2);//객체의 고유 민증번호는 같을 수 없다.
        Orange o3;//new 안하면 실제 비행기는 안만들어진것이다. 비행기 이름만 지은것
        //System.out.println(o3);
        o3 = new Orange();//얘도 새로운 민증번호 발급 가능
        System.out.println(o3);
        o3 = new Orange();//같은객체명을 만들면 기존객체는 삭제됨
        System.out.println(o3);
        Orange o4=o1;//공유는 참조
        System.out.println(o4);
        
        o4.a = 100;
        o4.b=200;
        Orange o5;
        {
        Orange o6 = new Orange();
        o5=o6;//스코프안에서 공유일어났으므로 스코프끝나고도 사용가능
        }
        System.out.println(o5.a);
        System.out.println(o1.a+""+o1.b);
    }
}
 
cs



23번


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
package Pack01;
import java.util.concurrent.SynchronousQueue;
class Orange {
    int a,b;
    Orange(){
        System.out.println(this);//this는 오렌지 객체다. - 민증번호가 같다.
    }
    void func01() {
        System.out.println(this);
    }
}
 
public class Hello {
    public static void main(String[] args) {
        Orange orange = new Orange();//확정적일땐 orange 다 쓴다.
        System.out.println(orange);
        
        Orange o1 = new Orange();
        System.out.println(o1);
        Orange o2 = new Orange();
        System.out.println(o2);//o1과 o2의 this는 다르다.
        System.out.println();
        o1.func01();
        o2.func01();
    }
}
cs


24번


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
63
64
65
66
67
package Pack01;
import java.util.concurrent.SynchronousQueue;
class Apple {
    int a,b;
    Apple(){
        System.out.println("사과 생성자콜");
    }
    void func01() {
        System.out.println("사과 함수1번");
    }
}
class Orange {
    int a,b;
    Orange(){
        System.out.println("오렌지 생성자콜");
    }
    void func01() {
        System.out.println("오렌지 함수1번");
    }
    int func02() {
        return 100;
    }
    float func03() {
        return 3.14f;
    }
    boolean func04() {
        return true;
    }
    long func05() {
        long a=10,b=20;
        return a+b;
    }
    double func06() {
        double a=3.14;
        double r = 10.0;
        double result = r*r*a;
        return result;
    }
    int func07(int x,int y) {
        int r=(x*x)+(y*y);
        return r;
    }
    Apple func08() {//중요
        Apple apple = new Apple();
        //Apple클래스가 있으므로 사용가능 
        return apple;
    }    
}
public class Hello {
    public static void main(String[] args) {
        Orange o1 = new Orange();//확정적일땐 orange 다 쓴다.
        //a1.func01();
        o1.func01();
        System.out.println(o1.func02());//return값이 있어서 출력값에 바로 연결가능하다.
        //return값이 있어야 출력값에서 호출가능
        System.out.println(o1.func03());
        System.out.println(o1.func04());
        System.out.println(o1.func05());
        System.out.println(o1.func06());
        System.out.println(o1.func07(10,10));
        Apple tt = o1.func08();//new Apple(); Orange의 func08 가져온다
        tt.func01();//Apple의 함수 1번 출력
        o1.func08().func01();//사라지기 일보직전에 .찍어서 사과를 살려서 사과의 1번을 출력
    }
}
 
 
cs


728x90

'비트 단기 > java' 카테고리의 다른 글

java 41번  (0) 2018.12.03
24~ 31  (0) 2018.11.29
java class시작  (0) 2018.11.27
java 2 10~  (0) 2018.11.27
java 1  (0) 2018.11.26

댓글