필드(field), 생성자함수(constructor), 메소드(method)
데이터가 저장되는 곳. (변수)
주로 private 접근지정자로 지정. (필드에 접근하기 위해서 보통 해당 클래스의 메소드를 통해 필드에 접근)
static field 와 instance field 두 종류가 있다.
객체 생성 시 초기화 역할을 담당.
생성자 함수는 객체 생성 시 자동으로 생성된다.
생성자 함수를 아무 선언도 안 해주면 디폴트 생성자 자동 생성된다.
리턴형이 없다.
public 클래스명() {
}
같은 클래스 내부에서 기능을 담당하는 영역을 구분해놓은 것.
특정 기능을 하기도 하지만, 외부에서 내부 필드 값(캡슐화를 위해 private으로 세팅) 을 수정하거나 얻기 위해서 사용한다.
메소드의 종류로는 static method와 instance method가 있다.
class 클래스이름 {
//필드
private int num1; //캡슐화를 위해 필드는 보통 private으로 접근 지정자를 지정한다.
//생성자함수
public 클래스이름() {
}
//메소드
//클래스 외부에서 필드로 접근하려면 메소드를 통해서 접근한다. 따라서 메소드의 경우 대부분 public으로 접근 지정자를 지정한다.
// setter : 객체로 필드값 저장하는 메소드
public void setNum1(int num1){
this.num1 = num1;
}
// getter : 외부로 필드값 제공해주는 메소드
public int getNum1() {
return num1;
}
}
public class RunClass {
public static void main(String[] args) {
클래스이름 객체이름 = new 클래스이름(); //클래스에 접근하기 위해 객체 생성
객체이름.getNum1(); //객체를 통해 클래스의 메소드를 호출하고 메소드를 통해 필드에 접근 가능하다
}
}
class는 (접근지정자) class 클래스이름 { ... }의 형태를 지닌다.
즉, class Class1 { } 혹은 public class Class2 { } 와 같은 형태로 사용 가능하다.
접근지정자를 지정해주지 않으면 default 접근지정자가 자동으로 들어간다. 따라서 같은 패키지 내에서만 해당 클래스에 접근이 가능하다.
//public class 클래스는 java 파일 하나당 하나만 생성 가능하다.
//물론 class 자체는 무한으로 만들 수 있다.
public class Hello {
//메소드는 객체의 기능을 구현하기 위해 "클래스 내부"에 구현한다.
//특정 기능끼리 묶어놓는 영역이다.
public void name1 { //name1이라는 메소드
}
//메인 메소드
public static void main(String[] args) {
}
}
package day5;
import java.util.Scanner;
class Score {
//필드
private String name;
private int kor;
private int eng;
private int math;
private int sum;
private float avg;
//메소드
public void setName (String name) {
this.name = name;
}
public String getName() {
return name;
}
public int getKor() {
return kor;
}
public void setKor(int kor) {
this.kor = kor;
}
public int getEng() {
return eng;
}
public void setEng(int eng) {
this.eng = eng;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getSum() {
sum = kor + eng + math;
return sum;
}
public float getAvg() {
avg = sum/3;
return avg;
}
}
public class Task2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Score scc = new Score();
System.out.println("총 반 갯수 입력 >> ");
int classNum = sc.nextInt();
int[][] arr1 = new int [classNum][];
for (int i = 0; i<classNum; i++) {
System.out.println((i+1) + "반의 학생 수 입력 >> ");
int inputNum = sc.nextInt();
arr1[i] = new int[inputNum];
}
int[][][] arr2 = new int[classNum][][];
for(int i = 0; i<classNum; i++) {
int studentCount = arr1[i].length;
arr2[i] = new int[studentCount][3];
}
for (int i = 0; i<classNum; i++) {
for (int j = 0 ; j<arr2[i].length; j++) {
System.out.println("학생의 이름을 입력하세요.");
String name = sc.next();
scc.setName(name);
System.out.println("국어 성적을 입력하세요.");
int korScore = sc.nextInt();
scc.setKor(korScore);
System.out.println("영어 성적을 입력하세요.");
int engScore = sc.nextInt();
scc.setEng(engScore);
System.out.println("수학 성적을 입력하세요.");
int mathScore = sc.nextInt();
scc.setMath(mathScore);
System.out.println("-------성적출력---------");
System.out.println(scc.getName() + " 학생의 성적은 ");
System.out.print("국어 성적 : " + scc.getKor() + " 영어 성적 : " + scc.getEng() + " 수학 성적 : " + scc.getMath());
System.out.println("");
System.out.println("과목 총합은 " + scc.getSum() + ", 과목 평균은 " + scc.getAvg() + " 입니다.");
System.out.println("----------------------");
}
}
sc.close();
}
}
와!