객체지향프로그래밍
OOP - Object Oriented Programming
( 추가로 알아두면 좋은 것)
API - Application Programming Interface
JDK - Java Development Tool Kit
프로그래밍 언어 + 객체지향개념(규칙)
- 코드의 재사용성이 높다 ( 재사용성 )
- 코드의 관리가 용이하다 ( 유지보수 )
- 신뢰성이 높은 프로그래밍을 가능하게 한다 ( 중복된 코드의 제거 )
-> 제품설계도
와 제품
과의 관계와 같다
클래스
- 정의: 객체를 정의해 놓은 것
- 용도: 객체를 생성하는데 사용
객체
- 정의: 실제로 존재하는 것. 사물 또는 개념.
- 용도: 객체가 가지고 있는 기능과 속성에 따라 다름
속성(변수)
과 기능(메서드)
으로 이루어짐
- 속성( property ) *멤버변수( member variable ), 특성( attribute ), 필드( field ), 상태( state )
- 기능( function ) *메서드( method ), 함수( function ), 행위( behavior )
ex ) TV
class Tv { // Tv의 클래스이자 설계도
// 변수
String color; // 색깔
boolean power; // 전원상태
int channel; // 채널
// 메서드
void power() {power = !power;}
void channelUp() {channel++;}
void channelDown() {channel--;}
}
object 설계 : 클래스 작성
class 클래스명{
변수선언
함수(method)
}
object 생성(선언)
클래스명 변수명 = new 클래스명(); // 클래스 객체 생성 후, 객체의 주소를 참조변수에 저장
-> 클래스변수 == instance(주체) == object(객체)
// 클래스변수는 stack영역, 실제영역은 heap영역에 위치
object 사용 : 변수와 메서드 사용
// 생성
Tv t = new Tv();
// 사용
t.channel = 7; // Tv인스턴스의 멤버변수 channel의 값을 7로 한다 -> 변수 사용
t.channelDown(); // Tv인스턴스의 메서드 channelDown()을 호출한다 -> 메서드 사용
System.out.println("현재 채널은 " + t.channel + " 입니다.");
- 하나의 인스턴스를 여러 개의 참조변수가 가리키는 경우 - 가능
- 여러 인스턴스를 하나의 참조변수가 가리키는 경우 - 불가능
객체 배열 == 참조변수 배열
// Tv tv1, tv2, tv3;
Tv[] tvArr = new Tv[3]; // 길이가 3인 Tv타입의 참조변수 배열
// 객체를 생성해서 배열의 각 요소에 저장
tvArr[0] = new Tv();
tvArr[1] = new Tv();
tvArr[2] = new Tv();
Tv[] tvArr = {new Tv(), new Tv(), new Tv()};
: 데이터 + 함수
: 사용자 정의 타입 - 원하는 타입을 직접 만들 수 있다
int huor;
int minute;
int second;
class Time {
int huor;
int minute;
int second;
}
Time t = new Time(); // 클래스로 정의할 경우 하나의 객체로만 생성 가능
t.hour = 12;
t.minute = 34;
t.second = 56; // 객체지향적인 코드
public class Main {
public static void main(String[] args) throws Exception {
MyClass mycls = new MyClass(); // mycls : 참조변수, new MyClass() : 객체
mycls.x = 1;
mycls.y = 2;
mycls.name = "홍길동";
mycls.method();
}
}
// 클래스 설계
class MyClass {
// (멤버)변수
int x, y;
String name;
// 메소드(함수)
void method() {
System.out.println("MyClass method() 호출: " + name + x + y);
}
}
// console
// MyClass method() 호출: 홍길동12
public class Main {
public static void main(String[] args) throws Exception {
Student arrStu[] = new Student[3]; // 참조변수 배열(객체 배열)을 생성
// -> Student arrStu1, arrStu2, arrStu3
// 각각의 배열에 객체를 할당해야 함
for(int i = 0; i < arrStu.length; i++) {
arrStu[i] = new Student();
}
arrStu[0].language = 100;
System.out.println("language = " + arrStu[0].language);
}
}
class Student {
String name;
int language, english, math, history;
}
// console
// language = 100
public class Main {
public static void main(String[] args) {
Sorting sort = new Sorting();
sort.init();
sort.input();
sort.sorting();
sort.result();
}
}
class Sorting {
int number[];
int updown;
// 초기화
void init() {
number = null;
updown = 0;
}
// 입력
void input() {
Scanner sc = new Scanner(System.in);
System.out.print("정렬할 숫자의 갯수 = ");
int count = sc.nextInt();
number = new int[count];
for(int i = 0; i < number.length; i++) {
System.out.print((i + 1) + "번째 수 = ");
number[i] = sc.nextInt();
}
System.out.print("오름(1)/내림(2) = ");
updown = sc.nextInt();
}
// 정렬
void sorting() {
int temp;
for(int i = 0; i < number.length - 1; i++) {
for(int j = i + 1; j < number.length; j++) {
if(updown == 1) {
if(number[i] > number[j]) {
temp = number[i];
number[i] = number[j];
number[j] = temp;
}
} else {
if(number[i] < number[j]) {
temp = number[i];
number[i] = number[j];
number[j] = temp;
}
}
}
}
}
// 결과
void result() {
for(int i = 0; i < number.length; i++) {
System.out.print(number[i] + " ");
}
}
}
// console
/*
정렬할 숫자의 갯수 = 5
1번째 수 = 3
2번째 수 = 6
3번째 수 = 5
4번째 수 = 1
5번째 수 = 8
오름(1)/내림(2) = 1
1 3 5 6 8
*/
public class Main {
public static void main(String[] args) throws Exception {
Calculator cal = new Calculator();
/*
cal.input();
cal.process();
cal.result();
*/
cal.loop();
}
}
class Calculator {
int number1, number2, result;
String operator;
void input() {
Scanner sc = new Scanner(System.in);
System.out.print("첫번째 숫자 = ");
number1 = sc.nextInt();
System.out.print("연산자(+, -, *, /) = ");
operator = sc.next();
System.out.print("두번째 숫자 = ");
number2 = sc.nextInt();
}
void process() {
switch(operator) {
case "+":
result = number1 + number2;
break;
case "-":
result = number1 - number2;
break;
case "*":
result = number1 * number2;
break;
case "/":
result = number1 / number2;
break;
}
}
void result() {
System.out.println(result);
}
// 무한반복
void loop() {
while(true) {
input();
process();
result();
}
}
}