第一部分 基础知识
第8章 函数
第9章 类
#2022.08.08 by zgw
#第八章 函数
#8.1 定义函数
#定义、实参和形参
from dataclasses import field
def greet_user(username):
print(f"hello, {username}")
greet_user('tom')
#8.2 传递实参
# 位置实参、关键字实参、默认值
#8.3 返回值
print('--------8.3--------')
def get_formatted_name(fname, lname, mname = ''): # 实参可选
if mname:
full_name = f"{fname} {mname} {lname}"
else:
full_name = f"{fname} {lname}"
return full_name.title()
musician = get_formatted_name('jimi', 'hendrix')
print(musician)
musician = get_formatted_name('john', 'hooker', 'lee')
print(musician)
#8.4 传递列表
#将列表传递给函数后,函数就可以对其进行修改。
#在函数中对这个列表所做的任何修改都是永久性的。
#8.5 传递任意数量的实参
print("n======传递任意数量的实参==========n")
def make_pizza(*toppings): # *toppings 创建一个空元组
print("Make Pizza n")
for topping in toppings:
print(topping)
print('n')
make_pizza('pepproni')
make_pizza('mushroonm', 'green peppers', 'extra cheese')
print("nexame2n")
def build_profile(first, last, **user_info): #创建空字典
user_info['first_name'] = first
user_info['last_name'] = last
return user_info
user_profile = build_profile('李', '小龙', location = '中国', field = '功夫')
print(user_profile)
#8.6 将函数存储在模块中
print("n-------- 8.6 -------n")
'''
import pizza
import pizza as p
from pizza import make_pizza, f2, ....
from pizza import make_pizza s mp
from pizza import *
'''
#第九章 类
print("n-------------第九章 类 ----------n")
#9.1 创建和使用类
class Dog:
"""一次模拟小狗的类"""
def __init__(self, name, age): #self是必须有的,而且是第一个
'''初始化属性'''
self.name = name
self.age = age
def sit(self):
print(f"{self.name} 小狗蹲下。")
def rool_over(self):
print(f"{self.name} 打滚。")
my_dog = Dog('Willie', 6)
print(f"My dog's name is : {my_dog.name}.")
print(f"My dog's age is : {my_dog.age} years old.")
my_dog.sit()
my_dog.rool_over()
#9.2 使用类和实例
print("n---------- 9.2 Car ---------n")
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def get_name(self):
long_name = f"{self.year} {self.make} {self.model}"
return long_name.title()
my_new_car = Car('audi', 'a6','2022')
print(my_new_car.get_name())
# 给属性指定默认值
# 修改属性的值:1. 直接修改 2.通过方法修改
#9.3 继承
# 父类、子类; 子类继承父类的所有属性和方法
print("n--------- 9.3 继承--------n")
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def get_name(self):
long_name = f"{self.year} {self.make} {self.model}"
return long_name.title()
class ElectricCar(Car):
def __init__(self, make, model, year):
super().__init__(make, model, year)
self.battery_size = 100
def print_battery(self):
print(f"此车的电池容量是: {self.battery_size} KWhn")
def fill_gas_tank(self): #可以重写父类方法
print("电动车没有油箱。n")
my_tida = ElectricCar('tida', 'auto','2022')
print(my_tida.get_name())
my_tida.print_battery()
#将实例用作属性
'''
class Car:
--skip--
class Battery:
--skip--
def print_battery(self)
print("-------")
clss ElectricCar(Car):
def __inin(self, make, model, year)
super().init(make, model, year)
self.battery = Battery()
my_car = ElectricCar('bmw', 'electric 400', 2022)
my_car.battery.print_battery()
'''
#9.4 导入类
print("n-------- 9.4 导入类 ---------n")
#9.5 Python标准库
print("n-------- 9.5 Python标准库----- n")
from random import randint, choice
print(randint(1,6))
players = ['aaa', 'bbb','ccc', 'ddd']
print(choice(players))
#9.6 类码风格
print("n9.6 类码风格:n类名采用驼峰命名法,即单词首字母大写。",end = ' ')
print("实例名和模块名采用小写,单词间加下划线。n")