객체지향프로그래밍
: 유지보수가 용이함
형식 : object 설계
class 클래스명{
변수 선언
함수(method)선언
}
객체를 만들어 사용하기 위해서는 클래스가 필요함
클래스는 객체를 만들기위해 정의된 설계도
클래스명 클래스 변수 = new 클래스명();
-> 객체 생성, 선언
//클래스명 클래스변수 = new 클래스명();
MyClass mycls = new MyClass(); //객체 생성
//System.out.println(mycls);
mycls.x =1;
mycls.y =2;
mycls.name = "박예린";
mycls.method();
Student stu = new Student();
stu.name = "박예린";
stu.language = 100;
stu.english = 90;
stu.math = 95;
//객체배열 -> 변수일 뿐
Student arrStu[] = new Student[3]; //배열 변수 선언!!
//Student arrStu1, arrStu2, arrStu3; //위 문장 형식과 똑같은 것
//for문과 주석3줄 같은 형식 -> 밑 주석을 표현하기 위해 for문 형식으로 적은
for (int i = 0; i < arrStu.length; i++) {
arrStu[i] = new Student(); //객체 선언
}
//arrStu1 = new Student();
//arrStu2 = new Student();
//arrStu3 = new Student();
//객체 사용 위해서는 이렇개 해야 함
arrStu[0] = new Student();
//arrStu[1] = new Student();
//arrStu[2] = new Student();
}
}
//클래스 설계(구축)
class MyClass{
//(멤버)변수
int x, y;
String name;
//함수(메소드)
void method() {
System.out.println("MyClass method() 호출");
}
}
class Student{
String name;
int language, math, english, history;
}