본문 바로가기

JAVA/되새김질

메서드 특징!? 아니 내가 몰랐던 것

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Product03{ 
    private String name; 
    private int price; 
    private int cnt;
 
 
 
    public Product03(String name, int price, int cnt) {  
        this.name = name; 
        this.price = price; 
        this.cnt = cnt; 
    }
 
    public int buyInfo() { 
        System.out.print(name+"\t"+price+"\t"+cnt+"\t"+(price*cnt)+"\n"); 
        return price*cnt; 
    }
 
}
 
public class A12_ObjVSMultiObjAns { 
    public static void main(String[] args) { 
 
        Product03 pr = new Product03(); 
        System.out.println(pr.buyInfo()); 
 
    } 
}
cs

 

buyInfo()메서드를 유심히 보자.

출력값이 2개다. print값과 return값.

이런거 처음봤다.

 

여태까지 메서드에 print가 있으면 return은 쓰면 안되고

반대로 return이 있으면 print를 쓰면 안되는 줄 알았다.

 

근데 이게 왠걸. 되네.

메인메서드에서 출력해보니까 둘다 나오네.

게다가 인트 메서드이니 더하고 빼고 할 수 있네.(리턴값만 영향받음) 신긔신긔 

이건 까먹은게 아니라 그냥 몰랐던 것.

근데 복습을 통해 알게됨 ㅎ_ㅎ ;; 결론은 복습최고