Class Basic Notes Flashcards
1
Q
What is a class?
A
a class is a blueprint from which individual objects of the same class are created.
2
Q
how do you create a class?
A
to create a class we use the class keyword the name of a class must begin with a capital letter we can define methods within a class
3
Q
how do you create a new instance of class?
A
by doing [class_name].new
4
Q
what gets executed when you call Class.new?
A
the defined initialized method is what gets executed
5
Q
what is a getter method?
A
a getter method is used to refer to the value of the attribute you assigned it to. class Cat def initialize(name, color, age) @name = name @color = color @age = age end
def get_name
@name
end
end
cat_1 = Cat.new(“Sennacy”, “brown”, 3)
p cat_1.get_name # “Sennacy”
6
Q
what is a setter method?
A
we use them to modify the @attribute class Cat def initialize(name, color, age) @name = name @color = color @age = age end
# getter
def age
@age
end
# setter def age=(number) @age = number end end
cat_1 = Cat.new(“Sennacy”, “brown”, 3)
p cat_1 #
cat_1.age = 42
p cat_1 #