`
ldb19890624
  • 浏览: 238454 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Python学习笔记(7)

 
阅读更多

Python学习笔记(7)



一、Python的类和面向对象编程
先看一个例子:
inventory.py文件
# The Cellphone class holds data about a cell phone.
#inventory.py
class CellPhone:
def __init__(self,manufact,model,price):
self.__manufact = manufact
self.__model = model
self.__retail_price = price
def set_manufact(self,manufact):
self.__manufact = manufact
def set_model(self,model):
self.__model = model
def set_retail_price(self, price):
self.__retail_price = price
def get_manufact(self):
return self.__manufact
def get_model(self):
return self.__model
def get_retail_price(self):
return self.__retail_price

p17.py文件
import inventory
def main():
man = raw_input('Enter the manufacturer: ')
mod = raw_input('Enter the model number: ')
retail = input('Enter the retail price:')
phone = inventory.CellPhone(man, mod, retail)

print 'Hear is the data that you entered:'
print 'Manufacturer:',phone.get_manufact()
print 'Model Number:',phone.get_model()
print 'Retail Price: $%.2f' %phone.get_retail_price()
main()

运行结果:
Enter the manufacturer: IBM
Enter the model number: COOL123
Enter the retail price:1050.50
Hear is the data that you entered:
Manufacturer: IBM
Model Number: COOL123
Retail Price: $1050.50

把类中的属性作为私有数据,只能通过访问器进行访问和修改,这是一种非常安全的方法。


二、Python的类继承
Python允许一个新类继承已有的类。
例:
vehicles.py文件
# vehicles.py
# The Automobile class holds general data about an automobile in inventory.
class Automobile:
def __init__(self,make,model,mileage,price):
self.__make = make
self.__model = model
self.__mileage = mileage
self.__price = price
def set_make(self, make):
self.__make = make
def set_model(self, model):
self.__model = model
def set_mileage(self, mileage):
self.__mileage = mileage
def set_price(self, price):
self.__price = price

def get_make(self):
return self.__make
def get_model(self):
return self.__model
def get_mileage(self):
return self.__mileage
def get_price(self):
return self.__price

class Car(Automobile):
def __init__(self,make,model,mileage,price,doors):
Automobile.__init__(self,make,model,mileage,price)
self.__doors = doors
def set_doors(self, doors):
self.__doors = doors
def get_doors(self):
return self.__doors

p18.py文件
# The Car class represents a car. It is a subclass of the Automobile class.
import vehicles

def main():
used_car = vehicles.Car('Audi',2007,12500,21500.00,4)
print 'Make:',used_car.get_make()
print 'Model:',used_car.get_model()
print 'Mileage:',used_car.get_mileage()
print 'Price:',used_car.get_price()
print 'Number of doors:',used_car.get_doors()
main()

运行结果:

Make: Audi
Model: 2007
Mileage: 12500
Price: 21500.0
Number of doors: 4


分享到:
评论

相关推荐

    Python学习笔记(干货) 中文PDF完整版.pdf

    这份"Python学习笔记"涵盖了从环境搭建到基础语法,再到数据类型和控制结构等关键知识点,旨在为初学者提供全面的学习指导。 首先,1.1章节介绍了Python的基础,包括Python的起源和历史。Python是由Guido van ...

    Python学习笔记--皮大庆.pdf.zip

    【Python学习笔记--皮大庆.pdf.zip】是一个针对初学者的Python编程教程,源自英文书籍《How to think like a computer scientist》。这本书以易懂的方式介绍了Python语言的基础知识,旨在帮助没有编程背景的人快速...

    皮大庆Python学习笔记

    Python基础入门教程,适合Python初学者,文档内容包括, 目录 前言 i 第一章 程序 1 1.1 程序 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 什么是调试 . . . . . . . . . . . . . . . ....

    最新Python学习笔记3

    本篇学习笔记主要介绍了Python中关于变量指向函数、高阶函数以及特殊高阶函数map()、reduce()、filter()和sorted()的使用方法和原理。 首先,变量在Python中不仅可以指向基本数据类型,还可以指向一个函数名。这...

    python学习笔记.pdf

    在这份《python学习笔记.pdf》中,记录了Python编程的基础知识和一些技巧,内容涵盖了字符串处理、变量操作、数据结构、循环、条件判断等方面。以下是对学习笔记中提到知识点的详细说明。 ### 字符串处理 在Python...

    Python学习笔记-王纯业

    【Python学习笔记-王纯业】是一份专为Python初学者设计的教程,由王纯业编撰。这个教程深入浅出地介绍了Python编程的基础知识,帮助初学者快速上手。下面将详细阐述该教程中可能包含的重要知识点,以及Python入门者...

    Python学习笔记--皮大庆

    Python学习笔记--皮大庆

    王纯业的Python学习笔记

    《王纯业的Python学习笔记》是一份专为Python初学者和进阶者设计的学习资料,旨在帮助读者全面掌握这门强大的编程语言。Python作为一门高级编程语言,因其简洁、易读的语法特性,被广泛应用于数据分析、机器学习、...

    python学习笔记汇总

    python学习笔记汇总

    python学习笔记+源码练习

    "Python学习笔记+源码练习"是一个适合初学者的资源包,旨在帮助你从零基础开始掌握Python编程。这份资料包含了理论知识讲解和实际代码实践,使学习过程更为直观和实用。 在学习Python时,笔记是关键,它们可以帮助...

    Python学习笔记大集合

    Python学习笔记 附赠可爱的Python 同时增加了UltraEdit的高亮显示Shell 及Python的文件

    python学习笔记用案例

    "Python学习笔记用案例"这个标题表明这是一份包含了实际应用示例的学习资料,旨在帮助初学者通过实例来理解和掌握Python编程。描述中的“欢迎下载”暗示这份资料是公开共享的,鼓励大家学习和交流。 在Python的学习...

    最新Python学习笔记5

    Python学习笔记5的知识点包括: 1. datetime模块的使用:datetime是Python处理日期和时间的标准库,可以完成多种与日期和时间相关的工作。 - 获取当前日期和时间:使用datetime.now()函数可以获取当前的日期和...

    王纯业版python学习笔记

    《王纯业版Python学习笔记》是一本专为Python初学者设计的教程,作者王纯业以其简洁明了的写作风格,使得这本相对较为薄的书籍成为了初学者掌握Python编程的理想选择。这本书深入浅出地介绍了Python的基础知识,包括...

    Python学习笔记1 ~ Python学习笔记9 - 海龟画图.zip

    Python学习笔记0001 - 安装与配置Python开发环境 Python学习笔记0002 - 海龟画图 - 初画图形 Python学习笔记0003 - 海龟画图 - 变量 Python学习笔记0004 - 海龟画图 - for循环 Python学习笔记0005 - 海龟画图 - ...

Global site tag (gtag.js) - Google Analytics