马士兵python学习杨淑娟老师第九章--面向对象

# # 面向对象从宏观把握事物之间的复杂关系,方便分析整个系统,具体到微观操作,仍然使用面向过程方式处理
# # python中一切皆对象,包括int、字符串等
# # 定义类
# class Student:        # 定义一个类,类名由一个或多个单词组成,每个单词首字母大写,其余小写
#     pass                # 这是一个简单的pass语句,不进行任何功能。
# print(id(Student)) #2167957162048
# print(type(Student)) #<class 'type'>
# print(Student) #<class '__main__.Student'>

# # 类的组成和简单的使用
# class Student:        # 定义一个抽象类,继承自一个或多个类或函数,这些类可以
#     native_space = '河南' #直接写在类里的变量称为类属性,被该类的所有对象所共享
#     def __init__(self,name,age):
#         self.name=name #self.name称为实体属性,进行一个赋值操作,将局部变量name值赋值给实体属性
#         self.age=age #self指向的是类的一个实例,一个类只能有一个实例。 
#     #实例方法
#     def eat(self):
#         print('学生吃饭。。')
#     # 静态方法,可以使用类名直接访问的方法
#     @staticmethod
#     def Smethod():
#         print('采用staticmethod修饰的方法为静态方法')
#     # 类方法,可以使用类名直接访问的方法
#     @classmethod    # 这个方法也可以在类的实例方法中使用
#     def Clamethod(cls):
#         print('使用classmethod修饰的方法为类方法')
# # 在类之外定义的称为函数,其不属于某个类,在类之内定义的称为方法
# def talk():        # 定义一个函数,名称随意
#     print('我在类外,是一个函数')
# # 创建student类的对象
# s1=Student('Tyler Bennett','32') #s1是一个类的实例,名称叫做Student
# print(id(s1)) #1732278433296 打印出id值 打印出类的实例值
# print(type(s1)) #<class '__main__.Student'> 打印出类的类型
# s1.eat() #调用Student的eat方法,s1是一个类的实例,采用对象名.方法名调用
# print(s1.name)
# Student.eat(s1) #调用Student的eat方法,与s1.eat()执行的功能相同,采用类名.方法名(类的对象)调用。
# # 类属性的使用
# s2=Student('John Rappl','19') #创建一个Student类的实例,名称叫做John Rappl
# print(s1.native_space)
# print(s2.native_space)
# Student.native_space = 'Japan' #修改类属性值
# print(s1.native_space)
# print(s2.native_space)
# # 类方法的使用,类方法也可以在类的实例方法中使用
# Student.Clamethod()
# # 静态方法的使用
# Student.Smethod()
# # 为s2动态绑定性别属性
# s2.gender = 'Male' #将属性设置给s2实例
# print(s2.name,s2.age,s2.gender)
# # 为s2动态绑定函数
# s2.talk=talk #将函数设置给s2实例的talk方法,s2实例也会收到这个调用。
# s2.talk() #执行s2实例的talk方法

# # 面向对象的三大特征:封装(提高安全性)、继承(提高代码的复用性)和多态(提高代码的扩展性和可维护性)
# # python中没有专门的修饰词用于属性的私有,如果想私有属性,即不希望再类对象外部被访问,前边使用两个“_”
# # 封装
# class Car:
#     def __init__(self, brand):
#         self.__brand = brand  #不希望在外部被使用可以加两个_  , 例如: self.__age=age
#     def start(self):
#         print(self.__brand)
#         print('汽车已启动..')
 
# car=Car('宝马X5')
# car.start()
# # 如果需要使用类定义的私有变量,可以采用_Car__brand,但是不建议这样去访问
# print(dir(Car))
# print(car._Car__brand)

# #继承(如果一个类没有继承任何类,则默认继承object)
# # python支持多继承,定义子类时,必须在其构造函数中调用父类的构造函数
# class Person(object):
#     def __init__(self,name,age):
#         self.name=name 
#         self.age=age
#     def info(self):
#         print(self.name,self.age)
# class Student(Person):
#     def __init__(self, name, age, stu_no):
#         super().__init__(name, age) #calling the constructor of the parent class.
#         self.stu_no=stu_no
# class Teacher(Person):
#     def __init__(self, name, age, teacheryear):
#         super().__init__(name, age) #calling the constructor of the parent class.
#         self.teacheryear=teacheryear  #overriding the parent's constructor.
# stu=Student('John',19,'S101') #object of the class.
# stu.info() #calling the info() method of the object.但是本函数不会打印stu的学号,所以需要修改info函数
# teacher=Teacher('Mary',18,'B101') #object of the class.
# teacher.info() #calling the info() method of the object.

# # 重写:如果对父类的某个属性或方法不满意,可以在子类对其方法进行重新编写
# class Person(object):    #Person继承object类
#     def __init__(self,name,age):
#         self.name=name
#         self.age=age
#     def info(self):
#         print(self.name,self.age) 
# class Student(Person):
#     def __init__(self,name,age,stu_no):
#         super().__init__(name,age)
#         self.stu_no=stu_no
#     def info(self): #重写info类方法(先调用父类的info,再重写子类的info)
#         super().info() #通过super().info()方法调用父类的info。
#         print('学号:',self.stu_no) 
# class Teacher(Person):
#     def __init__(self,name,age,teachofyear):
#         super().__init__(name,age)
#         self.teachofyear=teachofyear
#     def info(self): #重写info类方法(先调用父类的info,再重写子类的info)
#         super().info()
#         print('教龄:',self.teachofyear) 
# stu=Student('茆俊亚',23,'M210811448')
# teacher=Teacher('李四',34,10) 
# stu.info()
# print('-------------------')
# teacher.info()

# # object类是所有类的父类,所有类都有object类的属性和方法
# # 内置函数dir()可以查看指定对象所有的属性
# # object的__str__()方法,可以返回一个对于对象的描述,对应于内置函数str()经常用于print()方法,所以经常对__str__()进行重写
# class Student:
#     def __init__(self,name,age):
#         self.name=name
#         self.age=age
#     def __str__(self):     #重写父类的方法
#         return'我的名字是{0},今年{1}岁了'.format(self.name,self.age) 
# stu1=Student('茆俊亚',23)
# print(dir(stu1)) #采用dir(stu1)查看student所具有的属性和方法
# print(stu1)   #原来输出的是对象的内存地址,一旦重写str类型后,默认会调用__str__方法,输出里面的内容
# print(type(stu1))
 
# 类对象后面加括号代表着实例化
# java是静态语言(静态语言实现多态的三个必要条件:继承,方法重写,父类引用指向子类对象),
# python是动态语言

# # 多态性. 创建对象时,根据实际情况选择合适的类型。即运行过程中根据变量所引用对象的类型,动态决定调用哪个对象中的方法
# class Animal:
#     def eat(self):
#         print('动物吃') 		#this is the most important part of the code.
# class Dog(Animal): #Dog extends Animal. Dog is the most specific class. Dog is the most generic class.
#     def eat(self):
#         print('狗吃骨头') 
# class Cat(Animal): #Cat is a subclass of Animal.
#     def eat(self): 		#Cat is another class. It is also the most generic class.
#         print('猫吃鱼') 		#Cat is also the most generic class.
# class Person: #Person is a subclass of Object. Person is the most generic class.
#     def eat(self):
#         print('人吃五谷杂粮')
# def fun(obj):
#     obj.eat() #calling the eat method of obj instance.
# fun(Dog())
# fun(Cat())
# fun(Person())

# # 类的特殊属性
# class A:
#     pass
# class B:
#     pass
# class C(A,B):
#     def __init__(self,name,age):
#         self.name=name
#         self.age=age
# class D(A):
#     pass
# #创建C类的对象
# x=C('Jack',20)      #x是C类型的一个实例对象
# print(x.__dict__)    #实例对象的属性字典
# print(C.__dict__)
# print('---------------------------------------------------------------------------------')
# print(x.__class__)        #<class '__main__.C'> 输出了对象所属的类
# print(C.__bases__)        #C类的父类的元组
# print(C.__base__)         #类的基类,和它挨的最近的,例如C(A,B)就是A , C(B,A)就是B
# print(C.__mro__)          #类的层次结构
# print(A.__subclasses__()) #子类的列表
 
# # 特殊方法
# a = 20
# b = 100
# # 两个整数类型的相加操作
# print(a+b)
# print(a.__add__(b))
# # 两个对象实例相加:在Student类中,编写__add__()特殊方法
# class Student:
#     def __init__(self,name):
#         self.name = name
#     def __add__(self,other): #other is an instance of the class or a class itself.
#         return self.name+other.name #other can be a variable or a function.
#     def __len__(self): #len( is a built-in function. It returns the length of a string.
#         return len(self.name) #returns the length of the string.
# stu1 = Student('Jack') #instantiate the object.
# stu2 = Student('Mary') #instantiate another object.
# print(stu1+stu2) #outputs "JackMary"
# print(stu1.__add__(stu2)) #outputs "JackMary"
# # __len__() is a special method.
# print(len(stu1)) #outputs 4.
# lst = [11,12,13,14]
# print(len(lst))
# print(lst.__len__()) #outputs 4.

# # 类的特殊方法:__new__()和__init__()两个属性。
# class Person:
#     def __new__(cls,*args,**kwargs):
#         print('__new__被调用了,cls的id值为:{0}'.format(id(cls)))
#         obj=super().__new__(cls)     #调用object的new方法
#         print('创建对象的id为:{0}'.format(id(obj)))
#         return obj
#     def __init__(self,name,age):
#         print('__init__被调用了,self的id值为:{0}'.format(id(self)))
#         self.name=name
#         self.age=age 
# print('object这个类对象的id为:{0}'.format(id(object)))
# print('Person这个类对象的id为:{0}'.format(id(Person))) 
# #创建Person类的实例对象
# p1=Person('张三',20)
# print('p1这个Person类的实例对象的id:{0}'.format(id(p1))) 
# #总结:
# # 1.obj 就是 self 也是 p1
# # 2.new在前是创建对象,init在后是为对象的实例属性进行赋值,最后将创建的对象放到p1当中进行存储
 
# 类的浅拷贝和深拷贝 
class CPU:
    pass
class Disk:
    pass
class Computer:
    def __init__(self,cpu,disk):
        self.cpu=cpu
        self.disk=disk 
cpu1=CPU()     #创建一个cpu类的对象
disk1=Disk()     #创建一个硬盘类的对象
computer1=Computer(cpu1,disk1)   #创建一个计算机类的对象
print('----------------------------------------------------------------------------------------------------------------')
'''----------类的浅拷贝(对象包含的子对象内容不拷贝,只拷贝源对象)-----------'''
import copy
computer2=copy.copy(computer1)
print(computer1,computer1.cpu,computer1.disk)
print(computer2,computer2.cpu,computer2.disk) 
print('------------------------------------------------------------------------------------------------------------')
'''-----------类的深拷贝(源对象和对象包含的子对象内容都要重新拷贝一份)-----------'''
computer3=copy.deepcopy(computer1)
print(computer1,computer1.cpu,computer1.disk)
print(computer3,computer3.cpu,computer3.disk)
 
 
    




 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值