๐ฃ์ํ์ฝ๋ฉ ๊ฐ์๐ฃ
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
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());
}
}
'Spring > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Java Interface (0) | 2023.10.03 |
---|---|
Java ์์ (0) | 2023.10.02 |
Java Method (0) | 2023.09.28 |
Java ์์ธ (Try-Catch / Try-Catch-Finally / Try with Resource Statements / Throws) (0) | 2023.09.26 |
์ฑ ๋ง๋ค๊ธฐ 2 - ๋ฐฐ๋น๊ธ ๊ณ์ฐ๊ธฐ (์ ์ด๋ฌธ, ๋ฐฐ์ด, ๋ฉ์๋, ํด๋์ค, ์ธ์คํด์ค) (0) | 2023.09.25 |