tho*_*edb 6 language-agnostic oop
我正在寻找一种向初学者描述OO的好方法,虽然是类比.
目前我把一个班级比作一个购物清单,一个装满物品的购物车到一个物体.但我觉得这有点令人困惑.
优选地,类比将在代码示例(Ruby)中很好地反映出来,目前我有这个,并且它感觉很笨拙.
# First we create a class
class Shopping
# The items method gives us a list of items in the Shopping
def items
["apple", "cereal", "flour"]
end
end
# Create a new Shopping, called basket
basket = Shopping.new
# ask the basket what items it has
basket.items #=> ["apple", "cereal", "flour"]
Run Code Online (Sandbox Code Playgroud)
我已经看到它描述为Man是一个类,而Steve是一个对象(Man的实例).史蒂夫有金发,6英尺,重180磅等.
然后你可以继承,所以Man继承Person,Person继承Animal,然后继续.