Ruby/Class/inheritance

Материал из Wiki.crossplatform.ru

Версия от 17:10, 26 мая 2010; (Обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Содержание

Add new constructor

class CD
  include Comparable
  @@plays = 0
  attr_reader :name, :artist, :duration
  attr_writer :duration
  def initialize(name, artist, duration)
    @name     = name
    @artist   = artist
    @duration = duration
    @plays    = 0
  end
  def to_s
    "CD: #@name--#@artist (#@duration)"
  end
end
class NewCD < CD
  def initialize(name, artist, duration, lyrics)
    super(name, artist, duration)
    @lyrics = lyrics
  end
  def to_s
    super + " [#@lyrics]"
  end
end



Basing One Class on Another: a Demo

class Animal
  def initialize(color)
    @color = color
  end
  def get_color
    return @color
  end
end
class Dog < Animal
  def initialize(color, sound)
    super(color)
    @sound = sound
  end
  def get_sound
    return @sound
  end
end
dog = Dog.new("brown", "Bark")
puts "The new dog is " + dog.get_color
puts "The new dog says: " + dog.get_sound + "" + dog.get_sound



Basing one class on another is called inheritance.

# Creating a class named A and then using the A class as a base class for another class, B:
# Note that the syntax class B < A indicates that the B class inherits all that A class has
 
class A
.
.
.
end
class B < A
.
.
.
end



different types of people

class Person
  def initialize(name)
    @name = name
  end
  def name
    return @name
  end
end
class Doctor < Person
  def name
    "Dr. " + super
  end
end



Extends class

class Hello
  def howdy
    greeting = "Hello, Matz!"
    puts greeting
  end
end
class Goodbye < Hello
  def solong
    farewell = "Goodbye, Matz."
    puts farewell
  end
end
friendly = Goodbye.new
friendly.howdy
friendly.solong



how inheritance works in code form

class ParentClass
  def method1
    puts "Hello from method1 in the parent class"
  end
  def method2
    puts "Hello from method2 in the parent class"
  end
end
class ChildClass < ParentClass
  def method2
    puts "Hello from method2 in the child class"
  end
end
my_object = ChildClass.new
my_object.method1
my_object.method2



If the class Name were in a different file, you would just require that file first

class Name
  attr_accessor :given_name, :family_name
end
////////////////
File: address.rb
#!/usr/bin/env ruby
require "name"
class Address < Name
  attr_accessor :street, :city, :state, :country
end
a = Address.new
puts a.respond_to?(:given_name)



Module and class hierarchy

module A
end                 # Empty module
module B 
   include A 
end                 # Module B includes A
class C 
   include B 
end                 # Class C includes module B
puts C < B               # => true: C includes B
puts B < A               # => true: B includes A
puts C < A               # => true
puts Fixnum < Integer    # => true: all fixnums are integers
puts Integer <Comparable # => true: integers are comparable
puts Integer < Fixnum    # => false: not all integers are fixnums
puts String < Numeric    # => nil: strings are not numbers
puts A.ancestors         # => [A]
puts B.ancestors         # => [B, A]
puts C.ancestors         # => [C, B, A, Object, Kernel]
puts String.ancestors    # => [String, Enumerable, Comparable, Object, Kernel]
                    # Note: in Ruby 1.9 String is no longer Enumerable
puts C.include?(B)       # => true
puts C.include?(A)       # => true
puts B.include?(A)       # => true
puts A.include?(A)       # => false
puts A.include?(B)       # => false
puts A.included_modules  # => []
puts B.included_modules  # => [A]
puts C.included_modules  # => [B, A, Kernel]



Structuring Your Pets Logically

class Pet
  attr_accessor :name, :age, :gender, :color
end
class Cat < Pet
end
class Dog < Pet
end
class Snake < Pet
end



Subclass Array class

class Library < Array
  def add_book(author, title)
    self << [author, title]
  end
  def search_by_author(key)
    reject { |b| !match(b, 0, key) }
  end
  def search_by_author_or_title(key)
    reject { |b| !match(b, 0, key) && !match(b, 1, key) }
  end
  :private
  def match(b, index, key)
    b[index].index(key) != nil
  end
end
l = Library.new
l.add_book("author 1", "title 1")
l.add_book("author 2", "title 2")
p l.search_by_author("author 1")
p l.search_by_author_or_title("title 1")