1) 사용자 메소드 정의
public class Main {
public static void main(String[] args) {
myClass mc = new myClass();
mc.myFunction();
}
}
class myClass{
void myFunction(){
System.out.println("new Function");
}
}
2)
public class Main {
public static void main(String[] args) {
System.out.println("square 3: " + square(3));
System.out.println("square 4: " + square(4));
System.out.println("square 5: " + square(5));
int result = add(4,5);
System.out.println("add 4 + 5 = " + result);
int result2 = square(7);
System.out.println("square 7: " + result2);
}
public static int square(int number){
return number*number;
}
public static int add(int a, int b){
return a + b;
}
}
3) 클래스 string 메소드
public class StringExam {
public static void main(String[] args) {
String str1 = "안녕하세요. ";
String str2 = "벌써 여기까지 오셨네요. 끝까지 화이팅!!";
String concatResult;
String substringResult;
// 아래쪽에 코드를 작성하세요.
concatResult = str1.concat(str2);
substringResult = str1.substring(2);
// 이 아래는 정답 확인을 위한 코드입니다. 수정하지 마세요.
System.out.println(concatResult);
System.out.println(substringResult);
}
}
'개발이 좋아서 > Java가 좋아서' 카테고리의 다른 글
this예약어 (0) | 2023.05.02 |
---|---|
접근제어자(private/getter/setter) (0) | 2023.05.02 |
생성자 (0) | 2023.05.02 |
인스턴스 (0) | 2023.05.02 |
클래스 (0) | 2023.05.02 |