ποΈ Backend/Java
μλ°(Java) - κ°μ²΄ μ§ν₯ νλ‘κ·Έλλ° (2) λ©μλ (μμ )
kongmi
2023. 1. 10. 20:05
ν΄λμ€ λ΄μ μμ±μκ° μμΌλ©΄ κΈ°λ³Έ μμ±μκ° νΈμΆλ¨
Sample sample = new Sample();
package λ©μλμ°μ΅0110;
import java.util.Scanner;
public class ClassMethod {
public static void main(String[] args) {
Sample sample = new Sample();
Scanner sc = new Scanner(System.in);
System.out.print("μ μκ° μ
λ ₯ : ");
int n1 = sc.nextInt();
System.out.print("μ μκ° μ
λ ₯ : ");
int n2 = sc.nextInt();
int rst = sample.sum(n1, n2); // sum()μ λ§€κ°λ³μμ λ°νκ°μ΄ μλ λ©μλ
System.out.println(rst);
sample.say();
System.out.println(sample.sayStr());
}
}
System.out.print(μΆλ ₯ν¨μ)λ λ°νκ°μ΄ μμ΄μΌ μΆλ ₯ κ°λ₯ν¨.
λ°νκ° μμ κ²½μ° λ©μλλ§ μ¨λ λ¨.
void say() {
System.out.println("μλ νμΈμ");
}
sample.say();
return κ°μ StringμΌλ‘ λ°ννκΈ° λλ¬Έμ System.out.print(μΆλ ₯ν¨μ) μ¬μ© κ°λ₯
String sayStr() {
return "μλ νμΈμ";
}
System.out.println(sample.sayStr());
package λ©μλμ°μ΅0110;
public class Sample {
int sum(int a, int b) {
return a + b;
}
// λ§€κ°λ³μλ μκ³ λ°νκ°λ μλ λ©μλ
void say() {
System.out.print("μλ
νμΈμ");
}
String sayStr() {
return "μλ
νμΈμ";
}
}