開発環境
- Microsoft Windows 8 Pro 64bit 日本語 (OS)
- Microsoft Visual Studio Express 2012 for Windows Desktop (IDE)
- プログラミング言語: C#
『初めてのC# 第2版』(Jesse Liberty+Brian MacDonald著、日向俊二訳、オライリー・ジャパン、2006年、ISBN978-487312-194-7)の 第14章(ジェネリックとコレクション)14.6(練習問題)練習14-1.を解いてみる。
その他参考書籍
練習14-1.
コード
using System;
abstract class Animal
{
private int weight;
private string name;
public Animal(int weight, string name)
{
this.weight = weight;
this.name = name;
}
abstract public void Speak();
abstract public void Move();
public override string ToString()
{
return name + ", " + weight;
}
}
class Dog : Animal
{
public Dog(int weight, string name) :
base(weight, name) { }
public override void Speak()
{
Console.WriteLine("ワンワン");
}
public override void Move()
{
Console.WriteLine("てくてく");
}
}
class Cat : Animal
{
public Cat(int weight, string name) :
base(weight, name) { }
public override void Speak()
{
Console.WriteLine("ニャーニャー");
}
public override void Move()
{
Console.WriteLine("するする");
}
}
class Tester
{
public void Run()
{
Cat tama = new Cat(5, "Tama");
Cat sora = new Cat(15, "Sora");
Dog frisky = new Dog(10, "Frisky");
Dog laika = new Dog(20, "Laika");
Animal[] animals = { tama, sora, frisky, laika };
foreach (Animal animal in animals)
{
Console.WriteLine(animal);
animal.Speak();
animal.Move();
}
}
static void Main()
{
Tester t = new Tester();
t.Run();
}
}
入出力結果(Console Window)
Tama, 5 ニャーニャー するする Sora, 15 ニャーニャー するする Frisky, 10 ワンワン てくてく Laika, 20 ワンワン てくてく 続行するには何かキーを押してください . . .
ちなみにJavaScriptの場合。
コード(BBEdit)
var Animal = function (weight, name) {
var weight = weight
name = name;
this.getWeight = function () {
return weight;
},
this.getName = function ( ) {
return name;
};
},
Cat = function ( ) {
Animal.apply(this, arguments);
},
Dog = function ( ) {
Animal.apply(this, arguments);
},
animals = [],
tama, sora, frisky, laika, i, max;
Animal.prototype.speak = function () {
$('#pre0').append( "抽象speak\n" );
},
Animal.prototype.move = function ( ) {
$('#pre0').append( "抽象move\n" );
},
Animal.prototype.toString = function( ) {
return this.getName() +", " + this.getWeight();
},
Cat.prototype = new Animal(),
Cat.prototype.speak = function ( ) {
$('#pre0').append("ニャーニャー\n");
},
Cat.prototype.move = function ( ) {
$('#pre0').append("するする\n");
},
Dog.prototype = new Animal(),
Dog.prototype.speak = function ( ) {
$('#pre0').append("ワンワン\n");
},
Dog.prototype.move = function ( ) {
$('#pre0').append("てくてく\n");
},
tama = new Cat(5, "Tama"),
sora = new Cat(15, "Sora"),
frisky = new Dog(10, "Frisky"),
laika = new Dog(20, "Laika"),
animals = [tama, sora, frisky, laika];
for (i = 0, max = animals.length; i < max; i += 1) {
$('#pre0').append(animals[i].toString() + "\n");
animals[i].speak();
animals[i].move();
}
pythonの場合。
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-
class Animal:
def __init__(self, weight, name):
self._weight = weight
self._name = name
def speak(self):
raise Exception("Animal speak")
def move(self):
raise Exception("Animal move")
def __str__(self):
return "{0}, {1}".format(self._name, self._weight)
class Cat(Animal):
def speak(self):
print("ニャーニャー")
def move(self):
print("するする")
class Dog(Animal):
def speak(self):
print("ワンワン")
def move(self):
print("てくてく")
tama = Cat(5, "Tama")
sora = Cat(15, "Sora")
frisky = Dog(10, "Frisky")
laika = Dog(20, "Laika")
animals = [tama, sora, frisky, laika]
for animal in animals:
print(animal)
animal.speak()
animal.move()
入出力結果(Terminal)
$ ./sample.py Tama, 5 ニャーニャー するする Sora, 15 ニャーニャー するする Frisky, 10 ワンワン てくてく Laika, 20 ワンワン てくてく $
0 コメント:
コメントを投稿