1、类的概念和结构
1)类的概念
    和其他变成语言类似,python中的类是用来描述具有相同的属性和方法的对象的集合。它定义了该集合中每个对象所共有的属性和方法。而对象是类的实例。
    打个比方,人这个概念就是一个类。只要是人这个类,就都有一些属性(身高体重性别等)和方法(吃喝跑跳等)。
    再具体点,小红和小明都是人这个类的实例。小红和小明都有身高体重性别,也都会吃喝跑跳。但小红和小明身高体重性别的值肯定不同,吃喝跑跳的动作也肯定不同。
2)类的定义和实例化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
   |  class people:          name = ''     age = 0          __weight = 0          def __init__(self,n,a,w):         self.name = n         self.age = a         self.__weight = w     def speak(self):         print("%s 说: 我 %d 岁。" %(self.name,self.age))  
  XiaoHong = people('XiaoHong',16,35)   XiaoMing = people('XiaoMing',18,65)
  print(XiaoHong.name) print(XiaoMing.age)
  XiaoHong.speak() XiaoMing.speak()
 
  | 
 
    在上述代码中,类里面有一个特殊的方法init(),称为构造方法。在实例化对象时会首先执行这个方法,所以可以通过这个方法给对象的属性赋值。
    此外,类的方法与普通的函数有一个特别的区别——它们必须有一个额外的第一个参数名称, 按照惯例它的名称是 self。self 代表的是类的实例,代表当前对象的地址,而 self.class 则指向类。
2、类的继承
1)类的继承
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
   |  class people:          name = ''     age = 0          __weight = 0          def __init__(self,n,a,w):         self.name = n         self.age = a         self.__weight = w     def speak(self):         print("%s 说: 我 %d 岁。" %(self.name,self.age))  
  class student(people):     grade = ''     def __init__(self,n,a,w,g):                  people.__init__(self,n,a,w)         self.grade = g          def speak(self):         print("%s 说: 我 %d 岁了,我在读 %d 年级"%(self.name,self.age,self.grade))     s = student('ken',10,60,3) s.speak()
 
  | 
 
2)类的多继承
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
   |  class people:          name = ''     age = 0          __weight = 0          def __init__(self,n,a,w):         self.name = n         self.age = a         self.__weight = w     def speak(self):         print("%s 说: 我 %d 岁。" %(self.name,self.age))  
  class student(people):     grade = ''     def __init__(self,n,a,w,g):                  people.__init__(self,n,a,w)         self.grade = g          def speak(self):         print("%s 说: 我 %d 岁了,我在读 %d 年级"%(self.name,self.age,self.grade))  
  class speaker():     topic = ''     name = ''     def __init__(self,n,t):         self.name = n         self.topic = t     def speak(self):         print("我叫 %s,我是一个演说家,我演讲的主题是 %s"%(self.name,self.topic))  
  class sample(speaker,student):     a =''     def __init__(self,n,a,w,g,t):         student.__init__(self,n,a,w,g)         speaker.__init__(self,n,t)   test = sample("Tim",25,80,4,"Python") test.speak()   
 
  | 
 
3、方法重写
1)重写父类中的方法
1 2 3 4 5 6 7 8 9 10 11 12
   | class Parent:            def myMethod(self):       print ('调用父类方法')   class Child(Parent):     def myMethod(self):       print ('调用子类方法')   c = Child()           c.myMethod()         
  super(Child,c).myMethod() 
 
  | 
 
2)子类继承父类构造函数说明
  ①子类不重写 __init__,实例化子类时,会自动调用父类定义的 __init__
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
   | class Father(object):     def __init__(self, name):         self.name=name         print ( "name: %s" %( self.name) )     def getName(self):         return 'Father ' + self.name   class Son(Father):     def getName(self):         return 'Son '+self.name   son=Son('LC') print ( son.getName() )
 
 
 
 
  | 
 
  ②如果重写了__init__ 时,实例化子类,就不会调用父类已经定义的 __init__
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
   | class Father(object):     def __init__(self, name):         self.name=name         print ( "name: %s" %( self.name) )     def getName(self):         return 'Father ' + self.name   class Son(Father):     def __init__(self, name):         print ( "hi" )         self.name =  name     def getName(self):         return 'Son '+self.name   son=Son('LC') print ( son.getName() )
 
 
 
 
  | 
 
  ③如果重写了__init__时,要继承父类的构造方法,可以使用 super 关键字
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
   | class Father(object):     def __init__(self, name):         self.name=name         print ( "name: %s" %( self.name))     def getName(self):         return 'Father ' + self.name   class Son(Father):     def __init__(self, name):         super(Son, self).__init__(name)         print ("hi")         self.name =  name     def getName(self):         return 'Son '+self.name   son=Son('runoob') print ( son.getName() )
 
 
 
 
 
  | 
 
4、类的私有属性与私有方法
    之前说过,一个类中包含属性和方法。当实例化一个类后,可以通过 实例名.属性名 或实例名.方法名 的方式访问调用。
    此外,类中还会出现私有的属性和私有的方法。顾名思义就是只能在类内部使用的属性和方法。不能在类的外部被使用或直接访问。
    类的私有属性:两个下划线开头,在类内部的方法中使用时 self.private_attrs。
    类的私有方法:两个下划线开头,在类内部的方法中使用时 self.private_methods。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
   | class JustCounter:     __secretCount = 0       publicCount = 0           def count(self):         self.__secretCount += 1         self.publicCount += 1         print (self.__secretCount) class Site:     def __init__(self, name, url):         self.name = name            self.__url = url          def who(self):         print('name  : ', self.name)         print('url : ', self.__url)       def __foo(self):                   print('这是私有方法')       def foo(self):                     print('这是公共方法')         self.__foo()   counter = JustCounter() counter.count() counter.count() print (counter.publicCount) print (counter.__secretCount)  
  x = Site('LC', 'liuchengblog.github.io') x.who()         x.foo()         x.__foo()      
 
  |