[ this ν€μλ ]
[ this ν€μλ ]
- μΈμ κ° μμ±λ μμ κ°μ²΄(μΈμ€ν΄μ€)μ λ©λͺ¨λ¦¬ μ£Όμ
- μΈμ€ν΄μ€κ° μκΈ° μμ μ μ°Έμ‘°ν λ μ¬μ©
- this.λ©€λ²νλ
· ν΄λμ€λ΄ μμ μ λ©€λ²λ₯Ό μ§μ μ°Έμ‘°
· λ©μλμ λ§€κ°λ³μμ ν΄λμ€μ λ©€λ²νλκ° λμΌν λ μ΄λ₯Ό ꡬλΆνκΈ° μν΄ μ¬μ©
[ this ν€μλ μ¬μ© μμ ]
class HumanBody{
private String organs;
private String muscle;
private int bone;
private int blood;
// λ©μλμ λ§€κ°λ³μμ μΈμ€ν΄μ€ λ³μλͺ
μ΄ κ°μ κ²½μ° this ν€μλλ₯Ό ν΅ν΄ ꡬλΆ
HumanBody(String organs, String muscle, int bone, int blood){
this.organs = organs;
this.muscle = muscle;
this.bone = bone;
this.blood = blood;
}
void printHumanBody() {
System.out.println(organs + " " + muscle + " " + bone + " " + blood);
}
}
public class TestCode {
public static void main(String[] args) {
HumanBody Hong = new HumanBody("Brain", "arm", 206, 120);
Hong.printHumanBody();
}
}
/* [μΆλ ₯ κ²°κ³Ό]
Brain arm 206 120 */
[ this() λ©μλ ]
[ this() λ©μλ ]
- ν΄λμ€ μμ μ μμ±μ λ©μλλ₯Ό νΈμΆν λ μ¬μ©
- μμ μ μμ±μ λ©μλλ₯Ό μ¬μ΄μ©νλ κ²
- μ΄ λ μμ±μ λ©μλμμ λ€λ₯Έ μμ±μ λ©μλλ₯Ό νΈμΆν κ²½μ°
this() λ©μλμ νΈμΆμ λ°λμ μμ±μμ μ΅μλ¨μμλ§ κ°λ₯
[ this() λ©μλ μ¬μ© μμ ]
class HumanBody{
private String organs;
private String muscle;
private int bone;
private int blood;
// -- constructor --
HumanBody(String organs, String muscle, int bone, int blood){
this.organs = organs;
this.muscle = muscle;
this.bone = bone;
this.blood = blood;
}
HumanBody(int bone, int blood){
this.bone = bone;
this.blood = blood;
}
HumanBody(){
// λ κ°μ μ μν λ§€κ°λ³μ μ
λ ₯λ°λ μμ±μλ₯Ό νΈμΆ this()
this(310, 240); // μμ±μμ νΈμΆ μμΉλ μ΅μλ¨μ΄μ΄μΌ ν¨
organs = "heart";
muscle = "leg";
}
// -- print Method --
public void printHumanBody(){
System.out.println(organs + " " + muscle + " " + bone + " " + blood);
}
}
public class TestCode {
public static void main(String[] args) {
HumanBody Hong = new HumanBody("Brain", "arm", 206, 120);
Hong.printHumanBody();
HumanBody Kim = new HumanBody();
Kim.printHumanBody();
}
}
/* [μΆλ ₯ κ²°κ³Ό]
Brain arm 206 120
heart leg 310 240 */
[ super ν€μλ ]
[ super ν€μλ ]
- μμ ν΄λμ€λ‘λΆν° μμλ°μ λ©€λ²νλλ λ©μλλ₯Ό νμ ν΄λμ€μμ
μ°Έμ‘°νμ¬ λͺ μν λ μ¬μ©
- this ν€μλμ κ°μ΄ μμ ν΄λμ€μ λ©€λ²νλμ νμ ν΄λμ€μ λ©€λ²νλλͺ μ΄ κ°μ κ²½μ°
super ν€μλλ₯Ό ν΅ν΄ κ΅¬λΆ κ°λ₯
- this ν€μλμ λ§μ°¬κ°μ§λ‘ μ°Έμ‘° λ³μλ‘ μ¬μ©ν μ μλ λμμ μΈμ€ν΄μ€ λ©μλλΏμ.
(staticμΌλ‘ μ μΈλ ν΄λμ€ λ©μλx)
[ super ν€μλ μ¬μ© μμ ]
class Animal{
public String dog = "schnauzer";
}
class Mammalia extends Animal{
private String dog = "poodle";
void display() {
System.out.println("Mammalia.dog : " + dog);
System.out.println("this.dog : " + this.dog);
System.out.println("super.dog : " + super.dog);
}
}
public class TestCode {
public static void main(String[] args) {
Mammalia animal = new Mammalia();
animal.display();
}
}
/* [μΆλ ₯ κ²°κ³Ό]
Mammalia.dog : poodle
this.dog : poodle
super.dog : schnauzer */
[ super() λ©μλ ]
[ super() λ©μλ ]
- μμ ν΄λμ€μ μμ±μλ₯Ό νΈμΆν λ μ¬μ©
β νμ ν΄λμ€ κ°μ²΄ μμ± μ κ°μ²΄λ νμ ν΄λμ€μ κ³ μ λ©€λ²λ³μμ μμ ν΄λμ€μ λ©€λ²λ₯Ό λͺ¨λ ν¬ν¨
β‘ μμ ν΄λμ€μ λ©€λ²λ³μ μ΄κΈ°ν νμ
β’ νμ ν΄λμ€μ μμ±μμμ λΆλͺ¨ ν΄λμ€ μμ±μλ₯Ό νΈμΆν¨μΌλ‘ μμ ν΄λμ€ μ΄κΈ°ν
β£ μλ° μ»΄νμΌλ¬λ 묡μμ μΌλ‘ μμ ν΄λμ€ μμ±μλ₯Ό νμ ν΄λμ€ μμ±μμ 첫 μ€μ μλμΌλ‘ μΆκ°
β» μ΄ λ ν΄λμ€μ μ μλ μμ±μκ° μμ΄μΌλ§ μ»΄νμΌλ¬κ° μλμΌλ‘ μΆκ°
[ super() λ©μλ μ¬μ© μμ ]
class Animal{
public String dog;
// -- structor --
Animal(){
dog = "schnauzer";
}
Animal(String dog){
this.dog = dog;
}
}
class Mammalia extends Animal{
private String cat;
Mammalia(){
// νμ ν΄λμ€μ μμ±μμμ μμ ν΄λμ€μΈ Animalμ μμ±μ νΈμΆμ ν΅ν΄ λ©€λ²λ³μ μ΄κΈ°ν
super();
cat = "sphinx";
}
void display() {
System.out.println("dog : " + dog);
System.out.println("cat : " + cat);
}
}
public class TestCode {
public static void main(String[] args) {
Mammalia animal = new Mammalia();
animal.display();
}
}
/* [μΆλ ₯ κ²°κ³Ό]
dog : schnauzer
cat : sphinx
*/