Programming
JAVA 06-2 객체 간 협력
grace.codepoet
2022. 8. 28. 00:57
학생 클래스 구현
package cooperation;
public class Student {
public String studentName;
public int grade;
public int money;
public Student(String studentName, int money) {
this.studentName = studentName;
this.money = money;
}
public void takeBus(Bus bus){
bus.take(1000);
this.money -= 1000;
}
public void takeSubway(Subway subway) {
subway.take(1500);
this.money -= 1500;
}
public void showInfo(){
System.out.println(studentName + "님의 남은 돈은" + money + "입니다");
}
}
버스 클래스 구현
package cooperation;
public class Bus {
int busNumber;
int passengerCount;
int money;
public Bus(int busNumber){
this.busNumber = busNumber
}
public void take(int money) {
this. money += money;
passengerCount++;
}
public vlid showInfo() {
System.out.println("버스"+busnumber + "번의 승객은" + passengerCount + "명이고, 수입은" + money );
}
버스와 지하철 타기 클래스 구현
package cooperation;
public class TakeTrans {
public static void main(Stirng[] args) {
Student studentJames = new Student("James", 5000);
Student studentTomas = new Student("Tomas", 10000);
Bus bus100 = new Bus(100);
studentJames.takeBus(bus100);
studentJames.showInfo();
bus100.showInfo();
Subway subwayGreen = new Subway("2호선");
studentTomas.takesubway(subwayGreen);
studentTomas.showInfo();
subwayGreen.showInfo();
}
}