어쩌겠어 해내자!!!!!!!!!!!!!!!!!!!
실행클래스 : main을 포함한 클래스
라이브러리 클래스 : main이 없는 클래스
=> 실행클래스가 있어야 실행된다!
=> main이 있는 실행클래스는 하나만 있으면 된다!
Car 라는 라이브러리 클래스를 하나 만들어주고
ClassMain 이라는 실행클래스를 하나 만들어준다
Car (라이브러리 클래스)
①필드
<속성>
String model; // 모델명
String company; // 제조사
String color = "red"; // 색깔
int passengerCount; // 몇인승
int cc; // 배기량
<상태>
boolean rightState; // 차량상태정상
int speed; // 현재속도
int accKm; // 누적키로수
②생성자
③메소드
ClassMain (실행클래스)
Car myCar = new Car();
//클래스 변수 = new 클래스
Car friendCar = new Car();
Car momCar = new Car();
=> myCar, friendCar, momCar 라는 각각 다른 공간의 객체가 생성되었다는 뜻!
Car myCar = new Car();
myCar.model = "레이"; //모델은 String 이였으니까!
myCar.passengerCount = 5; // 몇인승은 int
Car friedCar = new Car();
friendCar.model = "캐스퍼";
friendCar.passengerCount = 4;
friendCar.color = "black";
Car momCar = new Car();
System.out.printf("내 차 모델 : %s 탑승인원 : %d 색깔 : %s\n"
, myCar.model, myCar.passengerCount, myCar.color);
System.out.printf("친구 차 모델 : %s 탑승인원 : %d 색깔 : %s\n"
, friendCar.model, friendCar.passengerCount, friendCar.color);
System.out.printf("엄마 차 모델 : %s 탑승인원 : %d 색깔 : %s\n"
, momCar.model, momCar.passengerCount, momCar.rightState);
출력 결과 :
내 차 모델 : 레이 탑승인원 : 5 색깔 : red
// 색깔은 Car클래스에서 red라고 써놨어서 그게 기본값이 되는것!
친구 차 모델 : 캐스퍼 탑승인원 : 4 색깔 : black
엄마 차 모델 : null 탑승인원 : 0 차량상태정상 : false
// 기본값 = String:null / int:0 / boolean:false
Car (라이브러리 클래스)
①필드
②생성자
1)기본 생성자
Car() {
//생성자에서 수행할 코드
System.out.println("Car 생성자가 호출됨");
}
③메소드
ClassMain (실행클래스)
=> 위에 기본 생성자로 Car()하고 Car 생성자가 호출됨을 불러냈기 때문에
new Car(); 부분마다 Car 생성자가 호출됨 이 다 출력된다!
Car(String m, int p, String c, String com) {
// 매개변수 : 현재 이 { } 구역에서 임시로 사용하는 생성자에 속한 지역변수
//모델명, 탑승인원, 색깔, 제조사
model = m; //필드변수로 값을 옮겨서 저장
passengerCount = p;
color = c;
company = com;
rightState = true;
cc = 0;
accKm = 0;}
③메소드
=> Car클래스(라이브러리 클래스)에서 Car(String m, int p, String c, String com) 이렇게 매개변수를 잡아줬으면
=> 실행클래스에서 Car myCar = new Car("파랑", 5, "레이", "기아"); 이런식으로 매개변수 순서대로 제대로 값을 써줘야한다!
: 클래스이름이 동일한 생성자 또는 메소드에 매개변수를 다르게 해서 선언하는..!
매개변수를 다르게 (갯수, 타입, 순서 차이)
매개변수를 보면 어느것을 실행해야 하는지 바로 구분이 되어야 함!
=>
Car(); Car(a); Car(a,b,c); Car(a,b,c,d); 이런식으로!
=>
Car(String, int, String);
Car(String, String, int); 이렇게도 가능!
Car(String model, int passengerCount, String color, String company) {
this.model = model;
this.passengerCount = passengerCount;
this.color = color;
this.company = company;
rightState = true;
this.cc = 0;
this.accKm = 20;
this.speed = 0;
}
Car(String model, int passengerCount, String company) {
this.model = model;
this.passengerCount = passengerCount;
this.company = company;
rightState = true;
color = null;
this.cc = 0;
this.accKm = 20;
this.speed = 0;
}
Car(String company, String model, int passengerCount) {
this.model = model;
this.passengerCount = passengerCount;
this.company = company;
rightState = true;
color = null;
this.cc = 0;
this.accKm = 20;
this.speed = 0;
}
Car(String company, String model) {
this.model = model;
his.passengerCount = 2; //탑승객수를 기본값으로 2
this.company = company;
rightState = true;
color = null;
this.cc = 0;
this.accKm = 20;
this.speed = 0;
}
Car(String model) {
this.model = model;
this.passengerCount = 2; //탑승객수를 기본값으로 2
this.company = null; // null
rightState = true;
color = null;
this.cc = 0;
this.accKm = 20;
this.speed = 0;
}
Car() {
this.model = null; //null
this.passengerCount = 2; //탑승객수를 기본값으로 2
this.company = null; //null
rightState = true;
color = null;
this.cc = 0;
this.accKm = 20;
this.speed = 0;
}
=> 오버로딩한 형태인데 안의 내용이 중복적일때
this(); 로 호출하면 자기 자신의 생성자를 호출할 수 있다!
=>
Car(String model, int passengerCount, String color, String company) {
this.model = model;
this.passengerCount = passengerCount;
this.color = color;
this.company = company;
rightState = true;
this.cc = 0;
this.accKm = 20;
this.speed = 0;
}
Car(String model, int passengerCount, String company) {
this(model, passengerCount, null, company); //new Car(model,passengerCount,color,company);
}
Car(String company, String model, int passengerCount) {
this(model, passengerCount, null, company); //new Car(model,passengerCount,color,company);
}
Car(String company, String model) {
this(model, 2, null, company); //new Car(model,passengerCount,color,company);
}
Car(String model) {
this(model, 2, null, null); //new Car(model,passengerCount,color,company);
}
Car() {
this(null, 2, null, null );
// 기본값
//new Car(model,passengerCount,color,company);
}
=> 매개변수 갯수 많은걸 기준으로 잡는다!
=> 따라서
Car(String model, int passengerCount, String color, String company) 를 기준으로 잡고
1)
Car(){
this(null, 2, null, null );
// 기본값!
}
Car(String model) {
this(model, 2, null, null);
}
Car(String company, String model) {
this(model, 2, null, company);
}
Car(String company, String model, int passengerCount) {
this(model, passengerCount, null, company);
}
이런식으로 자기자신의 생성자를 호출하면서 중복된내용을 줄여나갈 수 있다!
※이름짓는규칙※
① 소문자로 시작 ( $, _ 로도 시작 O)
//클래스:대문자, 변수,메소드:소문자
②숫자시작X 특수문자X
③단어만나면 카멜케이스 (ex)isPowerOn)
④의미있게 짓자! (최대한 동사로!) 동작하는 의미로
=> 마찬가지로 Calculator(라이브러리 클래스) 와 ClassMain(실행 클래스)를 만들어주고!
Calculator(라이브러리 클래스)
① 필드
String model; //모델명
int price; //가격
boolean isPowerOn; //전원켜짐 여부
② 생성자
Calculator(String model, int price) {
this.model = model;
this.price = price;
isPowerOn = false;
}
③ 메소드
int add(int n1, int n2) {
int result = n1 + n2;
return result;
}
// 메소드 오버로딩
double add(double n1, double n2) {
double result = n1 + n2;
return result;
}
System.out.println(myCal.add(1, 2));
System.out.println(myCal.add(1.0, 2.0));
System.out.println(myCal.add(12.22, 46.77));
=> 첫번째는 int형을 찾아갈거고 두번째,세번째는 double형을 찾아갈거다!
※주의※
double add(int,int) 이면 X
메소드이름 + 매개변수 가 같아야 한다!
Calculator(라이브러리 클래스)
① 필드
String model; //모델명
int price; //가격
boolean isPowerOn; //전원켜짐 여부
② 생성자
Calculator(String model, int price) {
this.model = model;
this.price = price;
isPowerOn = false;
}
③ 메소드
void powerOn() {
System.out.println("전원 켜짐.");
isPowerOn = true;
}
// void 는 마지막에 return을 안써줘도 되는!
// 근데 그냥 return; 써줘도 되긴함
// break;와 같은의미로 default 에 break 안쓰는것처럼 이것도 같은 이치!
boolean checkPowerOn() {
return isPowerOn;
}
ClassMain(실행 클래스)
Calculator myCal = new Calculator("전자계산기", 12000);
Calculator yourCal = new Calculator("최신계산기", 15000);
//새로운 객체 생성!
myCal.powerOn(); //myCal에 isPowerOn = true 켜줌!
System.out.println("myCal 전원 상태 : " + myCal.isPowerOn);
//myCal만 powerOn해서 true
System.out.println("yourCal 전원 상태 : " + yourCal.isPowerOn);
// false
=> 써논내용과 마찬가지로 myCal에만 isPowerOn=true 해줬기 때문에 myCal만 true
yourCal은 powerOn() 메소드 안타줬기때문에 생성자 처음에 만든대로 기본값 false!
if(yourCal.isPowerOn==true) { // 변수로 확인 (계산기가 켜져있는지)
int result = yourCal.add(30, 50);
System.out.println("최신계산기 더하기 결과 : " + result);
} else {
System.out.println("전원이 꺼져있습니다 전원부터 켜세요");
}
if(myCal.checkPowerOn()) { //메소드로 확인 (계산기가 켜져있는지)
double result2 = myCal.add(10, 20);
System.out.println(result2);
}
=> 변수로 확인 하는 경우는
yourCal이 isPowerOn이 true인지 아닌지 if절로 나눠서 true면 더하기를 해주고 false면 전원이 꺼져있습니다 전원부터 켜세요 라는 문구가 뜨게 한거고
=> 메소드로 확인하는 경우는
Calculator(라이브러리 클래스)에
boolean checkPowerOn() {
return isPowerOn;
}
메소드를 만들어줘서
if(myCal.checkPowerOn()) { //메소드로 확인 (계산기가 켜져있는지)
double result2 = myCal.add(10, 20);
System.out.println(result2);
}
=> 이미 myCal은 위에 myCal.powerOn();에서 isPowerOn=true;가 된 상태 이므로 true!!!