python 类
封装
class Person:
def __init__(self, name):
self.name = name # 姓名
self.__age = 1 # 年龄,默认为1岁,私有属性
# 设置私有属性值的方法
def set_age(self, new_age):
if 0 < new_age <= 120: # 判断年龄是否合法
self.__age = new_age
# 获取私有属性值的方法
def get_age(self):
return self.__age
继承
# 定义一个表示房屋的类
class House(object):
def live(self): # 居住
print("供人居住")
# 定义一个表示汽车的类
class Car(object):
def drive(self): # 行驶
print("行驶")
# 定义一个表示房车的类
class TouringCar(House, Car):
pass
tour_car = TouringCar()
tour_car.live() # 子类对象调用父类House的方法
tour_car.drive() # 子类对象调用父类Car的方法
输出同名父类
class Test:
def handle(self):
print("这只是一个测试")
class Testagain(Test):
def handle(self):
super().handle()
print("我刚刚输出我的同名父类方法")
#def handle(self):
#print("我要重写父类方法")
a=Testagain()
a.handle()
示例
'''
class Person:
def __init__(self,name):
self.name=name #初始化在class外调用
self.__age=10 #不能将私有初始化
def set_age(self,new_age): #私有只能在内内部
if 0<new_age<120:
self.__age=new_age
def get_age(self):
return self.__age
person=Person("小明")
person.set_age(100)
print(person.get_age())
class Cat(object): #基础类 共有的方法 (父类)
def __init__(self,color):
self.color=color
self.__age=1
def walk(self):
print("走猫步")
def __eat(self):
print("吃鱼")
def test(self):
self.__eat()
class ZheErCat(Cat):
erduo="折耳"
zheer=ZheErCat('blue')
print(zheer.color)
zheer.walk()
zheer.test()
'''
class Car:
def __init__(self,color,csfs):
self.color=color
self.csfs=csfs
def introduce(self):
print("我是一辆车 颜色是%s 耗油量是%d"%(self.color,self.csfs))
car=Car('blue',30)
car.introduce()
class Racing_Car(Car):
def __init__(self,color,csfs,top_speed):
Car.__init__(self,color,csfs)
self.top_speed=top_speed
class Bus(Car):
def __init__(self,color,csfs,Nuclear_Number):
Car.__init__(self,color,csfs)
self.Nuclear_Number=Nuclear_Number
racing=Racing_Car('black',50,20)
bus=Bus('bck',50,60)
print(racing.top_speed)
print(bus.Nuclear_Number)
# bus=Bus('yellow',80)
# bus.introduce()
# print("赛车时速为",racing.top_speed)
# racing.introduce()
# print("公交车荷载人数为",bus.Nuclear_Number)
通讯录示例
class Address_list:
def __init__(self):
self.dict={}
def add(self):
while True:
p = input("联系人: (按q退出)")
if p.lower() == 'q':
break
n = input("联系人电话号码: ")
self.dict[p]=n
print(self.dict)
ob=open('c:/Users/25671/Desktop/contact.txt','a',encoding='utf-8')
for i,j in self.dict.items():
ob.write(i+' '+j)
ob.write('\n')
def show(self):
ob=open('c:/Users/25671/Desktop/contact.txt','r',encoding='utf-8')
for i in ob.readlines():
print(i,end='')
def modify(self):
new={}
ob=open('c:/Users/25671/Desktop/contact.txt','r',encoding='utf-8')
for i in ob.readlines():
list2 = i.split(' ')
new[list2[0].strip()]=list2[1].strip()
print(new)
name=input('请输入修改对象姓名')
if name in new:
newnum = input('请输入新的电话号码')
new[name]=newnum
ob=open('c:/Users/25671/Desktop/contact.txt','w',encoding='utf-8')
for i,j in new.items():
ob.write(i+' '+j)
ob.write('\n')
def delete(self):
delete={}
ob=open('c:/Users/25671/Desktop/contact.txt','r',encoding='utf-8')
for i in ob.readlines():
list2 = i.split(' ')
delete[list2[0].strip()]=list2[1].strip()
print(delete)
rm = input('请输入删除对象')
if rm in delete:
delete.pop(rm)
print(delete)
ob=open('c:/Users/25671/Desktop/contact.txt','w',encoding='utf-8')
for i,j in delete.items():
ob.write(i+' '+j)
ob.write('\n')
a = Address_list()
a.add()
a.delete()