분류 전체보기
-
자바의정석 Ex7_10 추상 클래스, 추상 메서드Java/자바의정석 2023. 2. 21. 13:23
package Chap07; public class Ex7_10 { public static void main(String[] args) { Unit[] group = { new Marine(), new Tank(), new Dropship() }; for (int i = 0; i < group.length; i++) group[i].move(100, 200); } } abstract class Unit { int x, y; //move라는 추상 메서드 abstract void move(int x, int y); void stop() { } } class Marine extends Unit { //추상클래스를 완성시킴 void move(int x, int y) { System.out.println("Ma..
-
자바의 정석 Ex7_9 (하나의 배열에 여러종류의 객체 저장)Java/자바의정석 2023. 2. 20. 16:54
package Chap07; class Product2 { int price; int bonusPoint; Product2(int price) { this.price = price; bonusPoint = (int)(price/10.0); } } class Tv2 extends Product2 { Tv2() { super(100); } @Override public String toString() { return "Tv"; } } class Computer2 extends Product2 { Computer2() { super(200); } @Override public String toString() { return "Computer"; } } class Audio2 extends Product2 { ..
-
자바의정석 Ex7_7(다형성)Java/자바의정석 2023. 2. 20. 16:13
package Chap07; class Ex7_7 { public static void main(String args[]) { Car car = null; //Car타입의 car 참조변수에 null 값 대입 FireEngine fe = new FireEngine(); //FireEngine를 참조하는 fe 참조변수 생성 FireEngine fe2 = null; //FireEngine을 참조하는fe2참조변수 //FireEngine -> water()까지 사용가능 //Car참조하는 변수는 water() 사용 불가능 //Car -> 조상 , FireEngine -> 자손 fe.water(); car = fe; // car = (Car)fe;에서 형변환이 생략됨, 자손->부모 생략가능 // car.water()..
-
자바의 정석 (this, super)의 이해Java/자바의정석 2023. 2. 20. 15:15
package Chap07; public class Ex7_4 { public static void main(String[] args) { Point3D p = new Point3D(1, 2, 3); System.out.println("x=" + p.x + ",y=" + p.y + ",z=" + p.z); } } class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } class Point3D extends Point { int z; Point3D(int x, int y, int z) { super(x, y); // Point(int x, int y)를 호출 this.z = z; } } ===========실행결과=========..
-
자바의 정석 Ex7_2 (this, super)Java/자바의정석 2023. 2. 20. 14:48
package Chap07; class Ex7_2 { public static void main(String args[]) { Child c = new Child(); c.method(); } } class Parent { int x=10; } class Child extends Parent { static { } int x=20; void method() { System.out.println("x=" + x); System.out.println("this.x=" + this.x); System.out.println("super.x="+ super.x); } } ===========실행결과=========== x=20 this.x=20 super.x=10 위의 코드를 분석해보자. class Parent ..
-
자바의 정석 Ex7_1 (클래스 초기화, 인스턴스 초기화)Java/자바의정석 2023. 2. 20. 14:24
package Chap07; class Tv { static { System.out.println("클래스 초기화"); } { System.out.println("{인스턴스 }"); //인스턴스 초기화 } boolean power; // 전원상태(on/off) int channel; // 채널 void power() { power = !power; } void channelUp() { ++channel; } void channelDown() { --channel; } } class SmartTv extends Tv { boolean caption; void displayCaption(String text) { if (caption) { System.out.println(text); } } } class ..