mysql 小绿叶注册码,python3 基础 - eisc|小绿叶技术博客|linux基础|mysql基础|python|eisc脚本|小绿叶脚本...

1# 定义类#!/usr/bin/python3class MyClass:# 定义类,名字为:myclass    i = 12345# 定义类的变量,作用:给函数使用    def f(self):# 定义方法函数 名字为 f  属性变量为 self ;         return 'hello world'# 方法为打印 字符串 x = MyClass()# 实例化类:将类赋值给变量xprint("MyClass 类的属性 i 为:", x.i)# 访问并打印myclass类中的变量 i  print("MyClass 类的方法 f 输出为:", x.f())# 访问类并  调用类中的 f 函数#!/usr/bin/python3class 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  _weightp.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/python3class 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)python 类

0  0  140天前

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值