day15-2018-11-6 初识面向对象

# # class表示创建一个类
# # Car 类名. 类是对事物的定义. 归类
# class Car:
#     pass
#
# c = Car() # 把类概念进行实例化. 通过类来创建对象
# # # 对象 = 类()
#
# c.lunzi = "圆的"  # 属性
# c.color = "骚红色"
# c.pailiang = "6.3T"
# c.paizhao = "京A66666"
# # . 的
# print(c.pailiang)
# print(c.color)
# print(c.paizhao)
# print(c.lunzi)
#
# #  对象.属性
# c.suoyouren = "周杰伦"
# print(c.suoyouren)
# # print(c.price) # AttributeError: 'Car' object has no attribute 'price'
#
#
# c2 = Car()
# print(c2.color)

# 类名首字母大写.
class Computer:

    # 构造方法. 在创建的对象的时候由系统自动访问这个方法
    # self: 自己. 自身. 类的对象. 当前正在创建的对象
    def __init__(self, pinpai, price, cpu, neicun): # 谁在调用, self就是谁, self 由python自动传递
        self.pinpai = pinpai
        self.price = price
        self.cpu = cpu
        self.neicun = neicun

    # 方法. 该类型的对象能够执行哪些操作
    def dayouxi(self, game): # 谁调用的这个方法. self就是谁
        # 我用我的self.pinpai的调用. 利用很牛B的self.cpu来完成吃鸡操作
        print("我要打游戏, 我要%s" % game)

c = Computer("微软",25000,"i9","5T") # 创建对象
c.dayouxi("吃鸡")


# c2 = Computer("mac book", 22000, "i7", "16G")
# print(c2.price)

# c.pinpai = "神州"
# c.price = 100
# c.cpu = "赛扬双核"
# c.neicun = "1G"
#
# c2 = Computer()
# c2.pinpai = "小米"
# c2.price = 10000
# c2.cpu = "I7-8750"
# c2.neicun = "1T"
#
# print(c2.pinpai) # 小米
# print(c.pinpai) # 神州


# 思考过程:
# 准备创建类 -> 定义__init__()  self.xxx = xxxx
# 对象能干什么事儿?  方法. def 方法名(self, 参数...)

# 练习题:
#  1. 创建⼀个武松. 武松可以打老虎, 杀嫂⼦, 替天⾏道
# class HaoHan:
#     def __init__(self, waihao, name, wuqi, jineng):
#         self.waihao = waihao
#         self.name = name
#         self.wuqi = wuqi
#         self.jineng = jineng
#
#     def dalaohu(self):
#         pass
#
#     def shasaozi(self):
#         pass
#
#     def titianxingdao(self):
#         pass
#
# ws = HaoHan("行者", "武松", "拳头", "王八拳")
# ws.dalaohu()
# ws.shasaozi()
# ws.titianxingdao()
#
# lk = HaoHan("黑旋风", "李逵", "双板斧", "旋风斩")
# lk.dalaohu()
# lk.shasaozi()
# lk.titianxingdao()


#  2. ⽤⾯向对象的思维来模拟LOL⾥的盖伦(英雄)上阵杀敌.
# class Tank:
#     def __init__(self, name, waihao, hp, fy):
#         self.name = name
#         self.waihao = waihao
#         self.hp = hp
#         self.fy = fy
#
#     def kang(self):
#         pass
#
#     def zhengyishenpan(self):
#         pass
#
# gailun = Tank("盖伦","德玛西亚之力", 5000, 200)

#  3. 编写和尚类. ⾃⼰⾃由发挥和尚有哪些属性和⽅法.
# class HeShang:
#     def __init__(self, fahao, simiao):
#         self.fahao = fahao
#         self.simiao = simiao
#
#     def nianjing(self):
#         pass
#
#     def marray(self):
#         pass

# 因果报应 -> 做好事儿. 向善
#  4. ⽤⾯向对象的思维来完成⽤户登录.
class User:
    def __init__(self, username, password):
        self.username = username
        self.password = password

    def login(self, uname, upws):
        if uname == self.username and upws == self.password:
            return True
        else:
            return False
#
u1 = User("alex", "123")
ret = u1.login(input("请输入你的用户名"), input("请输入密码"))
print(ret)
# 要把大象装冰箱
# def open():
#     print("开门")
# def zhuang():
#     print("装大象")
# def close():
#     print("关门")
#
# open()
# zhuang()
# close()

# class DaXiang:
#     def open(self):
#         print("开门")
#     def jinbingxiang(self):
#         print('进冰箱')
#     def guanmen(self):
#         print("关门")
#
# dx = DaXiang()
# dx.open()
# dx.jinbingxiang()
# dx.guanmen()

# 主语发生了改变
# 函数式编程. 简单的基本功能还是函数式编程更加好用
# 面向对象: 如果功能非常复杂的时候.


# #  alex大战奥特曼
# def daZhanAoTeMan(name, wuqi, jineng):
#     print("%s 使用 %s 配合 %s技能狂怼奥特曼" % (name, wuqi, jineng))
# # alex大战哥斯拉
# def daZhanGeSiLa(name, wuqi, jineng):
#     print("%s 使用 %s 配合 %s技能狂怼哥斯拉" % (name, wuqi, jineng))
# # alex大战蝙蝠侠
# def daZhanBianFuXia(name, wuqi, jineng):
#     print("%s 使用 %s 配合 %s技能狂怼蝙蝠侠" % (name, wuqi, jineng))
#
# daZhanAoTeMan("alex", "菜刀", "动感光波")
# daZhanGeSiLa("alex", "菜刀", "动感光波")
# daZhanBianFuXia("alex", "菜刀", "动感光波")

# class Person:
#     def __init__(self, name, wuqi, jineng, taopaoluxian):
#         # 封装: 对属性的封装.
#         self.name = name
#         self.wuqi = wuqi
#         self.jineng = jineng
#         self.taopaoluxian = taopaoluxian
#
#
#     # 对方法进行封装
#     def daZhanAoTeMan(self):
#         print("%s 使用 %s 配合 %s技能狂怼奥特曼" % (self.name, self.wuqi, self.jineng))
#
#     # alex大战哥斯拉
#     def daZhanGeSiLa(self):
#         print("%s 使用 %s 配合 %s技能狂怼哥斯拉" % (self.name, self.wuqi, self.jineng))
#
#     # alex大战蝙蝠侠
#     def daZhanBianFuXia(self):
#         print("%s 使用 %s 配合 %s技能狂怼蝙蝠侠" % (self.name, self.wuqi, self.jineng))
#
# a1 = Person("alex", "菜刀", "动感光波", "打车跑")
# a1.daZhanAoTeMan()
# a1.daZhanBianFuXia()
# a1.daZhanGeSiLa()


# 面向对象比面向过程复杂

# 封装:
#
# class User:
#     def __init__(self, username, password):
#         self.username = username
#         self.password = password
#
#     def login(self):
#         pass
#     def regist(self):
#         pass
#     def update_psw(self):
#         pass


# db_package

# dbutil
class DBUtil:
    def __init__(self, ip, username, password):
        self.ip = ip
        self.username = username
        self.password = password

    # 对功能的封装
    def test_connect(self):
        pass

    def connect(self):
        pass

    def add(self):
        pass

    def upd(self):
        pass

    def remove(self):
        pass

    def sel(self):
        pass


# 文件读取 工具类
# excel doc txt 视频 图片
class FileUtil:

    def __init__(self, path):
        self.path = path
        self.f = open(path, mode="rb")

    def excel_read(self):
        self.f.seek(0)
        for line in self.f:
            pass

    def doc_read(self):
        pass

    def img_read(self):
        pass

    def movie_read(self):
        pass
# 继承: 子类自动拥有父类中除了私有内容外的其他所有内容
# 现在我们写的内容没有私有的

# class Niu:
#     def da(self):
#         print("牛魔王打架很厉害")
#
# class HongHaiEer(Niu):
#     def da(self):
#         print("喷火")
#
# hhl = HongHaiEer()
# hhl.da()


# class Animal:
#     def chi(self):
#         print("喜欢吃东西")
#
# # 当程序中出现了 x是一种y. x可以继承y
# class Cat(Animal):
#     pass


# class Foo:
#     def a(self):
#         pass
#
#     def b(self):
#         pass
#
# class Bar(Foo): #  子类在父类的基础上扩展了一些功能. 派生
#     def c(self):
#         pass
#
# b = Bar()
# b.a()
# b.b()
# b.c()

# class DBUtil:
#
#     def __init__(self, ip, username, password):
#         pass
#     def test_connect(self):
#         pass
#     def connect(self):
#         pass
#     def close(self):
#         pass
#
# class Mysql_Util(DBUtil):
#
#     def add(self):
#         pass
#     def delete(self):
#         pass
#     def update(self):
#         pass
#
# class Oracle_Util(DBUtil):
#
#     def add(self):
#         pass
#     def delete(self):
#         pass
#     def update(self):
#         pass
# 同一个对象. 拥有多种形态 java c++
# class Aniaml:
#     def dong(self):
#         print("动物可以动")
#
#
# class Cat(Aniaml):
#     def chi(self):
#         print("猫喜欢吃鱼")
#
# a = Cat()
# a.chi() # 站在猫的角度. 可以吃
# a.dong() # 站在动物的角度可以动

# 在python中多态的效果感受不够明确
class YaoGuai:
    def teng(self):
        print("妖怪一定会疼")

class ZhiZhuJing(YaoGuai):
    def teng(self):
        print("蜘蛛精很痛")

class HeiXiongJing(YaoGuai):
    def teng(self):
        print("黑熊精也很疼")

class BaiGuJing(YaoGuai):
    def teng(self):
        print("白骨精骨头很疼")

class JinJiaoDaWang(YaoGuai):
    def fangundeteng(self):
        print("爆炸的疼")

class SunWuKong:
    def da(self, yg):
        yg.teng() #  只要包含了xxx功能的内容都可以试用. 鸭子模型  ->  多态性

# 西游记开始
swk = SunWuKong()

hxj = HeiXiongJing()
swk.da(hxj)

bgj = BaiGuJing()
swk.da(bgj)

zzj = ZhiZhuJing()
swk.da(zzj)

alex = JinJiaoDaWang()
swk.da(alex)
class Foo1:
    def money(self):
        print("有钱")

class Foo2:
    def play(self):
        print("玩儿")

    def money(self):
        print("没钱")

class Bar(Foo2, Foo1): # MRO c3算法
    pass

b = Bar()
b.money()

猜你喜欢

转载自www.cnblogs.com/VastTry/p/9917107.html