!/usr/bin/env python
coding: utf-8 -*-
开发团队: 奶豆的小铺
开发人员: bcz
开发时间: 2020/11/21 20:36
函数式编程
def foo(name,age,sex,content):
print(name,age,sex,content)
foo('小明',23,'man','最稀罕美女')
foo('小红',25,'man','最稀罕美女')
foo('小李',24,'man','最稀罕美女')
foo('小黄',27,'man','最稀罕美女')
print('*'*12+'面向对象编程'+'*'*12)
class Bar(object):
def foo(self,name,age,sex,content):
print(name, age, sex, content)
obj = Bar()
obj.foo('小明',23,'man','最稀罕美女')
obj.foo('小红',25,'man','最稀罕美女')
obj.foo('小黄',27,'man','最稀罕美女')
在类内的函数称为:方法
self 指的是调用方法的对象(中间人),中间人可以添加变量
print('*'*12+'面向对象编程'+'*'*12)
class Bar1(object):
def foo(self):
print(self.name,self.age, self.sex, self.content)
obj1 = Bar1()
obj1.name = 'mary'
obj1.age =29
obj1.sex = 'woman'
obj1.content ='最稀罕帅哥'
obj1.foo()
print('*'*12+'面向对象编程 构造方法封装属性'+'*'*12)
class Person():
def __init__(self,name,age):
self.n = name
self.a = age
self.blood_type ='o'#公共属性
def show_person(self):
print(self.n,self.a,self.blood_type)
john = Person('john',23)
john.show_person()
mary = Person('mary',27)
mary.show_person()
print('*'*12+'面向对象编程 '+'*'*12)
面向对象编程适用场景:如果多个函数的参数有一些相同时,转换为面向对象编程
继承特性
class Father():
def drink(self):
print('能喝酒')
def football(self):
print('he likes to play football')
class Son(Father):
def play(self):
print('he likes playing')
def drink(self):
#如果非要想执行父类的此方法,参数分别是类名,self,此方法
super(Son, self).drink()
print('喝饮料')
s =Son()
s.drink()
s.play()