码迷,mamicode.com
首页 > 其他好文 > 详细

类(一)

时间:2017-11-01 23:55:54      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:调用   on()   参数   bsp   pytho   author   class   utf-8   int   

#-*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

class Person():
    def __init__(self,name,money = 100): 
        self.name = name
        self.money = money

    def talk(self):
        print("%s is talking to the shop." % self.name)

    def buy(self, thing):
        print("%s has %s$ and buy a %s" % (self.name,self.money, thing))

p1 = Person("Jack")  #实例化一个对象
p1.talk()     #调用Person里的talk函数
p1.buy("meat")

 def __init__(self) 为构造函数,给实例化的类传参数,self为实例化的对象赋予的变量,相当于例子中的p1

类(一)

标签:调用   on()   参数   bsp   pytho   author   class   utf-8   int   

原文地址:http://www.cnblogs.com/sch01ar/p/7769046.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!