본문 바로가기
오류리포트

[오류리포트] Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method score(int) is undefined for the type Student at class01.Test01.main(Test01.java:63)

by ssunooo 2024. 7. 12.

 

 

캡슐화에 관한 프로젝트를 진행중

63번 줄에 오류가 발생했다.

 

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
package class01;
 
class Student {
    // 클래스 외부에서 멤버변수에 접근하는것을 JAVA에서는 막아야함!!!
    // JAVA 공개정책
    // "접근제어자"를 사용해서 공개범위를 변경
    private int num;
    private String name;
    private int score;
    // private == 클래스 외부에서 접근할수없다!
    // 모든 멤버변수에 private을 반드시 추가해주세요
    Student(int num,String name){
        this.name=name;
        this.num=num;
        this.score=50;
    }
    
    // 게터세터
    // getter setter
    
    
    // 코드내에 어디서든지 호출될수 있어야함!
    // public
    public void setNum(int num) {
        this.num=num;
    }
    public int getScore() {
        return score;
    }
 
    public void setScore(int score) {
        this.score = score;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getNum() {
        return this.num;
    }
    
    
    @Override
    public String toString() {
        return this.num+"번 학생 "+this.name+"은 "+this.score+"점입니다.";
    }
    
}
 
public class Test01 {
    
    public static void main(String[] args) {
        
        Student stu1=new Student(1001,"홍길동");
        
        // 재시험 등으로 점수가 변경되어야할때????
        
        stu1.score(100); // 메인이라는 공간에서 홍길동의 점수를 바꿀수 있다?!
        // 개발자가 임의로 데이터값을 언제든지 수정할수있다는 뜻
        // 모든 데이터값은 "함수,메서드"를 통해서 변경해야만한다!!!!
        // 로그(기록)라는게 남거든요
        // 연산자는 안남아요...ㅠ
        System.out.println(stu1.getScore());
        
    }
}
 
cs

 

캡슐화 오류
오류내용

 

 

9번째줄 private int score; 에서 private로 클래스 외부,

즉 메인에서 데이터값을 변경할 수 없게 만들어놓았기 때문에

발생한 오류였다.

 

해결방안으로 getter,setter를 사용해

setScore() 메서드를 만들고 stu1.setScore(100);를 작성하니

오류가 해결되었다.

 

오류해결

 

 

 

2024.07.12