构造函数与类的方法
# !!!构造函数的使用!!!
class body(object):
name = 'meishaobo'
age = 23
id = 2221220047
def tell(self):
print("nannan,woxihuanni\n")
def __init__(self):
print("!!!")
a = body()
# print(a.name + " " + str(a.age) + " " + str(a.id))
a.tell()
类外函数调用类内函数
参数列表需要保持一致,在类外函数内要先创建实例,然后使用实例来调用类内函数。
class BuildCar:
def __init__(self, steel, glass):
self.colour = None
self.steel = steel
self.glass = glass
print('构造已完成')
def paint(self, color):
self.colour = color
print('颜色:', self.colour)
def new_paint(color):
car = BuildCar(4, 2)
car.paint(color)
benci = BuildCar(4, 2)
benci.paint('black')
new_paint("red")
私有属性与私有方法
私有属性和私有方法均在变量和函数名前加__,类外无法使用私有方法和访问私有属性,可在类内的公有方法里调用私有方法和访问私有属性。
class People:
# 定义基本属性
name = None
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 岁,%d公斤。" % (self.name, self.age, self.__weight))
def public_speak(self):
self.__speak()
print(p.__weight)
# 实例化类
p = People('runoob', 10, 40)
p.public_speak()
类的继承
单继承
# 类定义
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 岁,%d公斤。" % (self.name, self.age, self.__weight))
# 单继承示例
class student(people):
grade = None
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))
s = student('ken', 10, 40, 3)
s.speak()
单继承调用父类的构造函数和方法
# !!!调用父类构造函数和父类方法
class Father(object):
name = None
age = None
sex = '男'
def __init__(self, name, sex, age):
self.name = name
self.sex = sex
self.age = age
def out_print(self):
print(self.name, self.sex, self.age)
class Daughter(Father):
def __init__(self, name, sex, age):
# Father.__init__(self, name, sex, age)
super(Daughter, self).__init__(name, sex, age) # 使用super()函数时,后面函数的括号内不加self。 python3可以使用super().__init__(name, sex, age)
self.sex = sex
msb = Father('msb', '男', 23)
msb.out_print()
whn = Daughter('whn', '女', 18)
whn.out_print()
多继承
class Phone(object):
price = None
function = 'communication'
brand = None
def __init__(self, price, brand):
self.price = price
self.brand = brand
def out_print(self):
print(self.price, self.function, self.brand)
class Computer(object):
price = None
function = 'game'
number = None
def __init__(self, price, number):
self.price = price
self.number = number
def out_print(self):
print(self.price, self.function, self.number)
class Smartphone(Phone, Computer):
price = None
function = None
brand = None
number = None
def __init__(self, price, function, brand, number):
Phone.__init__(self, price, brand)
Computer.__init__(self, price, number)
self.function = function
def out_print(self):
print(self.price, self.function, self.brand, self.number)
nojia = Phone(999, '诺基亚')
hp = Computer(6599, 2022)
huawei = Smartphone(5999, '卫星通讯', '华为', 2023)
nojia.out_print()
hp.out_print()
huawei.out_print()
方法重写
class Parent: # 定义父类
def myMethod(self):
print ('调用父类方法')
class Child(Parent): # 定义子类
def myMethod(self):
print ('调用子类方法')
c = Child() # 子类实例
c.myMethod() # 子类调用重写方法
super(Child,c).myMethod() #用子类对象调用父类已被覆盖的方法