3. Обекти и класове

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Favorites, Groups & Events

    3. Обекти и класове - Presentation Transcript

    1. 3. Обекти и класове stefan = Lecturer.new “Стефан Кънев” nikolay = Lecturer.new “Николай Бачийски” Lecture.new ‘15‐10‐2008’, stefan, nikolay
    2. 15 мин
    3. Хиперкуб
    4. 3. 1415926535 8979323846 2643383279 5028841971 6939937510 5820974944 5923078164 0628620899 8628034825 3421170679 8214808651 3282306647 0938446095 5058223172 5359408128 4811174502 8410270193 8521105559 6446229489 5493038196 4428810975 6659334461 2847564823 3786783165 2712019091 4564856692 3460348610 4543266482 1339360726 0249141273 7245870066 0631558817 4881520920 9628292540 9171536436 7892590360 0113305305 4882046652 1384146951 9415116094 3305727036 5759591953 0921861173 8193261179 3105118548 0744623799 6274956735 1885752724 8912279381 8301194912 9833673362 4406566430 8602139494 6395224737 1907021798 6094370277 0539217176 2931767523 8467481846 7669405132 0005681271 4526356082 7785771342 7577896091 7363717872 1468440901 2249534301 4654958537 1050792279 6892589235 4201995611 2129021960 8640344181 5981362977 4771309960 5187072113 4999999837 2978049951 0597317328 1609631859 5024459455 3469083026 4252230825 3344685035 2619311881
    5. Обекти и класове
    6. class Vector end direction = Vector.new
    7. class Vector def initialize(x, y, z) @x, @y, @z = x, y, z end def length (@x * @x + @y * @y + @z * @z) ** 0.5 end def to_s() “(#@x, #@y, #@z)” end end orientation = Vector.new 1.0, 0.0, 1.0 puts orientation.length
    8. @name, @age
    9. class Vector # ... def x() @x end def y() @y end def z() @z end end v = Vector.new 1.0, 0.0, 1.0 puts v.x + v.y + v.z()
    10. name=
    11. class Thing def name=(value) puts “Name is set to #{value}” end end >> bacon = Thing.new >> bacon.name = “Bacon” Name is set to Bacon >> bacon.name=(“Chunky bacon”) Name is set to Chunky bacon
    12. dir = Vector.new 1.0, 0.0, 1.0 dir.z = 5.0 dir.x, dir.y = 1.0, ‐1.0
    13. class Vector # ... def x=(value)  @x = value end def y=(value)  @y = value end def z=(value)  @z = value end end
    14. class Vector attr_accessor :x, :y, :z def initialize(x, y, z) @x, @y, @z = x, y, z end end
    15. attr_accessor
    16. attr_accessor :name def name(value) @name end def name=(value) @name = value end
    17. attr_reader attr_writer
    18. Meyer’s Uniform Access Principle
    19. class Person attr_reader :first, :last def initialize(first, last) @first, @last = first, last end end
    20. class Person attr_reader :first, :last def initialize(first, last) @first, @last = first, last end def business_card “#@first #@last” end end
    21. class Person attr_reader :first, :last def initialize(first, last) @first, @last = first, last end def business_card “#@first #@last” end def solve_problem(problem) until problem.solved? or self.bored? stare_at problem try_something_random end end end
    22. class Programmer < Person end
    23. class Programmer < Person def solve_problem(problem) thoughts = self.think_about problem solution = self.invent_solution thoughts self.write_tests_about solution self.implement solution self.party! end end
    24. class SmartGuy < Person def business_card super() + “, Ph.D.” end end
    25. Няма множествено наследяване
    26. Статични методи
    27. Методи на класа
    28. Променливи на класа
    29. class Person def initialize(first, last) @first, @last = first, last @@count ||= 0 @@count += 1 end end
    30. @@count ||= 12
    31. @@count = @@count || 12
    32. class Person def initialize(first, last) @first, @last = first, last @@count ||= 0 @@count += 1 end def Person.count @@count end end >> Person.new “Thelonious”, “Monk” >> Person.new “Glenn”, “Gould” >> puts Person.count 2
    33. class Person def self.count @@count end end
    34. Оператори
    35. class Vector attr_accessor :x, :y, :z def initialize(x, y, z) @x, @y, @z = x, y, z end def length (@x * @x + @y * @y + @z * @z) ** 0.5 end def to_s() “(#@x, #@y, #@z)” end end
    36. c = a + b d = c * 7
    37. class Vector def +(other) Vector.new self.x + other.x, self.y + other.y, self.z + other.z end def *(n) Vector.new @x * n, @y * n, @z * n end end
    38. >> a = Vector.new 1.0, 0.0, 0.0 >> b = Vector.new 0.0, 1.0, 0.0 >> puts a + b (1.0, 1.0, 0.0) >> puts a + b * 2 (1.0, 2.0, 0.0)
    39. !   ~   @+  @‐ **  *   /    %   +   ‐ <<  >>  &   | ^   <   <=  >=  >   ==  === !=  =~  !~  <=>
    40. &&  ||  ..  ... ?:  =   **= *=  /=  %=  +=  ‐=  >>= <<= &&= &=  ||= |=  ^=  and not or
    41. 3. Обекти и класове stefan = Lecturer.new “Стефан Кънев” nikolay = Lecturer.new “Николай Бачийски” Lecture.new ‘15‐10‐2008’, stefan, nikolay
    42. ДИС‐2
    43. 0 = 1
    44. 1 ∫x dx
    45. 1 1 1 ∫x dx = x x − ∫ x xd
    46. 1 1 1 ∫x dx = x x − ∫ x xd = 1 = 1 − ∫ x(− 2 )dx x
    47. 1 1 1 ∫x dx = x x − ∫ xd x = 1 1 = 1 − ∫ x(− 2 )dx = 1 + ∫ dx x x
    48. 1 1 1 ∫x dx = x x − ∫ xd x = 1 1 = 1 − ∫ x(− 2 )dx = 1 + ∫ dx x x
    49. aquarius@arrakis:$ irb >> puts 0 == 1 false
    50. Fixnum
    51. class Fixnum alias broken_equal? == end
    52. >> puts 0.broken_equal?(1) false >> puts 2.broken_equal?(2) true
    53. class Fixnum alias broken_equal? == def ==(other) if (self.equal?(0) and other.equal?(1)) or (self.equal?(1) and other.equal?(0)) true else self.broken_equal?(other) end end end
    54. >> puts 0 == 1 true >> puts 1 == 1 true >> puts 2 == 1 false
    55. Александър Макендонски не е съществувал Всеки триъгълник е равностраннен
    56. class Coin end
    57. class Coin def initialize(value) @value = value end def pick_up(person) person.enrich self.value end def put_on_train_rail! self.flatten end end
    58. pesho = Coin.new 0.50
    59. pesho = Coin.new 0.50 def pesho.pick_up(person) person.get_hopes_high person.humiliate person.make_sad end
    60. Хиперкуб
    61. 3. 1415926535 8979323846 2643383279 5028841971 6939937510 5820974944 5923078164 0628620899 8628034825 3421170679 8214808651 3282306647 0938446095 5058223172 5359408128 4811174502 8410270193 8521105559 6446229489 5493038196 4428810975 6659334461 2847564823 3786783165 2712019091 4564856692 3460348610 4543266482 1339360726 0249141273 7245870066 0631558817 4881520920 9628292540 9171536436 7892590360 0113305305 4882046652 1384146951 9415116094 3305727036 5759591953 0921861173 8193261179 3105118548 0744623799 6274956735 1885752724 8912279381 8301194912 9833673362 4406566430 8602139494 6395224737 1907021798 6094370277 0539217176 2931767523 8467481846 7669405132 0005681271 4526356082 7785771342 7577896091 7363717872 1468440901 2249534301 4654958537 1050792279 6892589235 4201995611 2129021960 8640344181 5981362977 4771309960 5187072113 4999999837 2978049951 0597317328 1609631859 5024459455 3469083026 4252230825 3344685035 2619311881
    62. pesho = Coin.new 0.50 def pesho.pick_up(person) person.get_hopes_high person.humiliate person.make_sad end
    63. method_missing send
    64. method_missing(name, *args)
    65. class Thing def foo puts \"I pity the foo!\" end def method_missing(name, *args) puts \"Called #{name} with #{args.inspect}\" end end
    66. class Thing def foo puts \"I pity the foo!\" end def method_missing(name, *args) puts \"Called #{name} with #{args.inspect}\" end end >> thing = Thing.new >> thing.foo I pity the foo >> thing.larodi 1, \"bar\", [] Called larodi with [1, \"bar\", []]
    67. send
    68. send(name, arg0, arg1, …)
    69. >> people = [\"Monk\", \"Evans\", \"Coltrane\"] >> puts people.size 3 >> puts people.send(:size) 3 >> people.send :insert, 0, \"Davis“ >> puts people.size 4 >> people.send :<<, \"Rollins“ >> puts people.size 5
    70. class Mapper def initialize(list) @list = list end def method_missing(name, *args) @list.map { |x| x.send(name) } end end >> mapper = Mapper.new [\"Coltrane\", \"Evans\", \"Monk\"] >> mapper.size [8, 5, 4] >> puts mapper.downcase [\"coltrane\", \"evans\", \"monk\"]
    71. class Array alias old_star * def *(other = nil) if other.nil? Mapper.new self else self.old_star(other) end end end
    72. >> people = [\"Coltrane\", \"Evans\", \"Monk\"] >> people.*.size [8, 5, 4] >> people.*.downcase [\"coltrane\", \"evans\", \"monk\"] >> people * 2 [\"Coltrane\", \"Evans\", \"Monk\", \"Coltrane\",  \"Evans\", \"Monk\"]
    73. users.*.email

    + Stefan KanevStefan Kanev, 2 years ago

    custom

    476 views, 0 favs, 0 embeds more stats

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 476
      • 476 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 85
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories