Flatiron Bootcamp PTのためのCLIの構築2
これは2番目の部分です
Ruby構文を用いたオブジェクト指向プログラミングに注目します.
調べるWikipedia , OOPは「オブジェクト」という概念に基づくプログラミングパラダイムで、データとコードを含むことができます.
何がそれを意味しますか?
それはあなたが作成できることを意味しますclasses , 表現する方法objects プログラムで.
例は
上記の新しいオブジェクトを作成し、助けを借りて
ルビーは持っていることに注意してくださいbuilt クラスで.以下に例を示します:
我々が必要とする我々のクラスに振舞いを加えることmethods . メソッドは、プログラムの機能を実装します.以下に例を示します.
注意:オブジェクトのメソッドを呼び出します
前方と前方!
以前から我々のコードに戻ってください
我々
毎回新しいことを言いましょう
インスタンス変数
今何?
さて、実世界オブジェクトを表現するクラスを作成する方法を知っているので、そのデータにアクセスする方法を学ぶことができます.属性アクセサーを使用します.
私たちが記事を通して取り組んでいて、属性アクセサーを越えるコードを戻すことができます.
定義しましょうgetter method それで、我々は我々のTwitterRanger信者属性にアクセスすることができます.
よく書きます.
ここで包みにしよう.パートIIIのために調整される滞在!
Ruby構文を用いたオブジェクト指向プログラミングに注目します.
調べるWikipedia , OOPは「オブジェクト」という概念に基づくプログラミングパラダイムで、データとコードを含むことができます.
何がそれを意味しますか?
それはあなたが作成できることを意味しますclasses , 表現する方法objects プログラムで.
例は
class CoolestCanadianActors # this class represents the coolest Canadian actors
def initialize(first, last) # The initialize method is part of the object-creation process in Ruby & it allows you to set the initial values for an object.
@first = first
@last = last
end
end
obj1 = Name.new("Ryan", "Reynolds") # we've created a new object with the name "Ryan Reynolds".
print obj1 # prints out object
上記の通りinitialize メソッドはオブジェクト作成プロセスの一部であり、オブジェクトの初期値を設定するのに役立ちます.他のプログラミング言語では、これを「コンストラクタ」と呼びます.上記の新しいオブジェクトを作成し、助けを借りて
initialize
方法は、我々は力を使用し、新しい“クールなカナダの俳優”を作成することができました.ルビーは持っていることに注意してくださいbuilt クラスで.以下に例を示します:
array = Array.new
しかし、これらはRubyに組み込まれているので、それらを作成する特別な構文を取得します.array = []
hash = {}
string = ""
さあ、今、我々は我々を持っているcoolestCanadianCelebrity
クラスは、次は何ですか?よく我々は我々のオブジェクトに振舞いを加えることができます.我々は、すでに上記でこれをしましたinitialize
方法が少し深くダイビングをすることができます.我々が必要とする我々のクラスに振舞いを加えることmethods . メソッドは、プログラムの機能を実装します.以下に例を示します.
def one_plus_one
1 + 1
end
注意:Ruby , オブジェクトに属している(定義されている)メソッドは、ドットを追加することで、メソッド名を使用することができます.coolestCanadianCelebrity.one_plus_one
注意:オブジェクトのメソッドを呼び出します
coolestCanadianCelebrity
, ないcoolestCanadianCelebrity
自身.前方と前方!
以前から我々のコードに戻ってください
class CoolestCanadianActors
def initialize(first, last)
@first = first
@last = last
end
end
obj1 = Name.new("Ryan", "Reynolds")
私たちのオブジェクトはmethods それは物事を行う、彼らは物事を行う.我々
initialize
Rubyオブジェクトを作成するときにメソッドが実行されます.このメソッドの中で、あなたはfirst
, last
変数は、私たちが使用する変数とは少し異なります.@first = first
@last = last
上記のクラスは、クラス内で使用され、データを格納するインスタンス変数です.毎回新しいことを言いましょう
CoolestCanadianActors
オブジェクトは、我々はそれがどのように多くのTwitterのフォロワーを知ってほしいと作成されます.どのように私はフォロワーの数を格納することができますCoolestCanadianActors
クラス?インスタンス変数
class CoolestCanadianActors
def initialize(first, last)
@first = first
@last = last
@twitter_followers = twitter_followers
end
end
obj1 = Name.new("Ryan", "Reynolds", "17.7M")
上記のコードを見ると、新しく作成されたオブジェクトの例が表示されますobj1
. 今何?
さて、実世界オブジェクトを表現するクラスを作成する方法を知っているので、そのデータにアクセスする方法を学ぶことができます.属性アクセサーを使用します.
私たちが記事を通して取り組んでいて、属性アクセサーを越えるコードを戻すことができます.
class CoolestCanadianActors
def initialize(first, last)
@first = first
@last = last
@twitter_followers = twitter_followers
end
end
obj1 = Name.new("Ryan", "Reynolds", "17.7M")
To アクセス@first
, @last
and @twitter_followers
インスタンス変数Ryan Reynolds
オブジェクトのメソッドが必要です.定義しましょうgetter method それで、我々は我々のTwitterRanger信者属性にアクセスすることができます.
class CoolestCanadianActors
def initialize(first, last)
@first = first
@last = last
@twitter_followers = twitter_followers
end
def twitter_followers # getter method
@twitter_followers
end
end
obj1 = Name.new("Ryan", "Reynolds", "17.7M")
どのような場合は、各属性のメソッドを書くように感じていないのですか?ここでは、FeedAttributeアクセサーはここにありません.class CoolestCanadianActors
attr_accessor first, last, twitter_followers
def initialize(first, last, twitter_followers)
@first = first
@last = last
@twitter_followers = twitter_followers
end
end
attr_accessor ルビーのコア機能であり、getterメソッドとsetterメソッドを使用してインスタンス変数を生成するために使用します.注意:AttributeChorseアクセサーを利用できる様々な方法があります.私が更新したコードに戻ってみましょう.class CoolestCanadianActors
attr_accessor :first, :last, :twitter_followers
def initialize(first, last, twitter_followers)
@first = first
@last = last
@twitter_followers = twitter_followers
end
end
どのような場合は、読み取り専用のデータを取得したいtwitter_followers
属性?他の語では、データが公開され、ユーザーにアクセス可能です.よく書きます.
attr_reader :twitter_followers
これは、getterメソッドを書く必要がないことですobject
. The attr_reader
インスタンス変数の値を取得するGetterの関数に注意してください.ここで包みにしよう.パートIIIのために調整される滞在!
Reference
この問題について(Flatiron Bootcamp PTのためのCLIの構築2), 我々は、より多くの情報をここで見つけました https://dev.to/reyes2981/building-a-cli-for-flatiron-bootcamp-pt-2-f97テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol