新式类和经典类,类和对象的使用,如何基于面向对象开发软件

day27

新式类与经典类

在python3中,所有的类都是新式类:

class A:  # python3默认继承object类
    pass

print(A.__bases__)  # 查询继承类是什么,结果是 (<class 'object'>,)

在python2中,新式类:

class B(object):  # 继承object类,python2里必须写,python3默认继承
    pass

class C(B):  # 继承B类,
    pass

print(B.__bases__)  # 查询继承类是什么 .结果是 (<class 'object'>,)
print(C.__bases__)  # 结果是 (<class '__main__.B'>,)

python2中,经典类:

class D:  # 什么也没继承
    pass

print(D.__bases__)  # 在python2解释器中,结果什么也没有

类和对象的使用

class Student:
    country = "China"

    def __init__(self, ID, NAME, SEX, PROVINCE):
        self.id = ID
        self.name = NAME
        self.sex = SEX
        self.province = PROVINCE

    def search_score(self):
        print("tell score")

    def study(self):
        print("study")

    def walk(self):
        print("%s is walking." % (self.name))


# 类属性:特征(变量)和技能(函数、绑定方法),python的属性与其他语言不同

# 类的用法:实例化,属性引用
s1 = Student("124345463243223", "cobila", "female", "sahngxi")
print(Student.country)
print(Student.search_score)
print(Student.search_score(s1))

# 对象也称为实例
# 对象的属性:对象本身就是特征(变量)

# 对象的用法:属性引用
s2 = Student("2222222222", "cobila22", "female2", "sahngxi2")
print(s2.country)
s2.search_score()
s2.study()

print(Student.__dict__)  # 查看类的名称空间,存到一个字典里的,结果如下:
# {'__module__': '__main__', 'country': 'China', '__init__': <function Student.__init__ at 0x0000020BB2D398C8>,
# 'search_score': <function Student.search_score at 0x0000020BB2D39950>, 'study': <function Student.study at 0x0000020BB2D399D8>,
# '__dict__': <attribute '__dict__' of 'Student' objects>, '__weakref__': <attribute '__weakref__' of 'Student' objects>, '__doc__': None}

print(s1.__dict__)  # 查看对象的名称空间,存到一个字典里的,结果如下:
# {'id': '124345463243223', 'name': 'cobila', 'sex': 'female', 'province': 'sahngxi'}


# 对象寻找名字步骤: 1.先从对象自己的__dict__里找,2. 再从类的__dict__那里找

# 对于变量,对象和类调用变量都是引用的同一个内存地址
print(id(s1.country))  # 值为 1726078219744
print(id(s2.country))  # 值为 1726078219744
print(id(Student.country))  # 值为 1726078219744

# 对于绑定方法/函数,对象和类调用时内存地址不一样
print(s1.study,id(s1.study))  # 值为 <bound method Student.study of <__main__.Student object at 0x000001F47F7AF128>> 2149612792904
print(Student.study, id(Student.study))  # 值为 <function Student.study at 0x000001F47F7999D8> 2149622323672

# 绑定方法的核心在于“绑定”,唯一绑定一个确定的对象
s1.walk()  # 结果:cobila is walking.
s2.walk()  # 结果:cobila22 is walking.

如何基于面向对象开发软件

# 应用场景
# 找不到共同特征和技能不用强求

# 对象:学校-----》归类
# 共有的特征:商标为etiantian
# 共有的特征:招生
# 独有的特征:地址不一样,老师们,课程
class School:
    tag = "etiantian"

    def __init__(self, address):
        self.address = address
        self.teacher_list = []
        self.course_list = []

    def zhaosheng(self):
        pass

        # 对象:老师-----》归类
        # 共同的特征:教课
        # 独有的特征:名字,性别,level,课程


class Teacher:
    def __init__(self, name, sex, level):
        self.name = name
        self.sex = sex
        self.level = level
        self.course_list = []

    def teach(self):
        pass

        # 对象:学生-----》归类
        # 共同的特征:
        # 共同的技能:search_score, handin
        # 独有的特征:学号,名字,性别,课程


class Student:
    def __init__(self, ID, name, sex):
        self.id = ID
        self.name = name
        self.sex = sex
        self.course_list = []

    def search_score(self):
        pass

    def handin(self):
        pass


class Course:
    def __init__(self, name, price, period):
        self.name = name
        self.price = price
        self.period = period


s1 = Student("111111", "alex", "male")
print(s1.id, s1.name, s1.sex)
# s1.course_list.append("python")
# s1.course_list.append("linux")
# print(s1.course_list)

python_obj = Course("python", "1688元", "5个月")
linux_obj = Course("linux", "9800元", "6个月")
s1.course_list.append(python_obj)
s1.course_list.append(list)
print('''
name: %s
course: %s
price: %s
''' % (s1.name, s1.course_list[0].name, s1.course_list[0].price))

版权声明:
作者:admin
链接:https://www.chenxie.net/archives/1664.html
来源:蜀小陈
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>