[Java] ν΄λμ€, μμ, getter, setter, νλ³ν, super, μ€λ²λΌμ΄λ©μ νμ©ν μμ
2021. 3. 16. 19:37γκ°λ μ 리 μμ μ€/Java
728x90
λ°μν
μλ νμΈμ? μ£Όλνλ μ λλ€.
03μ 16μΌ μμ λ₯Ό νλ² ν΄λ³΄λλ‘ νκ² μ΅λλ€!
μμ λ΄μ©μ 곡μ ν΄μ μ¬λ¬λΆλ€κ»μλ 곡λΆνμλλ°, λ§μ λμ λμκΈ° λ°λΌκ² μ΅λλ€!
κ·ΈλΌ λ°λ‘ μμ ν΄ λ³΄κ² μ΅λλ€!
π λλ μ€κ³μ!
Super Classμ Sub Class(2κ° μ λ)λ₯Ό μ΄μ©νμ¬ λ°μ΄ν°λ₯Ό νννλ νλ‘κ·Έλ¨ κ΅¬ν κ° ν΄λμ€λ§λ€ λ³μ, λ©μλ, μμ±μλ₯Ό μ°μ μ μν΄μΌ νλ€. getter, setter, μμ, νλ³ν, super ν€μλ, μ€λ²λΌμ΄λ©μ μ¬μ©ν κ²!
π ν΄λμ€ κ΅¬νν
πμ£Όμ : 컀νΌμ
ν΄λμ€ μ΄λ¦ | μ€ λͺ |
Cafe.java | Main Method |
People.java | private String name; // μ΄λ¦ private String phoneNum; // νΈλν° λ²νΈ private String age; // λμ΄ ### Method ### getName() { ... } // μ΄λ¦μ getter setName() { ... } // μ΄λ¦μ setter getPhoneNum() { ... } // νΈλν°λ²νΈμ getter setPhoneNum() { ... } // νΈλν°λ²νΈμ setter getAge() { ... } // λμ΄μ getter setAge() { ... } // λμ΄μ setter toString() { ... } // Main Methodμμ λ°°μ΄ μΆλ ₯ μ λμ¬ 16μ§μ μ£Όμκ° λμ μνλ μΆλ ₯κ° μΆλ ₯μ μν΄ Overriding |
Guest.java | private int money; // μ§λΆκΈμ‘ private int point; // ν¬μΈνΈ μ λ¦½κΈ private String order; // μ£Όλ¬Έ λ΄μ© ### Method ### order() {...} // μ£Όλ¬Ένλ νλ takeMoney() {...} // λ μ§λΆ νλ bounusPoint() {...} // ν¬μΈνΈ μ 립 νλ |
Staff.java | private String takeOrder; // μ£Όλ¬Έλ°μλ΄μ© private String coffeeMachine; // 컀νΌλ§λλ νλ private String pass; // μ»€νΌ κ±΄λ€λ νλ private int money; // μμ κΈ private int point; // μ§λΆ ν¬μΈνΈ ### Method ### order() {...} // μ£Όλ¬Έ λ°μ νλ work() {...} // μ»€νΌ μ μ‘° νλ pointSave() {...} // ν¬μΈνΈ μ 립 νλ passCoffee() {...} // μλμκ² μ»€νΌλ₯Ό 건λ€λ νλ |
βοΈ Cafe.java (Main Method)
package homework_210316;
public class Cafe {
public static void main(String[] args) {
People[] saram = {
new Staff("νμ§μ", "010-123-2345", 34, "μ΄λ€ κ±° λ릴κΉμ?", "...μμ΄μ€μλ©λ¦¬μΉ΄λ
Έ λ§λλ μ€...", "μμ΄μ€μλ©λ¦¬μΉ΄λ
Έ", 2000, 200),
new Guest("νμΈμ€", "010-345-2345", 32, "μμ΄μ€μλ©λ¦¬μΉ΄λ
Έ μ· μΆκ°", 2000, 200)
};
for ( int i = 0; i < saram.length; ++i ) {
System.out.println("======================");
System.out.println(saram[i]);
if (saram[i] instanceof Staff) {
Staff tmp = (Staff)saram[i];
tmp.order();
} else if (saram[i] instanceof Guest) {
Guest tmp = (Guest)saram[i];
tmp.order();
}
}
for( int j = 0; j < saram.length; ++j ) {
System.out.println("======================");
System.out.println(saram[j]);
if (saram[j] instanceof Staff) {
Staff tmp = (Staff)saram[j];
tmp.work();
} else if (saram[j] instanceof Guest) {
Guest tmp1 = (Guest)saram[j];
tmp1.takeMoney();
}
}
for( int k = 0; k < saram.length; ++k ) {
System.out.println("======================");
System.out.println(saram[k]);
if (saram[k] instanceof Staff) {
Staff tmp = (Staff)saram[k];
tmp.pointSave();
tmp.passCoffee();
} else if (saram[k] instanceof Guest) {
Guest tmp1 = (Guest)saram[k];
tmp1.bonusPoint();
}
}
} // main λ
} // Class λ
βοΈ People.java
package homework_210316;
public class People {
private String name;
private String phoneNum;
private int age;
public People(String name, String phoneNum, int age) {
this.name = name;
this.phoneNum = phoneNum;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
String tmp = super.toString();
return "μ΄λ¦ : " + this.name + "\n" + "νΈλν°λ²νΈ : " + this.phoneNum + "\n" + "λμ΄ :" + this.age + "\n";
}
} // Class λ
βοΈ Staff.java
package homework_210316;
public class Staff extends People {
private String takeOrder;
private String coffeeMachine;
private String pass;
private int money;
private int point;
public Staff(String name, String phoneNum, int age, String takeOrder, String coffeeMachine, String pass, int money, int point) {
super(name, phoneNum, age);
this.takeOrder = takeOrder;
this.coffeeMachine = coffeeMachine;
this.pass = pass;
this.money = money;
this.point = point;
}
public void order() {
System.out.println(this.takeOrder);
}
public void work() {
System.out.println(this.money + "μ μ
λλ€!");
System.out.println(this.coffeeMachine);
}
public void pointSave() {
System.out.println("point " + this.point + " μ μ 립 λμ
¨μ΅λλ€!");
}
public void passCoffee() {
System.out.println("μ£Όλ¬Ένμ " + this.pass + " λμμ΅λλ€!");
}
} // Class λ
βοΈ Guest.java
package homework_210316;
public class Guest extends People {
private int money;
private int point;
private String order;
public Guest(String name, String phoneNum, int age, String order, int money, int point) {
super(name, phoneNum, age);
this.order = order;
this.money = money;
this.point = point;
}
public void order() {
System.out.println(this.order + "νμ λΆν λ립λλ€!");
}
public void takeMoney() {
System.out.println(this.money + "μ μ¬κΉμ΅λλ€!");
}
public void bonusPoint() {
System.out.println(this.point + "ν¬μΈνΈ μ¬λλ€!");
}
} // Class λ
728x90
λ°μν
'κ°λ μ 리 μμ μ€ > Java' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Java] Lamda Function (0) | 2021.06.21 |
---|---|
[Java] Enum (μ΄κ±°ν) (0) | 2021.06.20 |
[Java] Classμ λνμ¬ (λ―Έμμ±) (0) | 2021.03.15 |
[Java] ν€λ³΄λμμ μ λ ₯λ λ¬Έμμ΄μ μ μλ‘ λ°κΎΌ λ€ κ·Έ κ°μ μ΄ν©κ³Ό νκ· κ΅¬νκΈ° (0) | 2021.03.08 |
[Java] μλ°μ κ°μ²΄μ§ν₯ νλ‘κ·Έλλ°(Object-Oriented Programming) + ArrayList - μμ μ½λ νμ΄ (0) | 2021.03.03 |