๐ฃ์ํ์ฝ๋ฉ ๊ฐ์๐ฃ
JAVA method
https://www.youtube.com/playlist?list=PLuHgQVnccGMDkdt3N9tT0aZPZx5-Ri2jo
- java method : ์ฐ๊ด๋ ์ฝ๋๋ฅผ ๋ชจ์ ์ ๋ฆฌํ๊ธฐ (โ function)
์ด๋ฏธ ์ฌ์ฉํด๋ณธ ๋ฉ์๋
public class Main {
public static void main(String[] args) {
// main : method
System.out.println("Hello Method"); // println : ์
๋ ฅ๊ฐ ์ถ๋ ฅํ๊ธฐ
System.out.println(Math.floor(1.1)); // Math.floor : ์
๋ ฅ๊ฐ์ ๋ด๋ฆผํ๊ธฐ
}
}
๋ฉ์๋์ ๊ธฐ๋ณธ ํ์
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
// ๋ฉ์๋ ์ฌ์ฉํ๊ธฐ
printTwoTimes("a", "-"); // ์ธ์ (argument)
printTwoTimes("b", "*");
writeFileTwoTimes("c", "&");
// ๋ฆฌํด ๊ฐ ํ์ฉํ๊ธฐ 1 : printTwoTimes ์ ๊ฐ์ ์ฝ๋
System.out.println(twoTimes("a", "-"));
// ๋ฆฌํด ๊ฐ ํ์ฉํ๊ธฐ 2 : writeFileTwoTimes ์ ๊ฐ์ ์ฝ๋
FileWriter fw = new FileWriter("out.txt");
fw.write(twoTimes("e", "+"));
fw.close();
}
// ๋ฉ์๋ ์ ์ํ๊ธฐ - ์
๋ ฅ๊ฐ์ ๋ฐ๋ผ ๋ค๋ฅด๊ฒ ๋์
// Refactor -> Extract Method
public static void printTwoTimes(String text, String delimiter) { // ๋งค๊ฐ๋ณ์ (parameter)
System.out.println(delimiter);
System.out.println(text);
System.out.println(text);
}
public static void writeFileTwoTimes(String text, String delimiter) throws IOException {
FileWriter fw = new FileWriter("output.txt");
fw.write(delimiter+"\n");
fw.write(text+"\n");
fw.write(text+"\n");
fw.close();
}
public static String twoTimes(String text, String delimiter) {
String out = "";
out = out + delimiter + "\n";
out = out + text + "\n";
out = out + text + "\n";
return out;
}
}
๋ฉ์๋์ ์ฅ์
· ๊ด๋ จ ํฌ์คํ
· ๋ฉ์๋ ํ์ฉํ๊ธฐ
public class Main {
// ๊ณต๊ธ๊ฐ์ก
public static double valueOfSupply = 10000.0;
// ๋ถ๊ฐ๊ฐ์น์ธ์จ
public static double vatRate = 0.1;
public static void main(String[] args) {
System.out.println("Value of supply : " + valueOfSupply);
System.out.println("VAT : " + getVAT()); // ๋ถ๊ฐ์ธ
System.out.println("Total : " + getTotal()); // ํฉ๊ณ
}
public static double getVAT() {
return valueOfSupply * vatRate;
}
public static double getTotal() {
return valueOfSupply + getVAT();
}
}
→ ์ฝ๋ ๊ฐ๋ ์ฑ ํฅ์
→ ์ฌ์ฌ์ฉ์ฑ
→ ์ ์ง๋ณด์ ์ฉ์ด
Public
class Greeting {
// public ์ผ๋ก ์ ์ธํด์ผ class Main ์์ ์ฌ์ฉ ๊ฐ๋ฅ
public static void hi() {
System.out.println("HI");
}
}
public class Main {
// public, protected, default, private
// private -> ๋ด๋ถ์ ๋ฉ์๋
public static void main(String[] args) {
Greeting.hi();
}
}
Static
· static - class method
· no static - instance method
class Print {
// ์ธ์คํด์ค์ ๋ฉ์๋๋ก ์ฌ์ฉํ๊ธฐ ์ํด static ์ญ์ ํ๊ธฐ
public String delimiter;
public void a() {
System.out.println(this.delimiter);
System.out.println("a");
System.out.println("a");
}
public void b() {
System.out.println(this.delimiter);
System.out.println("b");
System.out.println("b");
}
// class Print ์ ๋ฉ์๋๋ก ์ฌ์ฉํ๊ธฐ ์ํด static ์ฌ์ฉํ๊ธฐ
public static void c(String delimiter) {
System.out.println(delimiter);
System.out.println("c");
System.out.println("c");
}
}
public class Main {
public static void main(String[] args) {
// instance
Print t1 = new Print();
t1.delimiter = "-";
t1.a();
t1.b();
// class
Print.c("*");
}
}
'Spring > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Java ์์ (0) | 2023.10.02 |
---|---|
Java ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ (2) | 2023.09.29 |
Java ์์ธ (Try-Catch / Try-Catch-Finally / Try with Resource Statements / Throws) (0) | 2023.09.26 |
์ฑ ๋ง๋ค๊ธฐ 2 - ๋ฐฐ๋น๊ธ ๊ณ์ฐ๊ธฐ (์ ์ด๋ฌธ, ๋ฐฐ์ด, ๋ฉ์๋, ํด๋์ค, ์ธ์คํด์ค) (0) | 2023.09.25 |
์ฑ ๋ง๋ค๊ธฐ 1 - ๋ฐฐ๋น๊ธ ๊ณ์ฐ๊ธฐ (๋ณ์, ์ ๋ ฅ๊ฐ) (0) | 2023.09.22 |