ex:
class Rabbit
def initialize( name)
@name = name
end
def run
puts “run”
end
end
initialize:
this method is called when the object is created
this is a private method. Hence, when using new to create object, the new method will call initialize
instance variable:
the variable that starts with @ is instance variable
naming convention:
class name: CuteRabbit
file name : cute_rabbit.rb
private:
the method is private
define methods after private
ex:
private
def test
end
protected:
the method is protected
modify existing class
ex: the Array is built-in class.
class Array
def test
end
end
now the Array has new method test
class variable:
the variable that starts with @@ is class variable
class method:
ex:
def self.test
end
inheritance:
ex:
class Circle < Shape
end
if class Square is defined inside class Shape, you must access Square with
Shape::Square outside class Shape
ex:
class Test < Shape::Square
end
constant in the class
if TEST is the constant of class Rabbit,
we can access Test using Rabbit::Test
沒有留言:
張貼留言