目录
零之前言
先说,本教程需要对象……没有对象的话,自己建一个。我是通过C++学习过面向对象方面的知识的,所以,我只作面向对象在Python中的应用,而对于面向对象的理解,需要看其他的东西。菜鸟教程中,对于py的面向对象教程:https://www.runoob.com/python3/python3-class.html
一.类及其成员定义
1.类定义
注意缩进
class ClassName:
<statement-1>
.
.
.
<statement-N>
2.类对象
class MyClass:
"""一个简单的类实例"""
#公有
i = 12345
def f(self):
return 'hello world'
#定义私有属性,私有属性在类外部无法直接进行访问
__weight = 0
# 实例化类
x = MyClass()
# 访问类的属性和方法
print("MyClass 类的属性 i 为:", x.i)
print("MyClass 类的方法 f 输出为:", x.f())
二.类操作
1.类方法:
在类的内部,使用 def 关键字来定义一个方法,与一般函数定义不同,类方法必须包含参数 self,且为第一个参数,self 代表的是类的实例。
self 的名字并不是规定死的,也可以使用 this,或者任何你想写的,但是最好还是按照约定是用 self。
①自定义
class people:
# 定义基本属性
name = 'kanna'
age = 1
def __foo(self):# 私有方法
print('这是私有方法')
def speak(self):# 公有方法
print("%s 说: 我 %d 岁。" % (self.name, self.age))
p = people()
p.speak()
②特殊
- __init__ : 构造函数,在生成对象时调用
- __del__ : 析构函数,释放对象时使用
- __repr__ : 打印,转换
- __setitem__ : 按照索引赋值
- __getitem__: 按照索引获取值
- __len__: 获得长度
- __cmp__: 比较运算
- __call__: 函数调用
- __add__: 加运算
- __sub__: 减运算
- __mul__: 乘运算
- __truediv__: 除运算
- __mod__: 求余运算
- __pow__: 乘方
class people:
#定义基本属性
name = ''
age = 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))
# 实例化类
p = people('runoob',10,30)
p.speak()
2.继承
①单继承
class DerivedClassName(BaseClassName1):
<statement-1>
.
.
.
<statement-N>
单继承Python和C++有非常大的差距,看一下代码:
class father1: # 定义父类
def __init__(self):
print("this is a call of father1")
class son(father1): # 定义子类
def h(self):
print("this is a call of son")
c = son() # 子类实例
---OUT---
this is a call of son
class father1: # 定义父类
def __init__(self):
print("this is a call of father1")
class son(father1): # 定义子类
def h(self):
print("this is a call of son")
c = son() # 子类实例
---OUT---
this is a call of father1
#include<iostream>
using namespace std;
class father1
{
public:
father1(){
cout << "this is a call of father1" << endl;
};
};
class son:public father1
{
public:
son(){
cout << "this is a call of son"<< endl;
};
};
int main(void)
{
son a;
}
---OUT---
this is a call of father1
this is a call of son
很明显,python的继承,如果子类有构造函数,那么相当于,构造函数就被重写惹,不会调用父亲的构造函数,而是直接调用子类的构造函数,而C++则是,先调用父构造函数,再调用子构造函数。这大抵就是解释器和编译器吧。
②多继承
需要注意圆括号中父类的顺序,若是父类中有相同的方法名,而在子类使用时未指定,python从左至右搜索 即方法在子类中未找到时,从左到右查找父类中是否包含方法。
class DerivedClassName(Base1, Base2, Base3):
<statement-1>
.
.
.
<statement-N>
Python和C++代码差别还是很大,就像:
#include<iostream>
using namespace std;
class father1
{
public:
father1(){
cout << "this is a call of father1" << endl;
};
};
class father2
{
public:
father2(){
cout << "this is a call of father2" << endl;
};
};
class son:public father1,father2
{
public:
son(){
cout << "this is a call of son"<< endl;
};
};
int main(void)
{
son a;
}
---OUT---
this is a call of father1
this is a call of father
this is a call of son
class father1: # 定义父类
def __init__(self):
print("this is a call of father1")
class father2: # 定义父类
def __init__(self):
print("this is a call of father2")
class son(father1): # 定义子类
def hhh(self):
print("this is a call of son")
c = son() # 子类实例
---OUT---
this is a call of father1
差距挺大的,自己看。
③解决途径
在子类初始化的时候,调用父类的初始化,记住,为了避免出现问题,一定要先调用父类的初始化:
class father1: # 定义父类
def __init__(self):
print("this is a call of father1")
class father2: # 定义父类
def __init__(self):
print("this is a call of father2")
class son(father1,father2): # 定义子类
def __init__(self):
father1.__init__(self)
father2.__init__(self)
print("this is a call of son")
c = son() # 子类实例
---OUT---
this is a call of son
this is a call of father1
this is a call of father2
3.方法重写
class Parent: # 定义父类
def myMethod(self):
print ('调用父类方法')
class Child(Parent): # 定义子类
def myMethod(self):
print ('调用子类方法')
c = Child() # 子类实例
c.myMethod() # 子类调用重写方法
super(Child,c).myMethod() #用子类对象调用父类已被覆盖的方法
4.运算符重载
运算符见前面,1.类定义:②特殊
class Vector:
def __init__(self, a, b):
self.a = a
self.b = b
def __str__(self):
return 'Vector (%d, %d)' % (self.a, self.b)
def __add__(self,other):
return Vector(self.a + other.a, self.b + other.b)
v1 = Vector(2,10)
v2 = Vector(5,-2)
print (v1 + v2)
啊,Python和C++一起学,真带劲儿!