μ΄ κΈμ μνμ½λ©μ 'Java' κ°μ’λ₯Ό λ°νμΌλ‘ μ 리ν λ΄μ©μ λλ€.
μμκ³Ό μμ±μ
package org.opentutorials.javatutorials.Inheritance.example4;
public class ConstructorDemo {
public static void main(String[] args) {
ConstructorDemo c = new ConstructorDemo();
}
}
μ μ½λλ μλ¬κ° λ°μνμ§ μμ΅λλ€. ConstructorDemo κ°μ²΄λ₯Ό μμ±ν λ μλμΌλ‘ μμ±μλ₯Ό λ§λ€μ΄μ£ΌκΈ° λλ¬Έμ λλ€.
package org.opentutorials.javatutorials.Inheritance.example4;
public class ConstructorDemo {
public ConstructorDemo(int param1) {}
public static void main(String[] args) {
ConstructorDemo c = new ConstructorDemo();
}
}
νμ§λ§ μ μ½λλ μλ¬κ° λ°μν©λλ€. 맀κ°λ³μκ° μλ μμ±μκ° μμ λλ μλμΌλ‘ κΈ°λ³Έ μμ±μλ₯Ό λ§λ€μ΄μ£Όμ§ μκΈ° λλ¬Έμ λλ€. μ¦ μ μ½λλ μ‘΄μ¬νμ§ μλ μμ±μλ₯Ό νΈμΆνκ³ μμ΅λλ€.
package org.opentutorials.javatutorials.Inheritance.example4;
public class ConstructorDemo {
public ConstructorDemo(){}
public ConstructorDemo(int param1) {}
public static void main(String[] args) {
ConstructorDemo c = new ConstructorDemo();
}
}
λ°λΌμ κΈ°λ³Έ μμ±μλ₯Ό μΆκ°ν΄μ€μΌ ν©λλ€. μ΄λ ν μμ±μλ μ μνλ©΄, μλ°λ κΈ°λ³Έ μμ±μλ₯Ό μλμΌλ‘ λ§λ€μ΄μ£Όμ§ μμ΅λλ€.
class Calculator {
int left, right;
public Calculator(){}
public Calculator(int left, int right){
this.left = left;
this.right = right;
}
public void setOprands(int left, int right) {
this.left = left;
this.right = right;
}
public void sum() {
System.out.println(this.left + this.right);
}
public void avg() {
System.out.println((this.left + this.right) / 2);
}
}
class SubstractionableCalculator extends Calculator {
public SubstractionableCalculator(int left, int right) {
this.left = left;
this.right = right;
}
public void substract() {
System.out.println(this.left - this.right);
}
}
public class CalculatorConstructorDemo5 {
public static void main(String[] args) {
SubstractionableCalculator c1 = new SubstractionableCalculator(10, 20);
c1.sum();
c1.avg();
c1.substract();
}
}
μμ ν΄λμ€μ κΈ°λ³Έ μμ±μλ₯Ό λ§λλ κ²λ λ°©λ²μ΄μ§λ§, κΈ°λ³Έ μμ±μλ₯Ό λ§λ€μ§ μκ³ λ λ¬Έμ μμ΄ κ°μ²΄κ° λμνλλ‘ ν μ μλ λ°©λ²μ΄ μμ΅λλ€. νμ¬ μμ ν΄λμ€μ μμ±μμ νμ ν΄λμ€μ μμ±μμ λ‘μ§μ κ°μ΅λλ€. νμ ν΄λμ€μμ λκ°μ μΌμ νκ³ μκΈ° λλ¬Έμ μ€λ³΅μ΄λΌκ³ ν μ μμ΅λλ€. μμ ν΄λμ€μ κ°μ μ½λλ₯Ό 볡μ¬νλ λμ μ, μμ ν΄λμ€μ μμ±μλ₯Ό νμ ν΄λμ€κ° μμ±λ λ νΈμΆν΄μ μ€νμν¬ μ μμ΅λλ€.
super
superλ μμ ν΄λμ€λ₯Ό κ°λ¦¬ν€λ ν€μλ μ λλ€.
package org.opentutorials.javatutorials.Inheritance.example3;
class Calculator {
int left, right;
public Calculator(){}
public Calculator(int left, int right){
this.left = left;
this.right = right;
}
public void setOprands(int left, int right) {
this.left = left;
this.right = right;
}
public void sum() {
System.out.println(this.left + this.right);
}
public void avg() {
System.out.println((this.left + this.right) / 2);
}
}
class SubstractionableCalculator extends Calculator {
public SubstractionableCalculator(int left, int right) {
super(left, right);
}
public void substract() {
System.out.println(this.left - this.right);
}
}
public class CalculatorConstructorDemo5 {
public static void main(String[] args) {
SubstractionableCalculator c1 = new SubstractionableCalculator(10, 20);
c1.sum();
c1.avg();
c1.substract();
}
}
// output
// 30
// 15
// -10
νμ ν΄λμ€μ μμ±μμμ superλ₯Ό μ¬μ©ν λ μ£Όμν μ μ superκ° κ°μ₯ λ¨Όμ λνλμΌ νλ€λ μ μ λλ€. μ¦ λΆλͺ¨κ° μ΄κΈ°νλκΈ° μ μ μμμ΄ μ΄κΈ°νλλ μΌμ λ°©μ§νκΈ° μν μ μ± μ΄λΌκ³ μκ°νλ©΄ λ©λλ€.
'Programming > Java' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Java] overloading μ΄λ? μ€λ²λ‘λ© λ» (0) | 2022.11.01 |
---|---|
[Java] overriding μ€λ²λΌμ΄λ©μ΄λ? (0) | 2022.10.25 |
[Java] μμ (0) | 2022.10.24 |
[Java] μ΄κΈ°νμ μμ±μ (0) | 2022.10.24 |