テレビデオ
概要
テレビを表すTelevisionクラスとテレビとビデオデッキが合体したTeleVideoクラスのオブジェクトをプログラム化し挙動を確認する。継承の基本を学ぶ。
class Television { private int channel; Television(int channel) { System.out.println("テレビのクラスのオブジェクトを生成しました"); this.channel = channel; } Television() { } public void display() { System.out.println("チャンネル番号[" + this.channel + "]が流れています"); } public void setChannel(int channel) { this.channel = channel; System.out.println("チャンネル番号[" + this.channel + "]に変更しました"); } public int getChannel() { return channel; } } class TeleVideo extends Television { private int recordedChannel; TeleVideo() { // super();が自動的に挿入されている。空なので何も起きない。 System.out.println("テレビデオクラスのオブジェクトを生成しました"); } public void record() { this.recordedChannel = super.getChannel(); System.out.println("チャンネル番号[" + this.recordedChannel + "]を録画します"); } public void play() { System.out.println("チャンネル番号[" + this.recordedChannel + "]を再生します"); } } public class MyTVtest { public static void main(String[] args) { Television tv =new Television(12); TeleVideo tvVideo=new TeleVideo(); System.out.println(); tv.setChannel(3); tv.display(); System.out.println(); tvVideo.setChannel(1); tvVideo.record(); tvVideo.play(); } }
実行結果
テレビのクラスのオブジェクトを生成しました
テレビデオクラスのオブジェクトを生成しました
チャンネル番号[3]に変更しました
チャンネル番号[3]が流れています
チャンネル番号[1]に変更しました
チャンネル番号[1]を録画します
チャンネル番号[1]を再生します
- 出版社/メーカー: シャープ
- 発売日: 2004/01/14
- メディア: エレクトロニクス
- この商品を含むブログを見る