かずきのBlog

C#やJavaやRubyとメモ書き

目次

Blog 利用状況

ニュース

わんくまBlogが不安定になったため、前に書いてたはてなダイアリーにメインを移動します。
かずきのBlog@Hatena
技術的なネタは、こちらにも、はてなへのリンクという形で掲載しますが、雑多ネタははてなダイアリーだけに掲載することが多いと思います。
コメント
プログラマ的自己紹介
お気に入りのツール/IDE
プロフィール
経歴
広告
アクセサリ

書庫

日記カテゴリ

オブジェクト指向におけるFizzBuzz問題 をしてみる

モトネタ オブジェクト指向におけるFizzBuzz問題
http://blogs.wankuma.com/episteme/archive/2007/11/08/106927.aspx

とりあえずRubyで…

main.rb
require 'animal.rb'

animal = nil
animal = Dog.new
animal.sound
animal.count = 3
animal.sound
animal = Cat.new
animal.sound

animal.rb
class Animal
  attr_accessor :cry, :count

  def initialize
    self.count = 1
  end

  def sound
    self.count.times { print cry }
    puts
  end
 
end

class Dog < Animal
  def initialize
    super
    self.cry = "わん"
  end
end

class Cat < Animal
  def initialize
    super
    self.cry = "にゃー"
  end
end

カプセル化?そんなの関係ね~!

投稿日時 : 2007年11月8日 19:19

Feedback

# re: オブジェクト指向におけるFizzBuzz問題 をしてみる 2007/11/08 20:51 さかもと

かっこえぇ・・・。

rubyっていいですか?(←素人の質問分類第一級

# re: オブジェクト指向におけるFizzBuzz問題 をしてみる 2007/11/08 21:13 かずき

覚えてて損はないと思いますよ。
似た感じで使える言語を覚えて無いなら、やってみる価値ありです!

# re: オブジェクト指向におけるFizzBuzz問題 をしてみる 2007/11/08 21:53 επιστημη

せんせぇしつもん。
rubyって抽象クラスつくれるんすか?
このままやと
animal = Animal.new
animal.sound
できちゃうですよね?

# re: オブジェクト指向におけるFizzBuzz問題 をしてみる 2007/11/09 6:59 かずき

Animalのインスタンス化を厳密に禁止しようと思うなら…
mocule Animal
# 途中省略
end

class Dog
include Animal
# 途中省略
end

class Cat
include Animal
# 途中省略
end
みたいにmoduleを使うかなぁ

# re: オブジェクト指向におけるFizzBuzz問題 をしてみる 2007/11/09 15:48 no-name

はじめまして。επιστημηさんの
元エントリで、Rubyで投稿した者です。

なるほど、moduleですか。
しかしmoduleだと、self.count = 1 を
Animalモジュールには定義できなくなるのでは?

まあ私自身、DogやCatに@count = 1を書いてしまい
ましたが。たかだか1行ですしね。

# オブジェクト指向におけるFizzBuzz問題をやってみた その2 2007/11/09 21:55 かずきのBlog

オブジェクト指向におけるFizzBuzz問題をやってみた その2

# louis vuitton handbags 2012/10/28 3:26 http://www.louisvuittonoutletbags2013.com/

Factual acquaintanceship foresees the needs of additional instead of just exalt the have possession of.
louis vuitton handbags http://www.louisvuittonoutletbags2013.com/

# mens shirts 2012/10/28 14:36 http://www.burberryoutletonlineshopping.com/burber

Very excellent information can be found on site . "Prayer is the wing wherewith the soul flies to heaven, and meditation the eye wherewith we see God." by Ambrose of Milan.
mens shirts http://www.burberryoutletonlineshopping.com/burberry-men-shirts.html

# Nike Free Run 2012/10/30 21:38 http://www.nikefree3runschuhe.com/

If you decide to might preserve your strategy due to an enemy, show the following among the someone i know.
Nike Free Run http://www.nikefree3runschuhe.com/

# mia clarisonic mia coupon 2012/10/30 21:38 http://www.clarisonicmia-coupon.com/

During abundance much of our neighbors understand or know individuals; at difficulty problems in later life much of our neighbors.
mia clarisonic mia coupon http://www.clarisonicmia-coupon.com/

# burberry outlet store 2012/11/02 22:56 http://www.burberrysalehandbags.com

I like this post, enjoyed this one appreciate it for putting up.
burberry outlet store http://www.burberrysalehandbags.com

# wallet 2012/11/03 2:21 http://www.burberryoutletscarfsale.com/accessories

Enjoyed studying this, very good stuff, appreciate it. "Hereafter, in a better world than this, I shall desire more love and knowledge of you." by William Shakespeare.
wallet http://www.burberryoutletscarfsale.com/accessories/burberry-wallets-2012.html

# burberry mens shirts 2012/11/03 2:21 http://www.burberryoutletscarfsale.com/burberry-me

I view something really special in this web site.
burberry mens shirts http://www.burberryoutletscarfsale.com/burberry-men-shirts.html

# t shirt scarf 2012/11/03 2:21 http://www.burberryoutletscarfsale.com/accessories

Some truly good info , Gladiolus I noticed this. "Ready tears are a sign of treachery, not of grief." by Publilius Syrus.
t shirt scarf http://www.burberryoutletscarfsale.com/accessories/burberry-scarf.html

# Burberry Watches 2012/11/03 2:21 http://www.burberryoutletscarfsale.com/accessories

Some really great posts on this web site , appreciate it for contribution.
Burberry Watches http://www.burberryoutletscarfsale.com/accessories/burberry-watches.html

# women t shirts 2012/11/03 2:22 http://www.burberryoutletscarfsale.com/burberry-wo

Perfectly pent written content , regards for selective information .
women t shirts http://www.burberryoutletscarfsale.com/burberry-womens-shirts.html

# cheap burberry bags 2012/11/03 2:22 http://www.burberryoutletscarfsale.com/burberry-ba

I was examining some of your posts on this website and I believe this internet site is real instructive! Retain posting .
cheap burberry bags http://www.burberryoutletscarfsale.com/burberry-bags.html

# mulberry bag 2012/11/04 2:04 http://www.bagmulberry.co.uk

Only wanna comment on few general things, The website design and style is perfect, the subject matter is very good. "The stars are constantly shining, but often we do not see them until the dark hours." by Earl Riney.
mulberry bag http://www.bagmulberry.co.uk

# burberry scarf 2012/11/05 23:41 http://www.burberrysalehandbags.com/burberry-scarf

Utterly composed articles, Really enjoyed studying.
burberry scarf http://www.burberrysalehandbags.com/burberry-scarf.html

# mulberry sale 2012/11/07 1:07 http://www.outletmulberryuk.co.uk

Perfectly composed written content , regards for selective information .
mulberry sale http://www.outletmulberryuk.co.uk

# mulberry 2012/11/07 1:08 http://www.mulberrybagukoutlet.co.uk

Appreciate it for helping out, fantastic information. "It does not do to dwell on dreams and forget to live." by J. K. Rowling.
mulberry http://www.mulberrybagukoutlet.co.uk

# longchamp 2012/11/08 13:44 http://www.sacslongchamppascher2013.com

Thanks, I have just been searching for info approximately this topic for ages and yours is the greatest I have came upon so far. However, what concerning the bottom line? Are you certain concerning the source?
longchamp http://www.sacslongchamppascher2013.com

# mulberry handbags 2012/11/12 12:26 http://www.bagmulberryuk.co.uk/mulberry-handbags-c

You have brought up a very great points , regards for the post.
mulberry handbags http://www.bagmulberryuk.co.uk/mulberry-handbags-c-9.html

# mulberry handbags 2012/11/12 12:26 http://www.bagmulberry.co.uk/mulberry-handbags-c-9

I haven't checked in here for some time because I thought it was getting boring, but the last few posts are good quality so I guess I'll add you back to my daily bloglist. You deserve it friend :)
mulberry handbags http://www.bagmulberry.co.uk/mulberry-handbags-c-9.html

タイトル
名前
Url
コメント