码农之家
这篇文章主要知识点是关于Python编程,从入门到实践,类,Python编程:从入门到实践,PHP中的日期时间处理利器Carbon实例用法 快速上手Java中的Properties集合类 C++无痛实现日期类的示例代码 Java中抽象类与方法的重写方式 Java异常类型以及异常处理总结 的内容,如果大家想对相关知识点有系统深入的学习,可以参阅以下电子书
创建一个名为Restaurant 的类,其方法__init__() 设置两个属性:restaurant_name 和cuisine_type 。创建一个名 为describe_restaurant() 的方法和一个名为open_restaurant() 的方法,其中前者打印前述两项信息,而后者打印一条消息,指出餐馆正在营业。
class Restraurant(): """docstring for Restraurant""" def __init__(self, restaurant_name,cuisine_type): self.restaurant_name = restaurant_name self.cuisine_type = cuisine_type def describe_restaurant(self): print(self.restaurant_name,":",self.cuisine_type) def open_restaurant(self): print("Is opening") restraurant = Restraurant('A','B') print(restraurant.restaurant_name," ",restraurant.cuisine_type) restraurant.describe_restaurant() restraurant.open_restaurant()
结果:
根据你为完成练习9-1而编写的类创建三个实例,并对每个实例调用方法describe_restaurant() 。
class Restraurant(): """docstring for Restraurant""" def __init__(self, restaurant_name,cuisine_type): self.restaurant_name = restaurant_name self.cuisine_type = cuisine_type def describe_restaurant(self): print(self.restaurant_name,":",self.cuisine_type) def open_restaurant(self): print("Is opening") restraurant1 = Restraurant('A1','B1') restraurant1.describe_restaurant() restraurant2 = Restraurant('A2','B2') restraurant2.describe_restaurant() restraurant3 = Restraurant('A3','B3') restraurant3.describe_restaurant()
结果:
创建一个名为User 的类,其中包含属性first_name 和last_name ,还有用户简介通常会存储的其他几个属性。在类User 中定义一个名 为describe_user() 的方法,它打印用户信息摘要;再定义一个名为greet_user() 的方法,它向用户发出个性化的问候。
创建多个表示不同用户的实例,并对每个实例都调用上述两个方法。
class User(): """docstring for User""" def __init__(self, first_name,last_name): self.first_name = first_name self.last_name = last_name def describe_name(self): print("fitst_name :",self.first_name," last_name: ",self.last_name) def greet_user(self): print("hello, ",self.first_name," ",self.last_name) user1 = User('Alice','Tom') user1.describe_name() user1.greet_user() user2 = User("Mary",'Bob') user2.describe_name() user2.greet_user() user3 = User("Tony",'Belbert') user3.describe_name() user3.greet_user()
结果:
在为完成练习9-1而编写的程序中,添加一个名为number_served 的属性,并将其默认值设置为0。根据这个类创建一个名为restaurant 的实 例;打印有多少人在这家餐馆就餐过,然后修改这个值并再次打印它。
添加一个名为set_number_served() 的方法,它让你能够设置就餐人数。调用这个方法并向它传递一个值,然后再次打印这个值。
添加一个名为increment_number_served() 的方法,它让你能够将就餐人数递增。调用这个方法并向它传递一个这样的值:你认为这家餐馆每天可能接待的就 餐人数。
class Restraurant(): """docstring for Restraurant""" def __init__(self, restaurant_name,cuisine_type): self.restaurant_name = restaurant_name self.cuisine_type = cuisine_type self.number_served = 0 def describe_restaurant(self): print(self.restaurant_name,":",self.cuisine_type) def open_restaurant(self): print("Is opening") def set_number_served(self,number): self.number_served = number def get_number_served(self): return self.number_served def increment_number_served(self,number): while self.number_served < number: print(self.number_served) self.number_served +=1 restraurant = Restraurant('A','B') print(restraurant.restaurant_name," ",restraurant.cuisine_type) restraurant.describe_restaurant() restraurant.open_restaurant() print(restraurant.get_number_served()) restraurant.set_number_served(2) print(restraurant.get_number_served()) restraurant.increment_number_served(10)
结果:
在为完成练习9-3而编写的User 类中,添加一个名为login_attempts 的属性。编写一个名为increment_login_attempts() 的方法, 它将属性login_attempts 的值加1。再编写一个名为reset_login_attempts() 的方法,它将属性login_attempts 的值重置为0。
根据User 类创建一个实例,再调用方法increment_login_attempts() 多次。打印属性login_attempts 的值,确认它被正确地递增;然后,调用方 法reset_login_attempts() ,并再次打印属性login_attempts 的值,确认它被重置为0。
class User(): """docstring for User""" def __init__(self, first_name,last_name): self.first_name = first_name self.last_name = last_name self.login_attempts = 0 def describe_name(self): print("fitst_name :",self.first_name," last_name: ",self.last_name) def greet_user(self): print("hello, ",self.first_name," ",self.last_name) def increment_login_attempts(self): self.login_attempts += 1 def reset_login_attempts(self): self.login_attempts = 0 user1 = User('Alice','Tom') user1.describe_name() print("login_attempts:",user1.login_attempts) user1.increment_login_attempts() print("login_attempts:",user1.login_attempts) user1.increment_login_attempts() print("login_attempts:",user1.login_attempts) user1.reset_login_attempts() print("login_attempts:",user1.login_attempts)
结果:
冰淇淋小店是一种特殊的餐馆。编写一个名为IceCreamStand 的类,让它继承你为完成练习9-1或练习9-4而编写的Restaurant 类。这两个版 本的Restaurant 类都可以,挑选你更喜欢的那个即可。添加一个名为flavors 的属性,用于存储一个由各种口味的冰淇淋组成的列表。编写一个显示这些冰淇淋 的方法。创建一个IceCreamStand 实例,并调用这个方法。
class Restraurant(): """docstring for Restraurant""" def __init__(self, restaurant_name,cuisine_type): self.restaurant_name = restaurant_name self.cuisine_type = cuisine_type self.number_served = 0 def describe_restaurant(self): print(self.restaurant_name,":",self.cuisine_type) def open_restaurant(self): print("Is opening") def set_number_served(self,number): self.number_served = number def get_number_served(self): return self.number_served def increment_number_served(self,number): while self.number_served < number: print(self.number_served) self.number_served +=1 class IceCreamStand(Restraurant): def __init__(self, restaurant_name,cuisine_type): super().__init__(restaurant_name,cuisine_type) self.flavors = ['banaa','apple','orange'] def show_icecram(self): for s in self.flavors: print(s) icecream = IceCreamStand('Hang','Big') icecream.show_icecram() icecream.describe_restaurant()
结果:
管理员是一种特殊的用户。编写一个名为Admin 的类,让它继承你为完成练习9-3或练习9-5而编写的User 类。添加一个名为privileges 的属性,用 于存储一个由字符串(如"can add post" 、"can delete post" 、"can ban user" 等)组成的列表。编写一个名为show_privileges() 的方法,它 显示管理员的权限。创建一个Admin 实例,并调用这个方法。
class User(): """docstring for User""" def __init__(self, first_name,last_name): self.first_name = first_name self.last_name = last_name self.login_attempts = 0 def describe_name(self): print("fitst_name :",self.first_name," last_name: ",self.last_name) def greet_user(self): print("hello, ",self.first_name," ",self.last_name) def increment_login_attempts(self): self.login_attempts += 1 def reset_login_attempts(self): self.login_attempts = 0 class Admin(User): """docstring for Admin""" def __init__(self, first_name,last_name): super().__init__(first_name,last_name) self.privileges = ['can add post','can ban user','can delete post'] def show_privileges(self): print(self.privileges) admin = Admin("Alice",'Bob') admin.show_privileges()
结果:
编写一个名为Privileges 的类,它只有一个属性——privileges ,其中存储了练习9-7 所说的字符串列表。将方法show_privileges() 移到这 个类中。在Admin 类中,将一个Privileges 实例用作其属性。创建一个Admin 实例,并使用方法show_privileges() 来显示其权限。
class User(): """docstring for User""" def __init__(self, first_name,last_name): self.first_name = first_name self.last_name = last_name self.login_attempts = 0 def describe_name(self): print("fitst_name :",self.first_name," last_name: ",self.last_name) def greet_user(self): print("hello, ",self.first_name," ",self.last_name) def increment_login_attempts(self): self.login_attempts += 1 def reset_login_attempts(self): self.login_attempts = 0 class Privileges(): """docstring for Privileges""" def __init__(self): self.privileges = ['can add post','can ban user','can delete post'] def show_privileges(self): print(self.privileges) class Admin(User): """docstring for Admin""" def __init__(self, first_name,last_name): super().__init__(first_name,last_name) self.privileges = Privileges() def show_privileges(self): self.privileges.show_privileges() admin = Admin("Alice",'Bob') admin.show_privileges()
结果:
在本节最后一个electric_car.py版本中,给Battery 类添加一个名为upgrade_battery() 的方法。这个方法检查电瓶容量,如果它不是85,就将它 设置为85。创建一辆电瓶容量为默认值的电动汽车,调用方法get_range() ,然后对电瓶进行升级,并再次调用get_range() 。你会看到这辆汽车的续航里程增 加了。
class Car(): """docstring for Car""" def __init__(self, make,model,year): self.make = make self.model = model self.year = year self.odometer_reading = 0 def get_descriptive_name(self): long_name = str(self.year) + ' ' + self.make + ' ' + self.model return long_name #打印里程消息 def read_odometer(self): print("This car has " + str(self.odometer_reading) + " miles on it.") #更新里程数 def update_odometer(self,milegeage): if milegeage >= self.odometer_reading: self.odometer_reading = milegeage else: print("You can't roll back an odometer!") def increment_odometer(self,miles): self.odometer_reading += miles class Battery(): """docstring for Battery""" def __init__(self, battery_size=70): self.battery_size = battery_size def describe_battery(self): print("This car has a " + str(self.battery_size) + "-kWh battery") def upgrade_battery(self): if self.battery_size!=85: self.battery_size = 85 def get_range(self): if self.battery_size == 70: range = 240 elif self.battery_size == 85: range = 270 message = "This car can go approximately " + str(range) message += "miles on a full charge." print(message) self.upgrade_battery() class ElectricCar(Car): """docstring for ElectricCar""" def __init__(self, make,model,year): super().__init__(make,model,year) self.battery_size = Battery() def describe_battery(self): self.battery_size.battery_size() def get_range(self): self.battery_size.get_range() my_tesla = ElectricCar('tesla','model s',2016) my_tesla.get_range() my_tesla.get_range()
结果:
将最新的Restaurant 类存储在一个模块中。在另一个文件中,导入Restaurant 类,创建一个Restaurant 实例,并调 用Restaurant 的一个方法,以确认import 语句正确无误。
restaurant.py
class Restraurant(): """docstring for Restraurant""" def __init__(self, restaurant_name,cuisine_type): self.restaurant_name = restaurant_name self.cuisine_type = cuisine_type self.number_served = 0 def describe_restaurant(self): print(self.restaurant_name,":",self.cuisine_type) def open_restaurant(self): print("Is opening") def set_number_served(self,number): self.number_served = number def get_number_served(self): return self.number_served def increment_number_served(self,number): while self.number_served < number: print(self.number_served) self.number_served +=1
coding.py
from restraurant import Restraurant restraurant1 = Restraurant('A','B') print(restraurant1.restaurant_name," ",restraurant1.cuisine_type) restraurant1.describe_restaurant() restraurant1.open_restaurant() print(restraurant1.get_number_served()) restraurant1.set_number_served(2) print(restraurant1.get_number_served()) restraurant1.increment_number_served(10)
结果:
以为完成练习9-8而做的工作为基础,将User 、Privileges 和Admin 类存储在一个模块中,再创建一个文件,在其中创建一个Admin 实例 并对其调用方法show_privileges() ,以确认一切都能正确地运行。
user.py
class User(): """docstring for User""" def __init__(self, first_name,last_name): self.first_name = first_name self.last_name = last_name self.login_attempts = 0 def describe_name(self): print("fitst_name :",self.first_name," last_name: ",self.last_name) def greet_user(self): print("hello, ",self.first_name," ",self.last_name) def increment_login_attempts(self): self.login_attempts += 1 def reset_login_attempts(self): self.login_attempts = 0 class Privileges(): """docstring for Privileges""" def __init__(self): self.privileges = ['can add post','can ban user','can delete post'] def show_privileges(self): print(self.privileges) class Admin(User): """docstring for Admin""" def __init__(self, first_name,last_name): super().__init__(first_name,last_name) self.privileges = Privileges() def show_privileges(self): self.privileges.show_privileges()
coding.py
from user import Admin admin = Admin("Alice",'Bob') admin.show_privileges()
结果:
将User 类存储在一个模块中,并将Privileges 和Admin 类存储在另一个模块中。再创建一个文件,在其中创建一个Admin 实例,并对其调用方法show_privileges() ,以确认一切都依然能够正确地运行。
privileges.py
class Privileges(): """docstring for Privileges""" def __init__(self): self.privileges = ['can add post','can ban user','can delete post'] def show_privileges(self): print(self.privileges)
admin.py
class Admin(User): """docstring for Admin""" def __init__(self, first_name,last_name): super().__init__(first_name,last_name) self.privileges = Privileges() def show_privileges(self): self.privileges.show_privileges()
user.py
class User(): """docstring for User""" def __init__(self, first_name,last_name): self.first_name = first_name self.last_name = last_name self.login_attempts = 0 def describe_name(self): print("fitst_name :",self.first_name," last_name: ",self.last_name) def greet_user(self): print("hello, ",self.first_name," ",self.last_name) def increment_login_attempts(self): self.login_attempts += 1 def reset_login_attempts(self): self.login_attempts = 0
coding.py
from user import User from admin import Admin from privilege import Privilege admin = Admin("Alice",'Bob') admin.show_privileges()
结果:
在练习6-4中,你使用了一个标准字典来表示词汇表。请使用OrderedDict 类来重写这个程序,并确认输出的顺序与你在字典中添加键 —值对的顺序一致。
from collections import OrderedDict favorite_languages = OrderedDict() favorite_languages['jen'] = 'python' favorite_languages['sarah'] = 'c' favorite_languages['edward'] = 'ruby' favorite_languages['phil'] = 'python' for key,value in favorite_languages.items(): print(key.title() + " 's favorite languages is "+value.title() + ".")
结果:
模块random 包含以各种方式生成随机数的函数,其中的randint() 返回一个位于指定范围内的整数,例如,下面的代码返回一个1~6内的整数:
from random import randint
x = randint(1, 6)
请创建一个Die 类,它包含一个名为sides 的属性,该属性的默认值为6。编写一个名为roll_die() 的方法,它打印位于1和骰子面数之间的随机数。创建一个6面 的骰子,再掷10次。 创建一个10面的骰子和一个20面的骰子,并将它们都掷10次。
from random import randint class Die(): """docstring for Die""" def __init__(self): self.sides = 6 def roll_die(self): x = randint(1,6) self.sides=x print(self.sides) def roll_die10(self): x = randint(1,10) self.sides = x print(self.sides) def roll_die20(self): self.sides = randint(1,20) print(self.sides) die = Die() print(die.sides) print("6 sides") for i in range(0,10): die.roll_die() print("10 sides") for i in range(0,10): die.roll_die10() print("20 sides") for i in range(0,10): die.roll_die20()
结果:
以上就是本次给大家分享的全部知识点内容总结,大家还可以在下方相关文章里找到儿童python编程入门书籍推、 spring+springmvc+mybatis整合注、 解决axios.interceptors.respon、 等python文章进一步学习,感谢大家的阅读和支持。
展开 +
收起 -
Copyright 2018-2019 xz577.com 码农之家