절차지향형 코딩
데이터의 흐름이 보임
객체지향형 코딩
//Calc.java
package ch06;
public class Calc {
private int a = 0;
private int b = 0;
public void add() {
System.out.println(this.a+this.b);
}
public void multi() {
System.out.println(this.a*this.b);
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
}
*** private 변수 : 외부에서 접근 불가
*** public 변수 : 외부에서 접근 가능
*** Class를 datatype으로 볼 수 있음 -> 매개 변수로 쓸 수 있음
함수지향형
생각대로 코딩
package ch06;
public class FuncLike {
interface Opt{
int opt(int a, int b);
}
interface Display {
void display(int opt);
}
private static void display(int a, int b, Opt opt, Display dis) {
dis.display(opt.opt(a, b));
}
public static void main(String[] args) {
Opt opt;
Display dis = (a) -> System.out.println(a);
opt(a,b) -> a+b;
display(1,2,opt,dis);
opt(a,b) -> a*b;
display(1,2,opt,dis);
}
}
클래스
- 클래스 타입을 이용해 메모리에 생성된 객체를 클래스 인스턴스라고 함
- 클래스의 변수와 메서드를 이용해 객체의 특징을 정의
- 클래스명은 CapWords를 이용함 (파스칼 표기법 : 대문자 시작)
- 클래스는 사용자 정의 데이터 타입과도 같음
- 클래스 선언 방법
public class 클래스명
멤버변수; // 인스턴스 멤버변수
static 멤버변수; // 클래스 멤버변수
piblic 클래스명 (매개변수) { // 생성자
실행구문
}
접근제한자 반환타입 메소드명(매개변수) { // 인스턴스 메소드
로컬변수;
실행구문
}
static 접근제한자 반환타입 메소드명(매개변수){ // 클래스 메소드
로컬변수;
실행구문
}
}
*** 힙공간엔 매개변수만 올라감
*** static 키워드 -> 클래스 메소드
Person 클래스 생성
//Person 클래스 생성
package ch06.person;
public class Person {
String name = "";
int age = 0;
String sex = "";
boolean isHungry = true;
public void eat() {
System.out.println(this.name+"은(는) 먹는다.");
this.isHungry = false;
}
public void work() {
System.out.println(this.name+"은(는) 일한다.");
this.isHungry = true;
}
@Override
public String toString() {
return "[name=" + name + ", age=" + age + ", sex=" + sex + ", isHungry=" + isHungry + "]";
}
}
// Person 클래스 텍스트
package ch06.person;
public class PersonTest {
public static void main(String[] args) {
Person person1 = new Person();
person1.name = "김지원";
person1.age = 25;
person1.sex = "F";
person1.isHungry = true;
person1.eat();
System.out.println(person1.toString());
Person person2 = new Person();
person2.name = "홍길동";
person2.age = 14;
person2.sex = "M";
person2.isHungry = true;
person2.eat();
person2.work();
System.out.println(person2.toString());
}
}
Getter 사용하기
package ch06.person;
public class Person {
String name = "";
int age = 0;
String sex = "";
private boolean isHungry = true;
public void eat() {
System.out.println(this.name+"은(는) 먹는다.");
this.isHungry = false;
public boolean isHungry() {
return isHungry;
}
isHungry를 외부에서 변경할 수 없도록 private 변수로 선언하고
조회할 때는 Getter 사용하도록 함
클래스에 생성자 만들기
package ch06.person;
public class Person {
String name = "";
int age = 0;
String sex = "";
private boolean isHungry = true;
public Person() {
}
public Person(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
package ch06.person;
public class PersonTest {
public static void main(String[] args) {
Person person3 = new Person("젼",20,"F");
person3.eat();
System.out.println(person3.toString());
}
}
생성자 오버로드
같은 메소드 이름에 매개변수만 다르게해 재정의
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person(String name, int age, String sex) {
this(name,age); // this 생성자
this.sex = sex;
}
클래스를 배열로 생성
package ch06.person;
public class PersonTest {
public static void main(String[] args) {
Person[] persons = new Person[3];
persons[0] = new Person("김지원",25,"F");
persons[1] = new Person("홍길동",25,"M");
persons[2] = new Person("ㅎ",20,"F");
for(Person person:persons) {
System.out.println(person.toString()); // 출력
}
}
}
나이의 총합 구하기
package ch06.person;
public class PersonTest {
public static void main(String[] args) {
Person[] persons = new Person[3];
persons[0] = new Person("김지원",25,"F");
persons[1] = new Person("홍길동",25,"M");
persons[2] = new Person("ㅎ",20,"F");
int total = 0;
for(Person person : persons) {
total+=person.age;
}
System.out.println(total);
}
}
메서드
가변인자 메서드
public int sum(int... a) {
int total=0;
for(int i:a) {
total+=i;
}
return total;
}
this
- 현재 클래스의 인스턴스를 참조할 때 이용
- 현재 클래스의 생성자를 사용할 때 이용
super
- 상속받을 경우 부모 클래스를 참조할 때 이용
- 부모의 생성자를 호출할 경우 이용
상속
public class 자식 클래스명 extends 부모 클래스명 {
실행구문
}
- 다형성을 확보하기 위한 가장 기본적인 방법
- 새로운 클래스에서 다른 클래스의 생성자를 제외한 멤버변수, 메서드 등을 이용 가능
- 새로운 클래스를 자식 클래스, 멤버 변수, 메서드를 제공하는 클래스를 부모 클래스라고 함
- 부모 클래스는 다양한 접근 제한자를 이용해 자식 클래스의 이용 범위를 제한할 수 있음
- 부모 클래스가 하나 이상인 다중 상속은 지원하지 않고, 단일 상속만 지원함
'Programming > Java' 카테고리의 다른 글
[Java] 1/21 강의정리 (인터페이스, 예외처리, try catch,String Tokenizer,Generic) (0) | 2021.01.21 |
---|---|
[Java] 1/20 강의 정리 (로그인 구현, 상속, 오버라이드, 패키지, 추상클래스, 인터페이스) (0) | 2021.01.20 |
[Java] 1/18 강의 정리 (JVM, JDK, 연산자, 배열, 객체지향) (0) | 2021.01.18 |
댓글