Ⅱ. 남의 클래스 & 남의 인스턴스
package projectOOP;
import java.io.FileWriter;
import java.io.IOException;
public class OthersOOP {
public static void main(String[] args) throws IOException {
System.out.println(Math.PI); // 3.14웅앵웅
System.out.println(Math.floor(1.8)); // 내림
System.out.println(Math.ceil(1.8)); // 올림
FileWriter f1 = new FileWriter("data.txt");
f1.write("Hello");
f1.write(" Java");
f1.close();
}
}
Ⅲ. 변수와 메소드
package projectOOP;
public class MyOOP {
public static void main(String[] args) {
delimiter = "---";
printA();
printB();
delimiter = "***";
printA();
printB();
}
public static String delimiter = "";
public static void printA() {
System.out.print(delimiter);
System.out.print("A");
System.out.println("A");
}
public static void printB() {
System.out.print(delimiter);
System.out.print("B");
System.out.println("B");
}
}
Ⅳ. 클래스
public class MyOOP {
public static void main(String[] args) {
Print.delimiter = "---";
Print.A();
Print.B();
Print.delimiter = "***";
Print.A();
Print.B();
}
}
class Print {
public static String delimiter = "";
public static void A() {
System.out.println(delimiter);
System.out.println("A");
System.out.println("A");
}
public static void B() {
System.out.println(delimiter);
System.out.println("B");
System.out.println("B");
}
}
Ⅴ. 인스턴스
package projectOOP;
public class MyOOP {
public static void main(String[] args) {
Print p1 = new Print();
p1.delimiter = "---";
p1.A();
p1.B();
Print p2 = new Print();
p2.delimiter = "***";
p2.A();
p2.B();
}
}
package projectOOP;
class Print {
public static String delimiter = "";
public void A() {
System.out.print(delimiter);
System.out.print("A");
System.out.println("A");
}
public void B() {
System.out.print(delimiter);
System.out.print("B");
System.out.println("B");
}
}
Ⅵ. static
package projectOOP;
class Foo {
public static String classVar = "I class var";
public String instanceVar = "I instance var";
public static void classMethod() {
System.out.println(classVar); // 접근 가능
// System.out.println(instanceVar); // 접근 불가
}
public void instanceMethod() {
System.out.println(classVar); // 접근 가능
System.out.println(instanceVar); // 접근 가능
}
}
public class StaticApp {
public static void main(String[] args) {
System.out.println(Foo.classVar); // 접근 가능
// System.out.println(Foo.instanceVar); // 접근 불가
Foo.classMethod(); // 접근 가능
// Foo.instanceMethod(); // 접근 불가
Foo f1 = new Foo();
Foo f2 = new Foo();
System.out.println(f1.classVar); // I class var
System.out.println(f1.instanceVar); // I instance var
f1.classVar = "changed by f1";
System.out.println(Foo.classVar); // changed by f1
System.out.println(f2.classVar); // changed by f1
f1.instanceVar = "changed by f1";
System.out.println(f1.instanceVar); // changed by f1
System.out.println(f2.instanceVar); // I instance var
}
}
Ⅶ. 생성자와 this
package projectOOP;
public class MyOOP {
public static void main(String[] args) {
Print p1 = new Print("---");
// p1.A();
// p1.B();
//
// Print p2 = new Print("***");
// p2.A();
// p2.B();
}
}
package projectOOP;
class Print { // Print 클래스 파일로 따로 분리함.
public static String delimiter = "";
public Print(String delimiter) {
this.delimiter = delimiter;
}
public void A() {
System.out.print(this.delimiter);
System.out.print("A");
System.out.println("A");
}
public void B() {
System.out.print(this.delimiter);
System.out.print("B");
System.out.println("B");
}
}
Ⅷ. 활용
1. 클래스화
package projectMethod;
class Accounting { // 클래스로 연관된 메소드끼리 정리정돈.
public static double valueOfSupply = 10000.0;
public static double vatRate = 0.1;
public static double getVAT() {
return valueOfSupply * vatRate;
}
public static double getTotal() {
return valueOfSupply + getVAT();
}
}
public class AccountingApp {
public static void main(String[] args) {
System.out.println("Value of supply : " + Accounting.valueOfSupply);
System.out.println("VAT : " + Accounting.getVAT());
System.out.println("Total : " + Accounting.getTotal());
}
}
2. 인스턴스화
package projectMethod;
class Accounting { // 클래스로 연관된 메소드끼리 정리정돈.
public double valueOfSupply;
public static double vatRate = 0.1;
public Accounting(double valueOfSupply) {
this.valueOfSupply = valueOfSupply;
}
public double getVAT() {
return valueOfSupply * vatRate;
}
public double getTotal() {
return valueOfSupply + getVAT();
}
}
public class AccountingApp {
public static void main(String[] args) {
Accounting a1 = new Accounting(10000.0);
Accounting a2 = new Accounting(20000.0);
System.out.println("Value of supply : " + a1.valueOfSupply);
System.out.println("Value of supply : " + a2.valueOfSupply);
System.out.println("VAT : " + a1.getVAT());
System.out.println("VAT : " + a2.getVAT());
System.out.println("Total : " + a1.getTotal());
System.out.println("Total : " + a2.getTotal());
}
}
출처 :
반응형
'개발(Web) > Web' 카테고리의 다른 글
[Java] 생활코딩 JAVA 상속 (0) | 2021.01.01 |
---|---|
[Java] 생활코딩 JAVA 예외 (0) | 2020.12.30 |
[Java] 생활코딩 JAVA method (0) | 2020.12.28 |
[Java] 접근 제어자 (0) | 2020.12.28 |
[Eclipse] Code Recommenders cannot download its model repository index. (0) | 2020.12.18 |