Spring/JAVA

Java Method

alsruds 2023. 9. 28. 22:49
๐Ÿฃ์ƒํ™œ์ฝ”๋”ฉ ๊ฐ•์˜๐Ÿฃ

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;
    }
}

 

๋ฉ”์†Œ๋“œ์˜ ์žฅ์ 

· ๊ด€๋ จ ํฌ์ŠคํŒ…

2023.09.25 - [Spring/์ƒํ™œ์ฝ”๋”ฉ - JAVA1] - ์•ฑ ๋งŒ๋“ค๊ธฐ 2 - ๋ฐฐ๋‹น๊ธˆ ๊ณ„์‚ฐ๊ธฐ (์ œ์–ด๋ฌธ, ๋ฐฐ์—ด, ๋ฉ”์†Œ๋“œ, ํด๋ž˜์Šค, ์ธ์Šคํ„ด์Šค)

 

์•ฑ ๋งŒ๋“ค๊ธฐ 2 - ๋ฐฐ๋‹น๊ธˆ ๊ณ„์‚ฐ๊ธฐ (์ œ์–ด๋ฌธ, ๋ฐฐ์—ด, ๋ฉ”์†Œ๋“œ, ํด๋ž˜์Šค, ์ธ์Šคํ„ด์Šค)

๐Ÿฃ์ƒํ™œ์ฝ”๋”ฉ ๊ฐ•์˜๐Ÿฃ JAVA1 - 14.6. ๋‚˜์˜ ์•ฑ ๋งŒ๋“ค๊ธฐ - ์กฐ๊ฑด๋ฌธ https://www.youtube.com/watch?v=y-qUWhanlkM&list=PLuHgQVnccGMAIluRRVsC1e79ri-dwnBmR&index=44 JAVA1 = 14.7. ๋‚˜์˜ ์•ฑ ๋งŒ๋“ค๊ธฐ - ๋ฐฐ์—ด https://www.youtube.com/watch?v=sMBu20ZZwzE&

alsrudalsrudalsrud.tistory.com

 

· ๋ฉ”์†Œ๋“œ ํ™œ์šฉํ•˜๊ธฐ

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("*");
    }
}