리턴타입 메소드이름(매개변수, 매개변수, ...) {
실행코드;
retune 값 or 변수;
}
void noReturn() {
}
int yesReturn() {
return 0;
}
Temp tt = new Temp();
tt.noReturn();
//int result = tt.noReturn(); //리턴타입이 없으니까 저장할수가 없다!
//System.out.println(tt.noReturn()); //리턴값이 없으면 실행도 x
//if (tt.noReturn() > 0) { // if도 사용 x
//}
=> 리턴타입이 없으면
따로 변수 지정해서 저장할 수 없고
리턴값이 없으니까 출력도 할 수 없고
값이 없으니까
void noReturn() {
}
int yesReturn() {
return 0;
}
Temp tt = new Temp();
tt.yesReturn();
int result = tt.noReturn();
//-> 리턴타입이 있으니까 result로 저장해서 이후에 사용할수 있다!
//if (result == 0)
리턴타입 메소드이름(매개변수, 매개변수, ...) {
실행코드;
retune 값 or 변수;
}
//필드
int inch;
String company;
String model;
//생성자
Monitor(String company, String model, int inch) {
this.company = company;
this.model = model;
this.inch = inch;
}
//메소드
void printInfo(String company, String model, int inch) {
System.out.printf("제조사:%s 모델명:%s 인치:%d\n", company, model, inch);
}
Monitor m1 = new Monitor("삼성", "더프레임", 65);
Monitor m2 = new Monitor("LG", "올레드", 80);
m1.printInfo("기아", "몰라", 500);
m2.printInfo("현대", "몰라", 999);
=> 출력
제조사:기아 모델명:몰라 인치:500
제조사:현대 모델명:몰라 인치:999
=>
void printInfo(String company, String model, int inch) {
System.out.printf("제조사:%s 모델명:%s 인치:%d\n", company, model, inch);
}
이렇게 매개변수 있게 작성 하면 외부에서
m1.printInfo("기아", "몰라", 500);
m2.printInfo("현대", "몰라", 999);
이렇게 쓰면 출력이 저대로 되버린다 ㅠㅠ
//필드
int inch;
String company;
String model;
//생성자
Monitor(String company, String model, int inch) {
this.company = company;
this.model = model;
this.inch = inch;
}
//메소드
void printInfo() {
System.out.printf("제조사:%s 모델명:%s 인치:%d\n", company, model, inch);
}
Monitor m1 = new Monitor("삼성", "더프레임", 65);
Monitor m2 = new Monitor("LG", "올레드", 80);
m1.printInfo();
m2.printInfo();
=> 출력
제조사:삼성 모델명:더프레임 인치:65
제조사:LG 모델명:올레드 인치:80
=>
void printInfo() {
System.out.printf("제조사:%s 모델명:%s 인치:%d\n", company, model, inch);
}
매개변수 없게 작성하면 이미 내부에서 정보입력한대로 나오게 된다!
※주의※
void printInfo(String company, String model, int inch)
=> 외부에서 입력하는대로 출력이 되는거고
void printInfo()
=> 내부에서 입력한대로 출력이 되는거고!