#!/usr/bin/python3
class MyClass: # 定义类,名字为:myclass
i = 12345 # 定义类的变量,作用:给函数使用
def f(self): # 定义方法函数 名字为 f 属性变量为 self ;
return 'hello world' # 方法为打印 字符串
x = MyClass() # 实例化类:将类赋值给变量x
print("MyClass 类的属性 i 为:", x.i) # 访问并打印myclass类中的变量 i
print("MyClass 类的方法 f 输出为:", x.f()) # 访问类并 调用类中的 f 函数
#!/usr/bin/python3
class people:
name = ' ' # 定义变量基本属性变量 name 和age 和 weight ;作用:给函数使用
age = 0
__weight = 0 # 定义私有属性前面加横线 weight [weɪt] 权重,私有属性在类外部无法直接进行访问
def __init__(self,n,a,w): # 定义构造方法; __init__ 初始化,使得每一个实例(instance)都有对应的基本属性;获取类里面的变量
self.name = n # 由slef 内置函数将 类 变量的值 给本函数变量 n, a , w 对应 获取其值
self.age = a
self.__weight = w
def speak(self): # 继承 self 函数的参数来使用
print("%s 说: 我 %d 岁。" %(self.name,self.age))
p = people('runoob',10,30) # 实例化类; people 类 重命名为 p ; 并将值赋值给 类的 三个初始化变量 name age _weight
p.speak() # 调用类中的speak 函数;将函数名字放出来才会执行
#!/usr/bin/python3
######## 单继承 ########
class people: ### 第一个类
name = ''
age = 0
__weight = 0 # 定义私有属性 weight [weɪt] 权重,私有属性在类外部无法直接进行访问
def __init__(self,n,a,w): # 定义构造方法; __init__ 初始化,获取类的变量
self.name = n # 由slef 内置函数将 类 变量的值 给本函数变量 n, a , w 对应 获取其值
self.age = a
self.__weight = w
def speak(self): # 继承self 函数的值
print("%s 说: 我 %d 岁。" %(self.name,self.age))
class student(people): ### 第二个类,继承people类的变量:n, a , w
grade = '' # 本类在次定义一个变量
def __init__(self,n,a,w,g): # 定义本函数属性变量
people.__init__(self,n,a,w) # 调用父类的构函:直接获取 people类 的变量名称
self.grade = g # 拉取本类的变量grade 重命名为本函数变量 g
def speak(self): # 覆写父类的方法
print("%s 说: 我 %d 岁了,我在读 %d 年级"%(self.name,self.age,self.grade))
s = student('ken',10,60,3)
s.speak() # ken 说: 我 10 岁了,我在读 3 年级
############################ 多重继承 ############################
#!/usr/bin/python3
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() #方法名同,默认调用的是在括号中排前地父类的方法
#!/usr/bin/python3
class Parent: # 定义父类
def myMethod(self):
print ('调用父类方法')
class Child(Parent): # 定义子类
def myMethod(self):
print ('调用子类方法')
c = Child() # 子类实例
c.myMethod() # 子类调用重写方法
super(Child,c).myMethod() # 用子类对象调用父类已被覆盖的方法
__init__ : 构造函数,在生成对象时调用
__del__ : 析构函数,释放对象时使用
__repr__ : 打印,转换
__setitem__ : 按照索引赋值
__getitem__: 按照索引获取值
__len__: 获得长度
__cmp__: 比较运算
__call__: 函数调用
__add__: 加运算
__sub__: 减运算
__mul__: 乘运算
__truediv__: 除运算
__mod__: 求余运算
__pow__: 乘方
#!/usr/bin/python3
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)
Powered by ddoss.cn 12.0
©2015 - 2025 ddoss
渝公网安备50011302222260号
渝ICP备2024035333号
【实验平台安全承诺书】
小绿叶技术社区,优化网络中,点击查看配置信息
主机监控系统: 安全防火墙已开启检查cc攻击-下载文件完成后等待10s 恢复访问,检查连接数低于峰值恢复访问
您的IP:216.73.216.110,2025-12-01 15:20:52,Processed in 0.02156 second(s).