1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | package a; class Calculator { int left, 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 { //extends Calculator 를 통해서 상속을 받음 public void substract() { System.out.println(this.left - this.right); } //substract라는 메소드를 추가 } public class CalculatorDemo1 { public static void main(String[] args) { SubstractionableCalculator c1 = new SubstractionableCalculator(); c1.setOprands(10, 20); c1.sum(); c1.avg(); //상속을 받았으므로 setOperands,sum,avg 메소드를 사용 할 수 있음 c1.substract(); } } | cs |
카테고리 없음