python类的学习笔记1

一、封装

  作为面向对象编程的三大要素之首的封装,其实就是将相关的数据和操作方式整合成一个更加便于定义和应用的单元罢了。

  数据:成员变量

  操作方法:成员方法

  类的格式:1)class  2)className 3)() 4):                 //1是类的关键字,2是希望的类名称,3是小括号,表明类,如果有父类,将父类名称放入,否则就留空,4是定义类的必备符号

        缩进:  成员变量罗列

        缩进:  成员函数定义

        空行

  函数的定义格式:1)def  2)funName 3)() 4):             //1是函数定义的关键字,2是希望的函数名称,3是小括号,表明方法,如果有参数,将参数名称放入,参数间用逗号分割,否则就留空,4是定义函数的必备符号

  用类生成对象就是类的实例化

  类中可定义一个类似C++的构造函数样的转换器:

  1)固定的名称函数 __init__

  2)函数的参数第一个必须为self

1 class Book():
2     title = 'picture'
3     price = 1680
4 
5     def printPrice(self, num):
6         print(self.title + ' : ', num, self.price * num)
7 
8 book1 = Book()
9 book1.printPrice(2)

  没有加入转换器,数据实用不够方便,引入了类成员变量

 1 class Book():
 2     def __init__(self, title, price):
 3         self.title = title
 4         self.price = price
 5 
 6     def printPrice(self, num):
 7         print(self.title + ' : ', num, self.price * num)
 8 
 9 book1 = Book('picture', 1680)
10 book1.printPrice(2)

  加入转换器,实用更方便

二、类的继承:可以获取其他类成员的便捷方式:

  继承格式:1)class  2)SubClassName 3)(SuperClassName) 4):  //1是类的关键字,2是希望的子类名称,3父类名称为SuperClassName,将其放入小括号,4是定义类的必备符号

  超类:被继承的类

  子类:继承的类

 1 class Book():
 2     def __init__(self, title, price):
 3         self.title = title
 4         self.price = price
 5 
 6     def printPrice(self, num):
 7         print(self.title + ' : ', num, self.price * num)
 8 
 9 class ColorBook(Book):
10     color = 'red'
11     def printPrice(self, num):
12         print(self.title , ' : ', num, self.price * num , ' , color: ', self.color)
13 
14 book0 = Book('cartoon', 8610)
15 book0.printPrice(3)
16 
17 book1 = ColorBook('picture', 1680)
18 book1.printPrice(2)

  超类是Book,子类是ColorBook,子类重写了printPrice

 1 class Book():
 2     def __init__(self, title, price):
 3         self.title = title
 4         self.__price = price
 5 
 6     def printPrice(self, num):
 7         print(self.title + ' : ', num, self.price * num)
 8 
 9     def getPrice(self):
10         return self.__price
11 
12     def setPrice(self, price):
13         self.__price = price
14 
15     def delPrice(self):
16         self.__price = 0
17 
18     price = property(fget=getPrice, fset=setPrice, fdel=delPrice, doc='attribue of price')
19 
20 class ColorBook(Book):
21     color = 'red'
22     def printPrice(self, num):
23         print(self.title , ' : ', num, self.price * num , ' , color: ', self.color)
24 
25 book0 = Book('cartoon', 8610)
26 book0.printPrice(3)
27 book0.price = 2680
28 book0.printPrice(3)
29 print(book0.price)
30 del(book0.price)
31 print(book0.price)
32 
33 book1 = ColorBook('picture', 1680)
34 book1.printPrice(2)

  通过引入__前缀的属性,加入对属性的操作方式,加强属性的访问方式的控制,用属性管理的方式,具体就是第18行的代码,

 1 class Book():
 2     def __init__(self, title, price):
 3         self.title = title
 4         self.__price = price
 5 
 6     def printPrice(self, num):
 7         print(self.title + ' : ', num, self.price * num)
 8 
 9     @property
10     def price(self):
11         return self.__price
12 
13     @price.setter
14     def price(self, price):
15         self.__price = price
16 
17     @price.deleter
18     def price(self):
19         self.__price = 0
20 
21 class ColorBook(Book):
22     color = 'red'
23     def printPrice(self, num):
24         print(self.title , ' : ', num, self.price * num , ' , color: ', self.color)
25 
26 book0 = Book('cartoon', 8610)
27 book0.printPrice(3)
28 book0.price = 2680
29 book0.printPrice(3)
30 print(book0.price)
31 del(book0.price)
32 print(book0.price)
33 
34 book1 = ColorBook('picture', 1680)
35 book1.printPrice(2)

  通过装饰模式的属性定义方式,加入对属性的操作方式,加强属性的访问方式的控制,具体就是第9,13,17行的代码,

三、类的多态

  方法的重写:继承后,子类中再次实现父类中的方法,并取而代之

  python中根本不提重载

 1 class Book():
 2     def __init__(self, title, price):
 3         self.title = title
 4         self.__price = price
 5 
 6     def printPrice(self, num):
 7         print(self.title + ' : ', num, self.price * num)
 8 
 9     @classmethod
10     def printMaxNum(cls):
11         print(20002)
12 
13     @property
14     def price(self):
15         return self.__price
16 
17     @price.setter
18     def price(self, price):
19         self.__price = price
20 
21     @price.deleter
22     def price(self):
23         self.__price = 0
24 
25 class ColorBook(Book):
26     color = 'red'
27     def printPrice(self, num):
28         print(self.title , ' : ', num, self.price * num , ' , color: ', self.color)
29 
30 Book.printMaxNum()
31 book0 = Book('cartoon', 8610)
32 book0.printPrice(3)
33 book0.price = 2680
34 book0.printPrice(3)
35 print(book0.price)
36 del(book0.price)
37 print(book0.price)
38 
39 book1 = ColorBook('picture', 1680)
40 book1.printPrice(2)

  成员的访问规矩多了

 1 class Book():
 2     title = 'picture'
 3     price = 1680
 4 
 5     def printPrice(self, num):
 6         print(self.title + ' : ', num, self.price * num)
 7 
 8 book1 = Book()
 9 book1.printPrice(2)
10 book1.title = 'dictionary'
11 book1.price = '1580'
12 print(book1.title, book1.price)
13 print(Book.title, Book.price)

  第11行该的是对象book1的price,而类的price并没有改变,可以通过运行程序很容易得知结果

四、特殊的方法

  与转换器__init__类似的方法主要有:

  __eq__  ==

  __ne__ !=

  __lt__  <

  __gt__  >

  __le__  <=

  __ge__  >=

  __add__  +

  __sub__  -

  __mul__  *

  __truediv__ /

  __floordiv__  //

  __mod__ %

  __pow__  **

 1 class Book():
 2     title = 'picture'
 3     price = 1680
 4 
 5     def printPrice(self, num):
 6         print(self.title + ' : ', num, self.price * num)
 7 
 8 class MyString():
 9     def __init__(self, text):
10         self.text = text
11 
12     def __eq__(self, other):
13         return self.text == other.text
14 
15 book1 = Book()
16 book1.printPrice(2)
17 book1.title = 'dictionary'
18 book1.price = '1580'
19 print(book1.title, book1.price)
20 print(Book.title, Book.price)
21 
22 str1 = MyString('love')
23 str2 = MyString('love')
24 print(str1 == str2)

  重新实现特定符号作用的方法是不错的。

  

        

上一篇:网页TDK标签“title、description、keywords“(笔记)


下一篇:JavaWeb 在html中引入javascript的两种方式