#!/usr/bin/env python
# -*- coding: utf-8 -*-
import string
# class gxd: #经典类
class gxd(object): #新式类 多继承方式改变
def __init__(self,name,age,address,score):
self.name = name
self.age = age
self.address = address
self.__score = score
def mingzi(self):
print("你的名字是",self.name,"年龄:",self.age)
def simple(self):
print("分数为:",self.__score) #私有属性 私有方法类似
# def __del__(self):
# print("程序结束(析构函数)",self.name)
# del __del__
r1 =gxd('gxd','22','henan','100')
r1.mingzi()
r1.simple()
class xxx(object):
def playwith(self,obj):
print("%s is play with %s"%(self.name,obj.name))
class ggg(gxd,xxx): #继承
####### 多继承默认从左到右 python3 都是是广度优先来继承 python2经典类是深度优先来继承 新式类也是广度优先来继承(加object)
def __init__(self,name,age,address,score,intresting): #重构子类属性
# gxd.__init__(self,name,age,address,score)
super(ggg,self).__init__(name,age,address,score) #同上 新式类写法
self.intresting = intresting
# @classmethod 类方法 只能访问类变量,不能访问实例变量
# @staticmethod 静态方法 名义上归类管理,实际上跟类没啥关系
# @property 属性方法 把一个方法变成一个静态属性 平常不能传参数
# 如果要传参数 要写一个同名字的方法,在方法上边@方法名.setter 删除的话 @方法名.deleter 同上
def play(self):
print("%s is playing DOTA2"% self.name)
def simple(self): #调用(重构)父类方法
gxd.simple(self)
# m = ggg("ggg","22","henan","99")
m = ggg("gxd","22","henan","99","DOTA2")
m.play()
m.simple()
m.playwith(r1)
多态
class Animal:
def __init__(self, name):
self.name = name
def talk(self):
pass
# @staticmethod
def animal_talk(obj):
obj.talk()
class Cat(Animal):
def talk(self):
print('Meow!')
class Dog(Animal):
def talk(self):
print('Woof! Woof!')
d = Dog("abc")
c = Cat("123")
Animal.animal_talk(c)
Animal.animal_talk(d)
python 类的练习题
https://blog.csdn.net/ywq935/article/details/78348590
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
@author: gxd
@function:
@file: 类的练习.py
@time: 2018/11/18 10:24
'''
Course_List=[]
class School(object):
def __init__(self,school_name):
self.school_name=school_name
self.students_list=[]
self.teachers_list=[]
global Course_List
def hire(self,obj):
self.teachers_list.append(obj.name)
print("[Notice]:雇佣了新员工 %s"%obj.name)
def enroll(self,obj):
self.students_list.append(obj.name)
print("[Notice]:学员 %s 信息注册成功,ID:%s"%(obj.name,obj.id))
class Grade(School):
def __init__(self,school_name,grade_code,grade_course):
super(Grade, self).__init__(school_name)
self.code=grade_code
self.course=grade_course
self.members=[]
Course_List.append(self.course)
print("[Notice]:Now,School |%s|grade|%s|create course|%s|"%(self.school_name,self.code,self.course))
def course_info(self):
print("""
Syllabus(课程大纲) of %s is :
day 1:
day 2:
....
....
""" %self.course)
Python = Grade('BJ',16,'Python')
Go = Grade('BJ',1,'Go')
Linux = Grade('SH',10,'Linux')
class School_member(object):
def __init__(self,name,age,sex,role):
self.name=name
self.age=age
self.sex=sex
self.role=role
self.course_list=[]
print('----My name is %s ,and I am a %s----'%(self.name,self.role))
stu_num_id=00
class Students(School_member):
def __init__(self,name,age,sex,role,course):
super(Students, self).__init__(name,age,sex,role)
global stu_num_id
stu_id = course.school_name+'S'+str(course.code)+str(stu_num_id).zfill(2)
self.id = stu_id
self.mark_list={}
def study(self,course):
print('---- I learn %s,ID %s ----'%(course.course,self.id))
def pay(self,course):
print('---- I pay 2000$ fo %s ----'%course.course)
self.course_list.append(course.course)
def Praise(self,obj):
print('---- %s Praise %s:Perfect!----'%(self.name,obj.name))
def mark_check(self):
for i in self.mark_list.items():
print(i)
def out(self):
print('[Notice]:The mark of %s was so bad,he(she) opt out at last'%self.name)
tea_num_id = 00
class Teachers(School_member):
def __init__(self,name,age,sex,role,course):
super(Teachers, self).__init__(name,age,sex,role)
global tea_num_id
tea_num_id += 1 #保证有两位数,个位数时往前补0
Tea_id = course.school_name+'T'+str(course.code)+str(tea_num_id).zfill(2)
self.id=Tea_id
def teach(self,course):
print('---- I teach course %s, id %s ----'%(course.course,self.id))
def record_mark(self,Date,course,obj,level):
print('It is %s \'s mark in the Day %s of the course %s : %s' %(obj.name,Date,course.course,level) )
obj.mark_list['Day'+Date]=level
a = Students('张三',21,'W','student',Python)
Python.enroll(a)
a.study(Python)
a.pay(Python)
b = Students('李四',22,'M','student',Python)
Python.enroll(b)
b.study(Python)
b.pay(Python)
c = Students('赵武',23,'M','student',Python)
Python.enroll(c)
c.study(Python)
c.pay(Python)
t = Teachers('王二',30,'M','teacher',Python)
Python.hire(t)
t.teach(Python)
t.record_mark('6',Python,a,'A')
t.record_mark('6',Python,c,'B')
t.record_mark('1',Python,b,'C-')
t.record_mark('2',Python,b,'C-')
t.record_mark('3',Python,b,'C-')
t.record_mark('4',Python,b,'C-')
t.record_mark('5',Python,b,'C-')
t.record_mark('6',Python,b,'C-')
print(b.course_list) #查看已报名课程
b.mark_check() #评分查询
b.out()
a.Praise(t)
类的特殊成员
# # 类的特殊成员
# class gxd(object):
# """ 这个类是干嘛的"""
# print(gxd.__doc__) #打印类的说明
# print(gxd.__module__) #输出当前所属模块
# print(gxd.__class__) #输出类
# print(gxd.__dict__) # 查看类或对象中的所有方法
# __str__ __getitem__ __setitem__ __delitem__
#__new__ 先于实例化过程,通过__new__方法来实例化,用来创建实例
#__metaclass__ 可以看这个http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python
# class Foo(object):
# def __init__(self,name):
# self.name=name
#
# f = Foo("ggg") #f对象是Foo类的一个实例,Foo类对象是type类的一个实例 Foo类对象是通过type类的构造方法创建
#
#另一种创建类
#类是由type类实例化产生
# def func(self):
# print("hello %s"%self.name)
# def __init__(self,name,age):
# self.name = name
# self.age = age
# Foo = type('Foo',(object,),{'hello':func,
# '__init__':__init__})
# f = Foo("ggg",22)
反射
def bulk(self):
print("%s is jiao ...."%self.name)
class Dog(object):
def __init__(self,name):
self.name=name
def eat(self,food):
print("%s is eating...."%self.name,food)
d = Dog("123")
choice = input(":::::").strip()
# print(hasattr(d,choice)) #判断d中有无这个choice方法
# getattr(d,choice)() #调用这个方法
if hasattr(d,choice):
# delattr(d,choice) 删除某个属性方法
func = getattr(d,choice)
func("333")
else:
setattr(d,choice,bulk) #动态装进去一个方法
# setattr(d,choice,22) 装进去一个属性值
# print(getattr(d,choice))
d.bulk(d)