1. Create a class
class ClassName: ‘‘‘ 定义类 ‘‘‘ def __init__(self,name,age):#self代表类的实例,而不是类本身 ‘‘‘ 类初始化函数 :param name:姓名 :param age: 年龄 ‘‘‘ self.name=name self.age=age def Class_method(self): ‘‘‘ 类中的方法 :return: ‘‘‘ pass
2. Class instantiation, creating objects of class
c_name1=ClassName(‘zhangsan‘,22)c_name2=ClassName(‘lisi‘,25)
3. Inheritance of Classes
class Child(ClassName): passclass c(a,b):#python的继承可以继承多个类 pass
4. Private properties and private methods for classes
__private_arrts #在前面加两个斜杠__private_method()
5. Polymorphism refers to functions with different functions can use the same function name
class people(object): def __init__(self,name,age): self.name=name self.age=age def show(self): print("姓名:%s,年龄:%s"%(self.name,self.age))class man(people): def show(self): print("man的姓名:%s,年龄:%s"%(self.name,self.age))class woman(people): def show(self): print("woman的姓名:%s,年龄:%s" % (self.name, self.age))class show_thing(): def test_show(people):#统一调用,传入类的实例 people.show()#调用类的实例的方法m=man(‘张三‘,‘20‘)w=woman(‘李四‘,‘23‘)show_thing.test_show(m)show_thing.test_show(w)>>:man的姓名:张三,年龄:20woman的姓名:李四,年龄:23
6. Properties of built-in classes
print(m.__dict__)#类的属性print(m.__doc__)#类的注释文档print(m.__module__)# __main__#print(m.__dir__())#打印类的所有属性
Python object-oriented-encapsulation, inheritance, polymorphism