지금 단계에서는 객체지향 프로그래밍이란 이렇게 기억해 봅시다.
객체지향이란 객체와 객체간에 관계를 형성하고 상호작용하게 코드를 작성하는 것
package ch10;
public class Bus {
int busNumber;
int count;
int money;
// 사용자 지정 생성자
public Bus(int number) {
busNumber = number;
//객체 생성시에 제일 먼저 실행되는 부분
}
//메소드
// 승객을 태우다.
public void take(int pay) {
// money = money + pay;
money += pay;
count++;
}
public void showInfo() {
System.out.println("------------상태창--------------");
System.out.println("버스 호선 : " + busNumber);
System.out.println("버스 승객수 : " + count);
System.out.println("버스 수익금 : " + money);
}
}
package ch10;
public class Subway {
int ilneNumber;//
int count;
int money;
// 사용자 정의생성자
public Subway(int number) {
ilneNumber = number;
}
//승객을 태우다
public void take(int pay) {
money += pay;
count++;
}
public void showInfo() {
System.out.println("------------상태창--------------");
System.out.println("지하철 호선 : " + ilneNumber);
System.out.println("지하철 승객수 : " + count);
System.out.println("지하철 수익금 : " + money);
}
}
package ch10;
public class Student {
String name;//이름
int money;//소지금
//매개변수 2개를 받는 생성자를 만들어 보자
public Student(String n, int m){
name = n;
money =m;
}
// 학생이 버스를 탄다.
public void takeBus(Bus bus){
bus.take(1000);
//버스에 천원을 내고 내 멤버 변수 money에 천원을 빼자
money = money - 1000;
}
//학생이 지하철을 탄다.
// 학생에 상태창을 보여준다.
public void showInfo(){
System.out.println("---------**상태창**----------");
System.out.println("학생 이름 : " + name);
System.out.println("학생 소지금 : " + money);
}
}
package ch10;
public class GoingToSchool {
//코드의 진입점
public static void main(String[] args) {
Bus bus1 = new Bus(133);
Bus bus2 = new Bus(1004);
Subway subway1 = new Subway(3);
Student s1 = new Student("홍길동", 10_000);
Student s2 = new Student("티모", 5_000);
//s1.showInfo();
//bus1-->실제 값은 주소이다.
s1.takeBus(bus1);
s2.takeBus(bus1);
System.out.println("-------------------");
s1.showInfo();
bus1.showInfo();
//사전 기반 지식
//기본 데이터(소문자로 시작 선언) , 참조 타입(대문자로 시작 선언)
//int 값이 담긴다
//대문자로 선언하는 타입은 전부 (참조 타입이다.)
// Student s1; <----(주소 값)
}// end of main
}
'JAVA 객체 지향 핵심' 카테고리의 다른 글
(JAVA)함수, 메서드 생성자 복습 - 6_1 (0) | 2025.04.16 |
---|---|
(JAVA)함수,메서드, 생성자란 (0) | 2025.04.15 |
(JAVA)생성자(constructor) (0) | 2025.04.15 |
(JAVA)메소드(method)란 뭘까? - 5 (0) | 2025.04.15 |
(JAVA)함수 만들기 (연습 문제) (0) | 2025.04.15 |