Skip to content

Commit a1cb1da

Browse files
committed
[C]增加了第9章类的相关操作,包括实例话,属性值的修改,函数声明和调用,还有类的导入操作。
Signed-off-by: VincentJ <im.vincentj@gmail.com>
1 parent b3e48f4 commit a1cb1da

File tree

7 files changed

+114
-0
lines changed

7 files changed

+114
-0
lines changed

.idea/codeStyles/codeStyleConfig.xml

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chapter6/confirmed_users.py

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
confirmed_users.append(current_user)
1313
# 显示所有已验证的用户
1414
print("\nThe following users have been confirmed:")
15+
1516
for confirmed_user in confirmed_users:
1617
print(confirmed_user.title())
1718

chapter7/trans_array.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# 传递列表
22

3+
34
def greet_users(names):
45
for name in names:
56
print('Hello,' + name)

chapter8/Car.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class Car:
2+
"""一次模拟汽车的简单尝试"""
3+
4+
def __init__(self, make, model, year):
5+
"""初始化描述汽车的属性"""
6+
self.make = make
7+
self.model = model
8+
self.year = year
9+
# 设置默认属性值
10+
self.odometer_reading = 0
11+
12+
# 提供外部更新内部属性的函数
13+
def update_odometer(self, mileage):
14+
"""
15+
将里程表读数设置为指定的值
16+
禁止将里程表读数往回调
17+
"""
18+
if mileage >= self.odometer_reading:
19+
self.odometer_reading = mileage
20+
else:
21+
print("You can't roll back an odometer!")
22+
23+
def get_descriptive_name(self):
24+
"""返回整洁的描述性信息"""
25+
long_name = str(self.year) + ' ' + self.make + ' ' + self.model
26+
return long_name
27+
28+
def read_odometer(self):
29+
"""打印一条指出汽车里程的消息"""
30+
print("This car has " + str(self.odometer_reading) + " miles on it.")
31+
32+
33+
# 继承父类,使用super实例化
34+
class ElectricCar(Car):
35+
def __init__(self, make, model, year):
36+
super().__init__(make, model, year)
37+
# 子类特有的属性
38+
self.battery_size = 70
39+
# 类作为属性的一部分
40+
self.battery = Battery()
41+
42+
def read_odometer(self):
43+
# 重写父类函数
44+
print("override func")
45+
46+
47+
class Battery:
48+
"""一次模拟电动汽车电瓶的简单尝试"""
49+
50+
def __init__(self, battery_size=70):
51+
"""初始化电瓶的属性"""
52+
self.battery_size = battery_size
53+
54+
def describe_battery(self):
55+
"""打印一条描述电瓶容量的消息"""
56+
print("This car has a " + str(self.battery_size) + "-kWh battery.")

chapter8/ClassRun.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# 从一个模块导入一个类,多个类
2+
from chapter8.Car import Car, ElectricCar
3+
# 导入整个模块
4+
# import chapter8.Car
5+
# 从一个模块导入所有类
6+
# from chapter8.Car import *
7+
from chapter8.Dog import Dog
8+
9+
my_dog = Dog("haha", 6)
10+
print("My dog's name is " + my_dog.name.title() + '.')
11+
print("My dog's age is " + str(my_dog.age) + '.')
12+
13+
my_dog.roll_over()
14+
my_dog.sit()
15+
16+
my_car = Car('audi', 'a4', 2016)
17+
print(my_car.get_descriptive_name())
18+
my_car.read_odometer()
19+
# 直接修改属性值
20+
my_car.odometer_reading = 100
21+
my_car.read_odometer()
22+
# 通过函数进行修改属性会
23+
my_car.update_odometer(200)
24+
my_car.read_odometer()
25+
26+
my_elecar = ElectricCar('tesla', 'a9', 2018)
27+
print(my_elecar.get_descriptive_name())

chapter8/Dog.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Dog:
2+
"""一次模拟小狗的简单尝试"""
3+
4+
# 这是2个下划线
5+
def __init__(self, name, age):
6+
"""初始化属性name和age"""
7+
self.name = name
8+
self.age = age
9+
10+
def sit(self):
11+
"""模拟小狗坐下"""
12+
print(self.name.title() + ' is now sitting.')
13+
14+
def roll_over(self):
15+
"""模拟小狗翻滚"""
16+
print(self.name.title() + ' is rolling over.')

chapter8/tip

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
类编码风格
2+
1.你必须熟悉有些与类相关的编码风格问题, 在你编写的程序较复杂时尤其如此。
3+
2.类名应采用驼峰命名法 , 即将类名中的每个单词的首字母都大写, 而不使用下划线。 实例名和模块名都采用小写格式, 并在单词之间加上下划线。
4+
3.对于每个类, 都应紧跟在类定义后面包含一个文档字符串。 这种文档字符串简要地描述类的功能, 并遵循编写函数的文档字符串时采用的格式约定。 每个模块也都应包含一个文
5+
档字符串, 对其中的类可用于做什么进行描述。
6+
4.可使用空行来组织代码, 但不要滥用。 在类中, 可使用一个空行来分隔方法; 而在模块中, 可使用两个空行来分隔类。
7+
5.需要同时导入标准库中的模块和你编写的模块时, 先编写导入标准库模块的import 语句, 再添加一个空行, 然后编写导入你自己编写的模块的import 语句。 在包含多
8+
条import 语句的程序中, 这种做法让人更容易明白程序使用的各个模块都来自何方。

0 commit comments

Comments
 (0)