ruby Ruby对象和类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby Ruby对象和类相关的知识,希望对你有一定的参考价值。

# Control Flow

a = 3

if a == 1
    puts "a is 1"
elsif a == 2
    puts "a is 2"
else 
	puts " I don't know the value of a"
end

-------
# case statement 

case a
when 1
    puts "a is 1"
when 2
    puts "a is 2"
else 
	puts " I don't know the value of a"
end
# Ruby Class

# Everything in Ruby is a class. A class is sort of a template we use to represent things 
# in our code. We use these templates to build instances of an object. 
# Think of a class as schematic that we can use to build objects.

# We create a class using the class keyword: 

class Car
  def honk
    puts "Beeeeeeeeep!"
  end
end


# We now have a class called Car. We can use it to create instances or objects of that 
# class using the new function. Those objects will have a function called honk that we 
# can call. Functions attached to objects are also called instance methods or just methods for short.

my_car = Car.new
my_car.honk


# With the dot we can call the new method of the Car 
# class and we also use it to call methods of instances of classes like Car.


# There is also a method called class we can invoke to see the class of an instance.

# Let's look at the class of a Car objects as well as some other value types we've been using in our examples:


class Car
  def honk
    puts "Beeeeeeeeep!"
  end
end

my_car = Car.new

puts my_car.class
puts "cosa".class
puts 4.class




# State in classes


# Aside from being a container for functions
# classes and instances can also save values specific to an instance. We call this state in programming

# think of characteristics that can change from car to car. (color - make - model - year)
# We want to have a way to represent that in our class.



------------------


# Instance variables


# A class can have instance variables
# An instance variable is a variable that contains a value specific to each instance of the class. 
# These variables are also accessible by the methods of that instance.
# We mark a variable as an instance variable preceding its name with an at @ symbol.

# Instance of person (age - gender)
# Instance of car (model - make)

class Car
  def initialize(color)
    @color = color
  end

  def honk
    puts "Beeeeeeeeep!"
  end

  def print_color
    puts @color
  end
end

my_car = Car.new "red"
other_car = Car.new "grey"

my_car.print_color
other_car.print_color



--------------


# Initialization Method

# That is a method called automatically when you create an instance with the new method
# This is called a constructor, a function that is called when instances a created.
# This is the way we set values for instance variables, by sending them as values for new.
# The values are in turn sent to initialize where we can set our instance variables.


# Class variables


# We can also create class variables that are shared by all instances of that class. 
# For example we can count the amount of cars we have created so far:


class Car
  @@total = 0

  def initialize(color)
    @color = color
    @@total = @@total + 1
  end

  def honk
    puts "Beeeeeeeeep!"
  end

  def print_color
    puts @color
  end

  def print_total
    puts "So far we've got #{@@total} cars."
  end
end

my_car = Car.new "red"
other_car = Car.new "rainbow"

other_car.print_total


------------


# Accessor methods

# By default we can't directly access the instance variables of a given instance. 
# Only methods have access to these values. We can use attr_accessor to provide access to them. 
# With direct access to the color variable we can do away with the print_color method:



class Car
  attr_accessor :color

  def initialize(color)
    @color = color
  end

  def honk
    puts "Beeeeeeeeep!"
  end
end

my_car = Car.new "yellow"
other_car = Car.new "brown"

my_car.color = "grey"

puts my_car.color
puts other_car.color



------------

# Inheritance

# Ruby also allows us to extend a class by using inheritance. 
# This is useful if we have a generic class that we want to 
# use as a base to create more specific classes:

class Animal
  def initialize(name)
    @name = name
  end

  def describe
    puts "This animal's name is #{@name}"
  end
end

class Dog < Animal
end

class Cat < Animal
end

class Human < Animal
  def initialize(name, salary)
    super(name)
    @salary = salary
  end

  def describe_with_salary
    puts "This human's name is #{@name} and its salary is #{@salary}"
  end
end

dog = Dog.new "Winston Furchill"
cat = Cat.new "David Meowie"
human = Human.new "Johnny Appleseed", 12000

dog.describe
cat.describe
human.describe
human.describe_with_salary


--------------------

# Class methods and instance methods
# Just like we can create class variables, we can also create class methods. 
# We can create class methods using the self.methodname syntax:


class Car
  attr_accessor :color

  @@total = 0

  def self.total
    return @@total
  end

  def initialize(color)
    @color = color
    @@total = @@total + 1
  end

  def honk
    puts "Beeeeeeeeep!"
  end
end

my_car = Car.new "red"
other_car = Car.new "rainbow"

puts "So far we've got #{Car.total} cars."

以上是关于ruby Ruby对象和类的主要内容,如果未能解决你的问题,请参考以下文章

ruby中的实例变量和类变量

ruby 暂时覆盖ruby对象方法

ruby Ruby对象空间

雷林鹏分享:Ruby 面向对象

什么是ruby?

Ruby 面向对象