一、继承
与java的继承不同python支持多继承,如Person类同时继承Animal类和Species类可以这样写:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
class Animal( object ): def __init__( self ): pass class Species: def __init__( self ): pass class Person(Animal, Species): country = "CN" def __init__( self , name, age, sex): self .name = name self .age = age self .sex = sex def printString( self ): print ( "name:" + self .name + "\n" + "age:" + str ( self .age) + "\n" + "sex:" + self .sex) person = Person( "Jack" , 12 , "male" ) print (person.printString()) |
括号里的即为父类,多类继承支持优先级如下:
二、是否继承object类的区别
继承了object以后类会增加一些属性,不影响基本使用。
三、封装
与java不同python没有private关键字,使用方法是在方法或变量前加“__”
如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
class Person(Animal, Species): __country = "CN" def __init__( self , name, age, sex): self .name = name self .age = age self .__sex = sex def __breath( self ): print ( self .name + "在呼吸......." ) def printString( self ): self .__breath() print ( "name:" + self .name + "\n" + "age:" + str ( self .age) + "\n" + "sex:" + self .__sex) person = Person( "Jack" , 12 , "male" ) print (person.printString()) print (person._Person__sex) print (person._Person__country) person._Person__breath() |
设置了“__”后就表示为私密变量或方法。
但python里却有破解私密访问的方法:
即:对象._类名__方法名()或变量名
保密性如下:
1
2
3
4
|
__foo__: 定义的是特殊方法,一般是系统定义名字 ,类似 __init__() 之类的。 _foo: 以单下划线开头的表示的是 protected 类型的变量,即保护类型只能允许其本身与子类进行访问,不能用于 from module import * __foo: 双下划线的表示的是私有类型(private)的变量, 只能是允许这个类本身进行访问了。 foo:就是public方法 |
四、类方法、静态方法与属性方法
类方法,不能调用实例变量或方法,只能调用类方法或变量,使用时需要在方法上添加“@classmethod”,如下:
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
|
class Person( object ): _number = 0 def __init__( self , name, age, sex): self .name = name self .age = age self ._number = 2 # 这里操作的是实例对象的变量 Person._number + = 1 # 这里对类的静态变量进行操作 self .__sex = sex def __breath( self ): print ( self .name + "在呼吸......." ) @classmethod def lucky( cls ): # cls代表class类本身与self或this不同,self和this代表实例对象 person2 = Person( "Alex" , 13 , 'male' ) person2.printString() print ( cls ._number) # 输出类变量 def printString( self ): self .__breath() print ( "name:" + self .name + "\n" + "age:" + str ( self .age) + "\n" + "sex:" + self .__sex) person = Person( "Jack" , 12 , "male" ) person.printString() # 调用时用实例对象 Person.lucky() # 调用时用类 |
也可以使用静态方法的注解“@staticmethod”类
静态方法,不能调用类或实例的任何变量
属性方法使用注解“@property”,特点是调用时和调用属性变量类似。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
class Person( object ): def __init__( self , name, age, sex): self .name = name self .age = age self .__sex = sex @property def lucky( self ): # cls代表class类本身与self或this不同,self和this代表实例对象 print ( "233" ) @lucky .setter def lucky( self , num): # cls代表class类本身与self或this不同,self和this代表实例对象 print ( "233" ) self .num = num person = Person( "Jack" , 12 , "male" ) person.lucky # 调用时像调用属性变量一样调用 person.lucky = 1 # 传参和变量传参类似 print (person.num) |
属性方法可以调用类和属性变量,传参和变量传参类似。
五、多态
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
class Animal( object ): #相对于抽象类 def show( self ): print ( "abstract class must be rewrite!" ) raise Exception class Person(Animal): def __init__( self , name, age, sex): self .name = name self .age = age self .__sex = sex def show( self ): print ( "hello" ) person = Person( "Jack" , 12 , "male" ) print (person.show()) |
六、python是否存在实质意义上的私有变量?
回答:
不存在的
Python中没有禁止访问类中某一成员的保护机制。Java是一门非常工程化的语言,其哲学就是为工程服务,通过各种限制,尽可能减少程序员编写错误代码的机会。而Python则相反,其哲学为信任编码者,给程序员最少的限制,但程序员必须为自己编写的代码负责。
那是否意味着Python认为面向对象编程不需要封装呢?答案也是否定的。Python通过编码规范而不是语言机制来完成封装,具体而言,Python规定了对变量命名的公约,约定什么样的变量名表示变量是私有的,不应该被访问(而不是不能被访问)。
总结
到此这篇关于python中面向对象的注意点概述总结的文章就介绍到这了,更多相关python面向对象内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/qq_50909707/article/details/122813079