python类继承,父类和子类的构造函数初始化调用

简介: # class Info(object):# def __init__(self,name,age,score):# self.
# class Info(object):
#     def __init__(self,name,age,score):
#         self.Name = name
#         self.Age = age
#         self.Score = score
#
#
# class Student(object):
#     def __init__(self,info):
#         self.Info = info
#
#     # Home = "地球"
#     # __Sxt = "女"
#     #
#     # def GetSex(self):
#     #     return Student.__Sxt
#     #
#     # def __init__(self,name,age,score,comp):
#     #     self.Name = name
#     #     self.Age = age
#     #     self.Score = score
#     #     self.__Comp = comp
#     #
#     # def GetComp(self):
#     #     return self.__Comp
#     #
#     # def SetComp(self,comp):
#     #     self.__Comp = comp
#
# # stu = Student("gou",21,98)
# #
# # stu.Home = "火星"
# # print(stu.Home)
# # print(stu.__class__.Home)
# # print(Student.Home)
#
# # stu1 = Student("a",11,33,"mac")
# # print(stu1.GetComp())
# # stu1.SetComp("deal")
# # print(stu1.GetComp())
# #
# # print(stu1.GetSex())
#
# # info = Info("a",18,98)
# stu = Student(Info("a",18,98))
#
# print(type(stu))
# print(type(stu.Info))
# print(stu.Info.Name)


#  信息基类
class MemberInfo(object):
    __name = None
    __age = None
    __score = None

    def __init__(self,name,age,score):
        print("parent init")
        self.__name = name
        self.__age = age
        self.__score = score

    def GetName(self):
        return self.__name

class Student(MemberInfo):
    def __init__(self,name,age,score,comp):
        MemberInfo.__init__(self,name,age,score)
        print("child init")
        self.__Comp = comp
    def GetComp(self):
        return self.__Comp

#
# # stu = Student("a",15,98)
# stu = Student("mac")
# print(stu.GetComp())

stu = Student("gou",18,98,"mac")
print(stu.GetName())
print(stu.GetComp())


相关文章
|
1天前
|
Python
Python从入门到精通:深入学习面向对象编程——2.1.2继承、封装和多态的概念
Python从入门到精通:深入学习面向对象编程——2.1.2继承、封装和多态的概念
|
2天前
|
Python
Python 一步一步教你用pyglet制作可播放音乐的扬声器类
Python 一步一步教你用pyglet制作可播放音乐的扬声器类
14 0
|
9天前
|
Python
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(上)
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(上)
52 0
|
9天前
|
索引 Python
python 格式化、set类型和class类基础知识练习(上)
python 格式化、set类型和class类基础知识练习
32 0
|
10天前
|
Python
python学习12-类对象和实例对象
python学习12-类对象和实例对象
|
1月前
|
Python
Python类(class)中self的理解
Python类(class)中self的理解
18 0
|
1月前
|
Python
Python类定义:从小白到专家的旅程
Python类定义:从小白到专家的旅程
8 0
|
1月前
|
Python
Python继承:深入探索与实际应用
Python中的继承是OOP三大特性之一,允许子类继承父类的属性和方法,实现代码重用和扩展。子类通过`class`关键字和父类名定义,支持单继承和多继承。子类可覆盖父类方法,使用`super()`调用父类同名方法。继承在实际应用中如游戏开发,可创建类体系,提高代码复用性,实现模块化和层次化。掌握继承对于构建高效软件系统至关重要。
|
1月前
|
Python
Python类与对象:深入解析与应用
本文介绍了Python中的核心概念——类和对象,以及它们在面向对象编程中的应用。类是用户定义的类型,描述具有相同属性和行为的对象集合;对象是类的实例,具备类的属性和方法。文章通过示例讲解了如何定义类、创建及使用对象,包括`__init__`方法、属性访问和方法调用。此外,还阐述了类的继承,允许子类继承父类的属性和方法并进行扩展。掌握这些概念有助于提升Python编程的效率和灵活性。
|
1月前
|
机器学习/深度学习 设计模式 开发者
python类用法(四)
python类用法(四)
18 0