π λΆνμ ν΄λΉνλ κ°μ²΄λ€μ λ¨Όμ λ§λ€μ΄μ μ΄λ€μ 쑰립νμ¬ νλμ νλ‘κ·Έλ¨μ μμ±μν€λ κ²
물리μ μΌλ‘ μ‘΄μ¬νλ κ²κ³Ό μΆμμ μΌλ‘ μκ°ν μ μλ κ² μ€μμ μμ±κ³Ό λμμ κ°μ§λ λͺ¨λ κ²μ΄ κ°μ²΄!
νλ(μμ±)μ λ©μλ(λμ)λ‘ κ΅¬μ±λ μλ° κ°μ²΄λ‘ λͺ¨λΈλ§μ΄ κ°λ₯
κ°μ²΄λΌλ¦¬ μλ‘μ κΈ°λ₯(λμ)μ μ΄μ©νκ³ λ°μ΄ν°λ₯Ό μ£Όκ³ λ°μ π κ°μ²΄μ μνΈ μμ©
κ°μ²΄λ μνΈ μμ© μλ¨μΌλ‘ λ©μλλ₯Ό νΈμΆνλ€.
// λ©μλ νΈμΆ νν
리ν΄κ° = κ°μ²΄λͺ
.λ©μλλͺ
(λ§€κ°κ°1, λ§€κ°κ°2, ...);
μΊ‘μν
κ°μ²΄μ νλμ λ©μλλ₯Ό νλλ‘ λ¬Άμ΄μ ꡬν λ΄μ©μ κ°μΆλ κ². μΈλΆ κ°μ²΄λ κ°μ²΄ λ΄λΆμ ꡬ쑰λ₯Ό μ μ μμΌλ©° ν΄λΉ κ°μ²΄μ νλμ λ©μλλ§ μ΄μ© κ°λ₯νλ€. μΊ‘μννμ¬ λ³΄νΈνλ μ΄μ λ μΈλΆμ μλͺ»λ μ¬μ©μΌλ‘ κ°μ²΄κ° μμλλ κ²μ λ°©μ§νκΈ° μν¨! μΊ‘μνλ₯Ό μν΄ μλ°λ μ κ·Ό μ νμ(Access Modifier)λ₯Ό μ¬μ©νλ€.
μμ
μμ κ°μ²΄μ νλμ λ©μλλ₯Ό νμ κ°μ²΄μ λ¬Όλ €μ£Όλ κ². νμ κ°μ²΄λ μμ κ°μ²΄λ₯Ό νμ₯ν΄μ μΆκ°μ μΈ νλμ λ©μλλ₯Ό κ°μ§ μ μλ€.
μ₯μ : μ€κ³ μκ° λ¨μΆ, μ€λ³΅ μ½λ κ°μ, μ μ§ λ³΄μ μκ° μ΅μν!
λ€νμ±
κ°μ νμ μ΄μ§λ§ μ€ν κ²°κ³Όκ° λ€μν κ°μ²΄λ₯Ό λμ ν μ μλ μ±μ§. μλ°λ μΈν°νμ΄μ€λ λΆλͺ¨ ν΄λμ€μ νμ λ³νμ΄ κ°λ₯νλ€.
// μμ
class Account {
String account;
String name;
int balance;
public void deposit(int money) {
if (money>0) {
balance += money;
}
}
public void withdrawal(int money) {
if (balance>=money) {
balance -= money;
} else {
System.out.println("μμ‘μ΄ λΆμ‘±ν©λλ€");
}
}
public String info() {
return "κ³μ’λ²νΈ: " + account + ", μ΄λ¦: " + name + ", μμ‘: " + balance;
}
}
public class AccountTest {
public static void main(String[] args) {
// Account κ°μ²΄ 2κ° λ§λ€κΈ°
Account acc1 = new Account();
Account acc2 = new Account();
// 101, νκΈΈλ, 100000
acc1.account = "101";
acc1.name = "νκΈΈλ";
acc1.balance = 100000;
// 102, κΉκΈΈλ, 200000
acc2.account = "102";
acc2.name = "κΉκΈΈλ";
acc2.balance = 200000;
// λ κ³μ’ μ 보 μΆλ ₯
System.out.println(acc1.info());
System.out.println(acc2.info());
acc1.deposit(10000);
acc2.withdrawal(20000);
System.out.println(acc1.info());
System.out.println(acc2.info());
}
}
νλ
μμ±μ
// μμ±μ μμ
class MyClass {
int num;
public MyClass() {} // κΈ°λ³Έ μμ±μ
public MyClass(int num) { // λ§€κ°λ³μκ° μλ μμ±μλ₯Ό λ§λ€λ©΄ κΈ°λ³Έ μμ±μκ° μλ μμ±λμ§ μλλ€. λ°λμ κΈ°λ³Έ μμ±μ μ½λ©ν΄μΌ ν¨.
this.num = num;
}
}
public class ConstructorTest {
public static void main(String[] args) {
MyClass mc = new MyClass();
}
}
κ°μ²΄ μμ± μ μ΄κΈ°ν μν μ λ΄λΉ(μ€μ²΄λ λ©λͺ¨λ¦¬)
new μ°μ°μλ‘ μμ±μκ° μ±κ³΅μ μΌλ‘ μ€νλλ©΄ ν μμμ κ°μ²΄κ° μμ±λκ³ κ°μ²΄μ μ£Όμκ° λ¦¬ν΄λλ€.
λͺ¨λ ν΄λμ€λ μμ±μκ° λ°λμ μ‘΄μ¬νλ©°, νλ μ΄μμ κ°μ§ μ μλ€.
λ©μλμ λΉμ·ν λͺ¨μμ΄μ§λ§ λ¦¬ν΄ νμ μ΄ μκ³ ν΄λμ€ μ΄λ¦κ³Ό λμΌ
ν΄λμ€μ μμ±μκ° λͺ μμ μΌλ‘ μ μΈλμ΄ μλ κ²½μ°μλ λ°λμ μ μΈλ μμ±μλ₯Ό νΈμΆν΄μ κ°μ²΄ μμ±ν΄μΌ νλ€. (κΈ°λ³Έ μμ±μλ νΈμΆ λΆκ°!)
π μμ±μ μ€λ²λ‘λ©(Overloading)
π λ§€κ° λ³μμ νμ or κ°―μκ° λ€λ₯Έ μμ±μλ₯Ό μ μΈνλ κ²
- μμ±μ μ€λ²λ‘λ©μ΄ λ§μμ§λ©΄ μμ±μ κ°μ μ€λ³΅λ μ½λκ° λ°μν μ μλ€!
this()
λ μμ μ λ€λ₯Έ μμ±μλ₯Ό νΈμΆνλ κ²μΌλ‘ λ°λμ μμ±μ 첫μ€μμλ§ νμ©!public class Person { int age; String name; // λ©μλλ§λ€ Person thisκ° μλμΌλ‘ λ€μ΄κ° μλ€. ex) public Person(Person this) public Person() { // μμ±μ: new μ°μ°μΌλ‘ μλ νΈμΆλλ ν¨μ this("none", 0); // this(): λ λ€λ₯Έ μμ±μ νΈμΆ, μμ±μμμλ§ νΈμΆ κ°λ₯ } public Person(String name, int age) { // μμ±μλ λ§€κ°λ³μμ κ°―μλ νμ μ΄ λ€λ₯΄λ©΄ μ¬λ¬ κ° μ¬ μ μλ€(Overload) this.name = name; // thisλ ν΄λμ€ this.age = age; } // public Person(String id, int age) { // // μ€λ²λ‘λ μ±λ¦½νμ§ μμ. λ§€κ°λ³μ νμ κ³Ό κ°―μκ° κ°μ κ²μ΄ μμ΄μ // } public Person(String name) { // nameλ§ λ³κ²½ this(name, 0); } // public Person(String id) { // // μ€λ²λ‘λ μ±λ¦½X λ§€κ°λ³μ νμ κ³Ό κ°―μκ° κ°μ κ²μ΄ μμ΄μ // } public Person(int age) { // ageλ§ λ³κ²½ this("none", age); } public String info() { return "μ΄λ¦: " + name + ", λμ΄:" + age; // this.name, this.ageλΌκ³ νλ©΄ λ λͺ ννμ§λ§ this μλ΅ν΄λ κ΄μΆ } }
public class PersonExample { public static void main(String[] args) { Person p = new Person(); // Personμ λ°μ΄ν°νμ (=μλ£ν), pλ heapμμμ κ°λ¦¬ν€λ μ£Όμ p.age = 20; p.name = "νμ§λ―Ό"; System.out.println(p.info()); Person p2 = new Person(); // Person()μ μμ±μ, μμ±μλ ν΄λμ€λͺ κ³Ό λμΌνκ³ ν¨μμ 리ν΄μ λΆμ΄μ§ μμ p2.age = 30; p2.name = "νμμ¬"; System.out.println(p2.info()); Person p3 = new Person("μ΅μ°μ", 43); // Person ν΄λμ€ μμ nameκ³Ό age λ£μ μ μκ² λμ΄ μμ΄μ μ½λ κΈΈμ΄κ° μ€μ΄λ¦ System.out.println(p3.info()); Person p4 = new Person(); System.out.println(p4.info()); Person p5 = new Person("μ΄μμ§"); System.out.println(p5.info()); Person p6 = new Person(32); System.out.println(p6.info()); } }
λ©μλ
// λ©μλ μ μΈ λ° λ©μλ νΈμΆ μμ
class Calculator {
int val1;
int val2;
public Calculator(int val1, int val2) {
this.val1 = val1;
this.val2 = val2;
}
public int add() {
int sum = val1 + val2;
return sum;
}
public int sub() {
return val1 - val2;
}
public int mul() {
return val1 * val2;
}
public double div() {
return val2==0? 0:(double)val1/val2;
}
}
public class CalculatorTest {
public static void main(String[] args) {
Calculator c1 = new Calculator(30, 10);
System.out.println(c1.add()); // 30
System.out.println(c1.sub()); // 10
System.out.println(c1.mul()); // 200
System.out.println(c1.div()); // 2
}
}
// μΈμ€ν΄μ€ λ©€λ² μ¬μ© μμ - μΈλΆ ν΄λμ€μμλ new μ°μ°μλ‘ κ°μ²΄λ₯Ό μμ±ν΄μ μ¬μ©
class Calculator2 {
public int add(int x, int y) {
return x+y;
}
public int sub(int x, int y) {
return x-y;
}
public int mul(int x, int y) {
return x*y;
}
public double div(int x, int y) {
return y==0? y: (double) x/y;
}
}
public class CalculatorTest2 {
public static void main(String[] args) {
Calculator2 c2 = new Calculator2(); // new μ°μ°μλ‘ κ°μ²΄ μμ±
System.out.println(Calculator2.add(10, 20));
System.out.println(Calculator2.sub(40, 5));
System.out.println(Calculator2.mul(42, 9));
System.out.println(Calculator2.div(9, 3));
}
}
// staticμΌλ‘ νμ©νλ μμ
class Calculator2 {
// staticμ΄ λΆμ΄ μλ λ©μλλ μΈμ€ν΄μ€νλμ΄μ μΈλΆμμ new μ°μ°μ νμμμ
public static int add(int x, int y) {
return x+y;
}
public static int sub(int x, int y) {
return x-y;
}
public static int mul(int x, int y) {
return x*y;
}
public static double div(int x, int y) {
return y==0? y: (double) x/y;
}
}
public class CalculatorTest2 {
public static void main(String[] args) {
// Calculator2 c2 = new Calculator2(); // staticμ΄ μμΌλ©΄ νμμμ
System.out.println(Calculator2.add(10, 20));
System.out.println(Calculator2.sub(40, 5));
System.out.println(Calculator2.mul(42, 9));
System.out.println(Calculator2.div(9, 3));
}
}
// static νμ© μμ2
class Product {
int serialNum = 0;
static int snum = 100;
public Product() {
// serialNum++;
snum++;
serialNum = snum; // μ μΌν΄μΌ νλ κ²½μ°, static λ³μλ₯Ό νμ©ν μ μμ ex) νλ², κ΅μ§μλ²νΈ λ±
System.out.println("serialNum: " + serialNum + ", snum: " + snum);
}
}
public class StaticTest {
public static void main(String[] args) {
Product[] prs = new Product[5];
for (int i=0; i<prs.length; i++) {
prs[i] = new Product();
}
}
}
// result
// serialNum: 1, snum: 101
// serialNum: 1, snum: 102
// serialNum: 1, snum: 103
// serialNum: 1, snum: 104
// serialNum: 1, snum: 105
// serialNumμ κ³μ κ°μ²΄κ° μλ‘ μμ±λκΈ° λλ¬Έμ μ«μκ° 1λ‘ λμ€κ³ , snumμ κ°μ²΄μ μκ΄μμ΄ ν΄λμ€μ μν΄ μμΌλ―λ‘(static) μ«μκ° λ¬λΌμ§λ€
// μΈμ€ν΄μ€ λ©€λ²μ static λ©€λ²μ μ°¨μ΄ μμ
class Test {
int ival;
static int sval; // ν΄λμ€ λ‘λ© μ(νλ‘κ·Έλ¨ μμ μ) λ³μ ν λΉ
public void iMethod() {
ival = 100; // instance λ©μλμμ instance λ³μ μ κ·Ό κ°λ₯
sval = 200; // instance λ©μλμμ static λ³μ μ κ·Ό κ°λ₯
}
public static void sMethod() {
// ival = 10; // static λ©μλμμλ instance λ³μ μ κ·Ό λΆκ°λ₯
sval = 20; // static λ©μλμμλ static λ³μ μ κ·Ό κ°λ₯
// this.sval = 30; // static λ©μλλ this μ¬μ© λΆκ°, thisλ λ©μλ μμ λ³μμ λΆμ΄λ κ²
}
}
public class StaticInstanceTest {
public static void main(String[] args) {
Test.sval = 10; // static λ³μλ κ°μ²΄ μμ± μμ΄ ν΄λμ€λͺ
μΌλ‘ μ κ·Ό κ°λ₯
Test.sMethod(); // static λ©μλλ κ°μ²΄ μμ± μμ΄ ν΄λμ€λͺ
μΌλ‘ μ κ·Ό κ°λ₯
// Test.ival = 20; // instance λ³μλ ν΄λμ€λͺ
μΌλ‘ μ κ·Ό λΆκ°λ₯
Test t = new Test();
t.ival = 20; // instance λ³μλ λ°λμ κ°μ²΄ μμ±νμ¬ μ°Έμ‘°λ³μ ν΅ν΄ μ κ·Όν΄μΌ νλ€.
// Test.iMethod(); // instance λ©μλλ ν΄λμ€λͺ
μΌλ‘ μ κ·Ό λΆκ°λ₯
t.iMethod(); // μμ±λ κ°μ²΄λ₯Ό ν΅ν΄μλ§ νΈμΆ κ°λ₯
}
}
_
λ‘ μ°κ²°// final νλμ μμ νμ© μμ
class MyClass {
static final int num = 20; // μ μΈκ³Ό λμμ λ°λμ μ΄κΈ°ν, λ³κ²½ λΆκ°λ₯ν κ°μ΄κΈ° λλ¬Έμ staticμΌλ‘ μ μΈνλ κ²μ΄ ν©λ¦¬μ
public MyClass(int num) {
// this.num = num; ν΄λμ€ λ΄ final λ³μλ μμ±μλ λ©μλμμ λ³κ²½ λΆκ°
}
}
public class FinalTest {
public static void main(String[] args) {
final int num = 10; // μμ: λ³κ²½X, λ°λμ μ μΈκ³Ό λμμ μ΄κΈ°ν
int num2 = 30;
num2 = 50;
final int num3;
num3 = 20;
// num3 = 60; μ΄λ―Έ μμ€μμ μ΄κΈ°ννμΌλ―λ‘ λ€μ μ΄κΈ°ν λΆκ°λ₯
}
}
import
Getterμ Setter λ©μλ μ¬μ©
π ν¨ν€μ§ μ΄λ¦ μ μΈ λ°©λ²
- κ΄λ‘μ λͺ¨λ μλ¬Έμλ‘ μμ±
- μ«μλ‘ μμ λΆκ°
_
μ$
μ μΈ νΉμ λ¬Έμ μ¬μ© λΆκ°- javaλ‘ μμνλ ν¨ν€μ§λ μλ° νμ€ APIμμλ§ μ¬μ©νλ―λ‘ μ¬μ©π ββοΈ
// ν¨ν€μ§ μ΄λ¦κ³Ό Getterμ Setterλ₯Ό μ¬μ©ν μμ
package var;
public class Account {
public static final int MIN_BALANCE = 0;
public static final int MAX_BALANCE = 1000000;
private int balance;
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
if (balance>=MIN_BALANCE && balance<=MAX_BALANCE) {
this.balance = balance;
}
}
}
μ§λκ° μ λ§ μ΄λ§μ΄λ§ν μμ€μΌλ‘ λΉ λ₯΄κ² λκ°λ²λ¦¬λ κΈ°λΆ γ
<-<
ν΄λμ€ ννΈκ° μ λ§ μμ΄ λ§μ κ² κ°λ€. ν루μ’
μΌ μ§λ λκ°κ³ λ λ¨μλ²λ Έλλ°, μ΄ ννΈκ° μ무λλ μλ°μ κΈ°λ³Έμ΄μ μ μΌ μ€μν λΆλΆμ΄λΌ κ·Έλ° λͺ¨μμ΄λ€. μ 리λ μ’ λ μ΄μ¬ν μν΄λμΌμ§.