Created
July 11, 2013 03:30
-
-
Save TGSmith/edf790f0dd0fdc96be99 to your computer and use it in GitHub Desktop.
Solution for P6. OO Terminology
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Solution for Challenge: P6. OO Terminology. Started 2013-07-11T03:30:13+00:00 | |
class Bicycle | |
attr_reader :color, :gears, :move, :brake, :wheels | |
def initialize(args={}) | |
@color = args[:color] || "silver" | |
@gears = args[:gears] || "single" | |
@wheels = 2 | |
@move = false | |
@brake = false | |
#@type = arg[:type] | |
end | |
def pedaling! | |
#if @move == true @move = false : @move = false | |
@move = true if @move == false | |
@brake = false if @brake == true | |
"The bicycle is moving!" | |
end | |
def braking! | |
#@brake == true && pedaling! == false ? @move == false : @move = true | |
@move = false if @move == true | |
@brake = true if @brake == false | |
"The bicycle is stopped!" | |
end | |
end | |
class Gears < Bicycle | |
def initialize(args) | |
@gear = args[:gear] | |
@current_gear = args[:current_gear] | |
end | |
def shift_gear_up | |
@current_gear += 1 | |
end | |
def shift_gear_down | |
@current_gear -= 1 | |
end | |
end | |
class Wheels < Bicycle | |
def initialize(args) | |
@wheel_size = args[:wheel_size] | |
end | |
end | |
bicycle = Bicycle.new | |
p bicycle.color | |
p "move after initializtion = #{bicycle.move}" | |
p "brake after init = #{bicycle.brake}" | |
p bicycle.braking! | |
p "move after braking! = #{bicycle.move}" | |
p "brake after braking! = #{bicycle.brake}" | |
bicycle = Bicycle.new | |
p bicycle.move | |
p bicycle.brake | |
bicycle.pedaling! | |
p bicycle.move | |
p bicycle.brake | |
#attr_reader, attr_writer and attr_accessor methods define the "getter" and "setter" methods for instance variables. | |
#attr_reader reads the object (which can be modified), attr_writer allows you to change the attribute (it replaces it) | |
#attr_reader, writer, or accessor | |
##These are built in class methods that take variales as parameters. | |
##By calling these methods on the class you can either: | |
##Read what the variable is in the class(reader) | |
##Reassign the variable from outside of the class(writer) | |
##Do both at the same time(accessor) | |
#A private type attribute or method can only be accessed within the class itself(private to this class itself). | |
#A public type attribute or method can be accessed from anywhere(default). | |
# | |
#An instance method is method that is called on an instance of a class. You have to create a instance of a class to use them. | |
#A class method is a method that resides at class level. They only exist in the object that defined them(the class). | |
#class Test | |
# def self.class_method | |
# "I'm a class method." | |
# end | |
#end | |
# | |
#Class variables are variables that are shared across all instances of a class. They are accessible from instance methods and class methods with accessors from outside the class itself. | |
#--they are denoted by two @@ signs in front of the variable name. | |
# | |
#Instance variables are variables that can only be accessed within class methods. They are denoted by one @ in front of the variable. They can be referenced in any method in that class. | |
# | |
#Inheritance is used to indicate that one class will get most or all of its features from a parent class. This happens when you write class child < class parent. | |
# class Person | |
# attr_accessor :name | |
# def initialize(name) | |
# @name = name | |
# end | |
# end | |
# class Employee < Person | |
# attr_accessor :salary | |
# def pay | |
# "paid this months salary: £#{@salary}" | |
# end | |
# def drink_coffee | |
# 'glug glug' | |
# end | |
# end | |
# class Developer < Employee | |
# def make_bugs | |
# "#{@name} does some typing" | |
# end | |
# end | |
# class Tester < Employee | |
# def test | |
# "#{@name} does some testing" | |
# end | |
# end | |
#Composition is an alternative to inheritance and is used to package code into modules that is used in many different places and situations. | |
# | |
#class Code | |
# def initialize(person) | |
# @person = person | |
# end | |
# def make_bugs | |
# "#{@person.name} does some typing" | |
# end | |
# end | |
# class Developer < Employee | |
# def make_bugs | |
# Code.new(self).make_bugs | |
# end | |
# end | |
# Single Responsibility says that a class should have one, and only one, reason to change. The methods of a class should change for the same reasons, they should not be affected by different forces that change at different rates. | |
#Law of Demeter is the design principle that only objects that are closely related to each other should be able to read or interact with each other. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment