java

[JAVA] 리턴형 있는 메서드와 리턴형 없는 메서드

바이홍 2008. 3. 21. 18:17
반응형
//생성자//
리터형이 없는 메서드
class Test{
    public static void main(String args[]){
        Child ch=new Child("똘이",6);
        Child ch1=new Child("순이");
        ch.gotoSchool();            
        ch.getCandy();
        ch1.gotoSchool();
        ch1.getCandy();
    }
}
class Child{
    String s;
    int i;
    Child(String s, int i){
        this.s=s;
        this.i=i;
    }
    Child(String s){
        this.s=s;
    }
    void gotoSchool(){
        System.out.println(i+"학년인"+s+"가 학교에간다");
    }
    void getCandy(){
        System.out.println(i+"학년인"+s+"가 사탕을 먹는다");
    }
    
}


-----------------------------------------------
리턴형이 있는 메서드
class Test{
    public static void main(String args[]){
        Child ch=new Child("똘이",6);
        Child ch1=new Child("순이");
        
        String str=ch.gotoSchool();            
        String str1=ch.getCandy();
        String str2=ch1.gotoSchool();
        String str3=ch1.getCandy();
            System.out.println(str);
            System.out.println(str1);
            System.out.println(str2);
            System.out.println(str3);
    }
}
class Child{
    String s;
    int i;
    Child(String s, int i){
        this.s=s;
        this.i=i;
    }
    Child(String s){
        this.s=s;
    }
    String gotoSchool(){
        return i+"학년인"+s+"가 학교에 간다";
    }
    String getCandy(){
        return i+"학년인"+s+"가 사탕을 먹는다";
    }
    
}
 
반응형