函数与类*******


print('hello,world')

##嵌套函数
def printName(isChinese,name,fName):
    def innerPrint(a,b):
        print('{0}{1}'.format(a,b))

    if isChinese:
        innerPrint(fName,name)
    else:
        innerPrint(name,fName)

printName(True,'chao','wang')
printName(False,'li','hua')


##nonlocal
def outer():
    b=20
    def inner():
        nonlocal b
        print('inner b:',b)
        b=60

    inner()
    print('outer b',b)

outer()


#面向对象
##类和对象的关系
class Student:
    def __init__(self,name,age):
        self.name=name
        self.age=age

    def say_age(self):
        print('{0}的年龄是{1}'.format(self.name,self.age))

Student('yang',18).say_age()
s1=Student('yang',18)
s1.score=100
s1.salary=1000
print(dir(s1))
print(isinstance(s1,Student))
stu2=Student
s1.say_age()
s2=stu2('yang',20)
s2.say_age()

##类属性
class Student:
    school='bupt'
    count=0

    def __init__(self,name,score):
        self.name=name
        self.score=score
        Student.count=Student.count+1

    def say_score(self):
        print('我们的学校是:',Student.school)
        print(self.name,'的分数是:',self.score)

f1=Student('yang',100)
f2=Student('wang',80)
f2.say_score()
f1.say_score()
print('一共创建了{0}个学生对象'.format(Student.count))


##类方法
class Student:
    school='bupt'

    @classmethod
    def printSchool(cls):
        print(cls.school)

Student.printSchool()

##析构方法
class person:
    def __del__(self):
        print('销毁对象{0}'.format(self))

p1=person()
p2=person()
del p2
print('程序结束')


##可调用方法__call__
class salaryAccount:
    def __call__(self,salary):
        print('算工资了')
        yearsalary=salary*12
        daysalary=salary//22
        hoursalary=daysalary//8
        return dict(yearsalary=yearsalary,monthsalary=salary,daysalary=daysalary,hoursalary=hoursalary)

#print(salaryAccount(9000))
s=salaryAccount()
print(s(9000))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值