2013年4月1日月曜日

開発環境

『初めてのC# 第2版』(Jesse Liberty+Brian MacDonald著、日向俊二訳、オライリー・ジャパン、2006年、ISBN978-487312-194-7)の 第10章(配列)10.9(練習問題)練習10-1.を解いてみる。

その他参考書籍

練習10-1.

コード

using System;

namespace Sample
{
    class Dog
    {
        private int weight;
        private string name;
        public Dog(int weight, string name)
        {
            this.weight = weight;
            this.name = name;
        }
        public int Weight
        {
            get { return weight; }
        }
        public string Name
        {
            get { return name; }
        }
    }
    class Tester
    {
        public void Run()
        {
            Dog milo = new Dog(26, "Milo");
            Dog frisky = new Dog(10, "Frisky");
            Dog laika = new Dog(50, "Laika");
            Dog[] dogs = { milo, frisky, laika };
            foreach (Dog dog in dogs)
            {
                Console.WriteLine("Name: {0}  Weight: {1}", dog.Name, dog.Weight);
            }
        }
        static void Main()
        {
            Tester t = new Tester();
            t.Run();
        }
    }
}

入出力結果(Console Window)

Name: Milo  Weight: 26
Name: Frisky  Weight: 10
Name: Laika  Weight: 50
続行するには何かキーを押してください . . .

pythonの場合。

コード(BBEdit)

sample.py

#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-

class Dog:
    def __init__(self, weight, name):
        self._weight = weight
        self._name = name
    def getWeight(self):
        return self._weight
    def getName(self):
        return self._name

milo = Dog(26, "Milo")
frisky = Dog(10, "Frisky")
laika = Dog(50, "Laika")

dogs = [milo, frisky, laika]
for dog in dogs:
    print("Name: {0}  Weight: {1}".format(dog.getName(), dog.getWeight()))

入出力結果(Terminal)

$ ./sample.py
Name: Milo  Weight: 26
Name: Frisky  Weight: 10
Name: Laika  Weight: 50
$

0 コメント:

コメントを投稿