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 Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf ...
最新Python学习笔记,Day 3,总结性学习笔记,适合初学者归纳总结。
Python学习笔记
Python基础入门教程,适合Python初学者,文档内容包括, 目录 前言 i 第一章 程序 1 1.1 程序 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 什么是调试 . . . . . . . . . . . . . . . ....
Python学习笔记.pdf
1、Python环境搭建 1.1 Python简介 1.2 Python语言的发展历程 1.3 Python语言的主要特点 1.4 Python应用场景 1.5 Python环境搭建 1.6 第一个Python程序 1.7 集成开发工具 2、Python语法基础 2.1 Python中的...
python学习笔记python学习笔记python学习笔记python学习笔记python学习笔记
Python 学习笔记
Python学习笔记--皮大庆,非常适合零基础入门。对应英文版本《How to think like a computer scientist》
最新Python学习笔记,Day 5,总结性学习笔记,适合初学者归纳总结。
python 学习笔记资料python 学习笔记资料python 学习笔记资料python 学习笔记资料
王纯业的Python学习笔记非常适合初学者入门的Python教程
python 学习笔记——线性回归预测模型.pdf python 学习笔记——线性回归预测模型.pdf python 学习笔记——线性回归预测模型.pdf python 学习笔记——线性回归预测模型.pdf python 学习笔记——线性回归预测模型.pdf ...
Python学习笔记--皮大庆
王纯业版《Python学习笔记》.pdf
python学习笔记+源码练习,简单易懂,让你从入门到不放弃
王纯业的Python学习笔记
python基础学习笔记