Spring/JAVA

Java ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ

alsruds 2023. 9. 29. 23:54
๐Ÿฃ์ƒํ™œ์ฝ”๋”ฉ ๊ฐ•์˜๐Ÿฃ

JAVA ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ
https://www.youtube.com/playlist?list=PLuHgQVnccGMAb-e41kXPSIpmoz1RvHyN4

 

  • Class : { Method & Variable }

 

๋‚จ์˜ ํด๋ž˜์Šค & ์ธ์Šคํ„ด์Šค ์‚ฌ์šฉํ•˜๊ธฐ

import java.io.FileWriter;
import java.io.IOException;

public class Main {
    public static void main (String[] args) throws IOException {

        // Math class ์‚ฌ์šฉํ•˜๊ธฐ
        System.out.println(Math.PI); //3.141592653589793
        System.out.println(Math.floor(1.5)); //1.0
        System.out.println(Math.ceil(1.5)); //2.0

        // class ์˜ ๋ณต์ œ๋ณธ์„ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•˜๊ธฐ
        FileWriter f1 = new FileWriter("data.txt");
        f1.write("Hello");
        f1.write(" Java");
        f1.close();
    }
}

· class : System, Math, FileWriter

· instance : f1

 

๋ณ€์ˆ˜ & ๋ฉ”์†Œ๋“œ์— ํด๋ž˜์Šค ์ ์šฉํ•˜๊ธฐ

โ˜‘๏ธ class Print ๋„์ž… ์ „

public class Main {
    public static String delimiter = "";
    public static void main(String[] args) {

        delimiter = "---";
        printA();

        delimiter = "***";
        printB();
    }

    public static void A() {
        System.out.println(delimiter);
        System.out.println("A");
    }

    public static void B() {
        System.out.println(delimiter);
        System.out.println("B");
    }
}

 

โœ… class Print ๋„์ž… ํ›„

class Print{
    public static String delimiter = "";
    public static void A() {
        System.out.println(delimiter);
        System.out.println("A");
    }

    public static void B() {
        System.out.println(delimiter);
        System.out.println("B");
    }
}
public class Main {
    public static void main(String[] args) {

        Print.delimiter = "---";
        Print.A();

        Print.delimiter = "***";
        Print.B();
    }
}

 

ํด๋ž˜์Šค์— ์ธ์Šคํ„ด์Šค ์ ์šฉํ•˜๊ธฐ (+ ์ƒ์„ฑ์ž)

· instance = class ์˜ ๋ณต์ œ๋ณธ

· ์ƒ์„ฑ์ž (constructor) : ์ดˆ๊ธฐ์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰

 

class Print {
    public String delimiter = "";
    public Print(String delimiter) { // ์ƒ์„ฑ์ž
        this.delimiter = delimiter;
    }
    public void A() {
        System.out.println(this.delimiter);
        System.out.println("A");
    }
    public void B() {
        System.out.println(this.delimiter);
        System.out.println("B");
    }
}
public class Main {
    public static void main(String[] args) {
        Print p1 = new Print("===");
        Print p2 = new Print("^^^");

        p1.A();
        p2.A();
        p1.B();
        p2.B();
    }
}

 

๋ฉ”์†Œ๋“œ ํ™œ์šฉ ์˜ˆ์ œ์— ์ธ์Šคํ„ด์Šค ๋„์ž…ํ•˜๊ธฐ (= ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šคํ™”)

· ์ด์ „ ํฌ์ŠคํŒ…

2023.09.28 - [Spring/์ƒํ™œ์ฝ”๋”ฉ - JAVA1] - Java Method

 

Java Method

๐Ÿฃ์ƒํ™œ์ฝ”๋”ฉ ๊ฐ•์˜๐Ÿฃ JAVA method https://www.youtube.com/playlist?list=PLuHgQVnccGMDkdt3N9tT0aZPZx5-Ri2jo java method : ์—ฐ๊ด€๋œ ์ฝ”๋“œ๋ฅผ ๋ชจ์•„ ์ •๋ฆฌํ•˜๊ธฐ (โ‰’ function) ์ด๋ฏธ ์‚ฌ์šฉํ•ด๋ณธ ๋ฉ”์†Œ๋“œ public class Main { public static void main(

alsrudalsrudalsrud.tistory.com

 

class Accounting {
    public double valueOfSupply;
    public static double vatRate = 0.1; // ๊ณ ์ •๋œ ๊ฐ’์€ static ์œผ๋กœ ์ง€์ •
    public Accounting(double valueOfSupply) {
        this.valueOfSupply = valueOfSupply;
    }
    public double getVAT() {
        return valueOfSupply * vatRate;
    }
    public double getTotal() {
        return valueOfSupply * getVAT();
    }
}
public class Main {
    public static void main(String[] args) {

        Accounting acc = new Accounting(10000.0);
        Accounting acc2 = new Accounting(20000.0);

        System.out.println("Value Of Supply = " + acc.valueOfSupply);
        System.out.println("Value Of Supply = " + acc2.valueOfSupply);

        System.out.println("VAT = " + acc.getVAT());
        System.out.println("VAT = " + acc2.getVAT());

        System.out.println("Total = " + acc.getTotal());
        System.out.println("Total = " + acc2.getTotal());
    }
}