Introduction to Classes

class Animal(object):   #类的声明,括号内填父类,object是最高父类
    pass    #如果类体暂时为空,则需要填pass作为占位符
class Animal(object):
    def __init__(self):    #类似构造函数的东西,这里需要传入一个self作为指向构造出的对象的引用,和java不同这里的'this'需要显式的传进去
        pass       #暂时为空,又用到了占位符
class Animal(object):
    def __init__(self, name):     #传入了新的参数
        self.name = name       #构造函数中可以直接创建一个新的属性,如这里的name

zebra = Animal("Jeffrey")   #真正创建一个对象的时候不需要传self,把后面的参数对应传入就好

print zebra.name     #在类的外部想要访问某对象的属性,直接用 .  即可

When dealing with classes, you can have variables that are available everywhere (global variables), variables that are only available to members of a certain class (member variables), and variables that are only available to particular instances of a class (instance variables). 关于这个玩意。。codecademy上面讲的一般般以后再慢慢拓展。
The same goes for functions: some are available everywhere, some are only available to members of a certain class, and still others are only available to particular instance objects.

class Animal(object):
    """Makes cute animals."""
    is_alive = True
    def __init__(self, name, age):
        self.name = name
        self.age = age
    # Add your method here!
    def description(self):   #添加自己的方法,注意这里还是要显式传入一个self,当然实际调用的时候不用自己传。
        print self.name
        print self.age
        
hippo = Animal("Baka",11)
hippo.description()
class Animal(object):
    """Makes cute animals."""
    is_alive = True            #这里的is_alive 和 health 即member variable
    health = "good"
    def __init__(self, name, age):
        self.name = name
        self.age = age
    # Add your method here!
    def description(self):
        print self.name
        print self.age
        
hippo = Animal("Baka",11)
hippo.description()

sloth = Animal("Najin", 10)
ocelot = Animal("Modo", 5)

print hippo.health
print sloth.health
print ocelot.health  #打印出来都是good
class ShoppingCart(object):
    """Creates shopping cart objects
    for users of our fine website."""
    items_in_cart = {}         #这里的member variable是一个字典
    def __init__(self, customer_name):
        self.customer_name = customer_name

    def add_item(self, product, price):
        """Add product to the cart."""
        if not product in self.items_in_cart:
            self.items_in_cart[product] = price      #向购物车里添加item,注意对字典添加item的方法
            print product + " added."
        else:
            print product + " is already in the cart."

    def remove_item(self, product):
        """Remove product from the cart."""
        if product in self.items_in_cart:
            del self.items_in_cart[product]
            print product + " removed."
        else:
            print product + " is not in the cart."

Inheritance

Inheritance is the process by which one class takes on the attributes and methods of another, and it's used to express an is-a relationship. For example, a Panda is a bear, so a Panda class could inherit from a Bear class.

class Employee(object):
    """Models real-life employees!"""
    def __init__(self, employee_name):
        self.employee_name = employee_name

    def calculate_wage(self, hours):
        self.hours = hours
        return hours * 20.00

# Add your code below!
class PartTimeEmployee(Employee):   #继承的语法,首先声明中括号里填写父类名称
    
    def calculate_wage(self, hours):   #方法的覆盖,很好理解
        self.hours = hours   
        return hours * 12.00
    
    def full_time_wage(self, hours):   #对应方法被覆盖以后,如果还是想要直接调用父类的此方法,可用super
        return super(PartTimeEmployee, self).calculate_wage(hours)    #这里super的实现和java不太一样,super(子类名,self).方法名(参数)
    
milton = PartTimeEmployee("Mike")

print milton.full_time_wage(10)
 posted on 2015-06-07 15:35  絮状颗粒  阅读(171)  评论(0)    收藏  举报