hyeonga_code

Java_44_클래스_지정 예약어 본문

Java

Java_44_클래스_지정 예약어

hyeonga 2023. 10. 7. 05:59
반응형

- 클래스
  - final
     - 상속이나 익명 중첩 클래스에서 메소드 오버라이드를 할 수 없게 하는 기능

  - static final
 - 잘 사용되는 기능은 아님
 - static 기능과 final 기능을 합친 기능

  - synchronized
     - 동기화_멀티스레드

  - abstract
     - 추상화 메소드

  - native
     - 다른 언어_C++에서 만든 기능을 가져와 사용하고자 할 때

- final 클래스
- 상속 할 수 없는 클래스
=====<ㅠㄱ>

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
final class A07{
    int a;
    int b;
    public A07() {
        a = 10;
        b = 20;
    }
}
 
class B07 extends A07{
    // 오류 : The type B07 cannot subclass the final class A07
    int c;
    public B07() {
        c = 30;    
    }
    public void disp() {
        System.out.println("a = " + a);
        System.out.println("b = " + b);
        System.out.println("c = " + c);
    }
}
 
public class same {
    public static void main(String[] args) {
        B07 ap = new B07();
        ap.disp();
    }
}



- final 메소드
- 성적 관리 프로그램
=====

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import java.util.*;
 
class Member {
    String name;
    private int kor, eng;
    protected int tot;
    private int rank;
    Member(String name, int kor, int eng) {
        // 생성자 서언
        this.name = name;
        this.kor = kor;
        this.eng = eng;
        this.tot = kor + eng;
    }
    public String getName() {
        return name;
        // 이름 변경 불가
    }
    public void setKor(int kor) {
        this.kor = kor;
    }
    public int getKor() {
        return kor;
    }
    public void setEng(int eng) {
        this.eng = eng;
    }
    public int getEng() {
        return eng;
    }
    public void setTot() {
        tot = kor + eng;
    }
    public int getTot() {
        return tot;
    }
    public int getRank() {
        return rank;
    }
    public void clearRank() {
        rank = 1;
    }
    public void plusRank() {
        rank++;
    }
    void disp() {
        // 출력
        System.out.println(name + "'s total score : " + tot + " | Rank : " + rank);
    }
}
 
class Member2 extends Member {
    // Member 상속 받은 Memeber2 클래스
    private int math;
    // 수학 점수 추가
    public Member2(String name, int kor, int eng, int math){
        // 함수 생성
        super(name, kor, eng);
        this.math = math;
        // 수학은 Member2에서 선언한 변수 값
        this.tot = getKor() + getEng() + this.math;
        // 총 점 갱신
    }
    public void setMath(int math) {
        // 수학 점수 받아오는 함수 생성
        this.math = math;
    }
    public int getMath() {
        // 수학 점수 가져오는 함수 생성
        return math;
    }
    public void setTot() {
        // 총 점 갱신하는 함수 생성
        tot = getKor() + getEng() + getMath();
    }
}
 
public class same {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 입력 기능
        System.out.print("Input people count: ");
        int people = in.nextInt();
        // 인원수 입력 받기 
        Member[] mb = new Member[people];    // 객체 선언부
        // 입력받은 인원수 크기의 배열 생성
 
        for (int i = 0; i < people; i++) {
            // 인원 수 만큼 입력 받기
            System.out.print("Input name: ");
            String name = in.next();
            System.out.printf("Input %s's kor: ", name);
            int kor = in.nextInt();
            System.out.printf("Input %s's eng: ", name);
            int eng = in.nextInt();
            System.out.printf("Input %s's math: ", name);
            int math = in.nextInt();
            mb[i] = new Member(name, kor, eng);    // 객체 생성부
        }
 
        for (int i = 0; i < people; ++i) {
            // i = 내 성적
 
            mb[i].clearRank();
            for (int j = 0; j < people; ++j) {
                // j = 다른 학생 성적
                if (mb[i].getTot() < mb[j].getTot()) {
                    // 내 성적이 다른 학생 성적의 총 점보다 작으면
                    mb[i].plusRank();
                    // 내 순위를 하나 올린다.
                }
            }
        }
 
        for (int i = 0; i < people; ++i) {
            mb[i].disp();
        }
    }
}

 

반응형

'Java' 카테고리의 다른 글

Java_46_toString  (0) 2023.10.09
Java_45_클래스_중첩 클래스  (0) 2023.10.08
Java_43_지정 예약어  (0) 2023.10.06
Java_42_캡슐화  (0) 2023.10.05
Java_41_접근 제한자_public < protected < default < private  (0) 2023.10.02