设为首页 加入收藏

TOP

python — 面向对象(二)
2019-08-24 00:07:58 】 浏览:122
Tags:python 面向 对象
sword = pwd self.email = email USER_LIST = [对象(用户/密码/邮箱),对象(用户/密码/邮箱),对象(用户/密码/邮箱)] while True: user = input('请输入用户名:') pwd = input('请输入密码:') email = input('请输入邮箱:') p = Person(user,pwd,email) USER_LIST.append(p) for item in USER_LIST: temp = "我的名字:%s,密码:%s,邮箱%s" %(item.username,item.password,item.email,) print(temp) # ########## 面向对象写法 class Person: def __init__(self,user,pwd,email): self.username = user self.password = pwd self.email = email def info(self): return "我的名字:%s,密码:%s,邮箱%s" %(item.username,item.password,item.email,) USER_LIST = [对象(用户/密码/邮箱),对象(用户/密码/邮箱),对象(用户/密码/邮箱)] while True: user = input('请输入用户名:') pwd = input('请输入密码:') email = input('请输入邮箱:') p = Person(user,pwd,email) USER_LIST.append(p) for item in USER_LIST: msg = item.info() print(msg)

2.4 小练习:游戏开发

class Police:
    def __init__(self,name) 
        self.name = name 
         self.hp = 10000
    
    def tax(self):
        msg = "%s收了个税。" %(self.name,)
        print(msg)
    
    def fight(self):
        msg = "%s去战了个斗。" %(self.name,)

lsq = Police('张三') 
zzh = Police('渣渣会')
tyg = Police('堂有光')


class Bandit:
    def __init__(self,nickname) 
        self.nickname = nickname 
        self.hp = 1000
    
    def murder(self,name):
        msg = "%s去谋杀了%s" %(self.nickname, name,)
        
        
lcj = Bandit('二蛋')
lp = Bandit('二狗')
zsd = Bandit('狗蛋')

# 1. 二狗去谋杀渣渣会,二狗生命值-100; 渣渣会生命值减5000
lp.murder(zzh.name)
lp.hp = lp.hp - 100
zzh.hp = zzh.hp - 5000
# ... 
class Police:
    def __init__(self,name) 
        self.name = name 
         self.hp = 10000
    
    def dao(self,other):
        msg = "%s个了%s一刀。" %(self.name,other.nickname)
        self.hp = self.hp - 10
        other.hp = other.hp - 50
        print(msg)
    
    def qiang(self):
        msg = "%s去战了个斗。" %(self.name,)
        
    def quan(self,other):
        msg = "%s个了%s一全。" %(self.name,other.nickname)
        self.hp = self.hp - 2
        other.hp = other.hp - 10
        print(msg)
        
        
class Bandit:
    def __init__(self,nickname) 
        self.nickname = nickname 
        self.hp = 1000
    
    def qiang(self,other):
        msg = "%s个了%s一全。" %(self.nickname,other.name)
        self.hp -= 20
        other.hp -= 500   
    
lcj = Bandit('二蛋')
lsq = Police('张三')
lsq.dao(lcj)
lsq.quan(lcj)
lcj.qiang(lsq)

3 面向对象的三大特性

3.1 封装

封装思想:将同一类的函数封装到了同一个py文件中,以后方便使用。

面向对象也有封装的作用:将同一类的函数封装到一个类中。(广义)

广义的封装 :类中的成员

狭义的封装 :私有成员

            只能在类的内部使用,既不能在类的外部调用,也不能在子类中使用

如果写代码时,函数比较多比较乱。

  • 1.可以将函数归类并放到同一个类中。
  • 2.函数如果有一个反复使用的公共值,则可以放到对象中。
class File:
    def read(self):
        pass
    def write(self):
        pass
class Person:
    def __init__(sef,name,age):
        self.name = name
        self.age = age
p = Person('alex',19)

3.2 继承

# 父类(基类/超类)
class Base:
    def f1(self):
        pass
# 子类(派生类)
class Foo(Base):
    def f2(self):
        pass

# 创建了一个子类的对象
obj = Foo()
# 执行对象.方法时,优先在自己的类中找,如果没有就是父类中找。
obj.f2()
obj.f1()

# 创建了一个父类的对象
obj = Base()
obj.f1()

问题:什么时候才能用到继承?

  • 多个类中如果有公共的方法,可以放到基类中避免重复编写。
class Base:
    def f1(self):
        pass
    
class Foo(Base):
    def f2(self):
        pass
    
class Bar(Base):
    def f3(self):
        pass

obj1 = Foo()
obj2 = Bar()

继承关系中的查找方法的顺序:

# 示例一
class Base:
    def f1(self):
        print('base.f1')
        
class Foo(Base):
    def f2(self):
        print('foo.f2')
        
obj = Foo()
obj.f1()
obj.f2()

# 示例二
class Base:
    def f1(self):
        print('base.f1')
        
class Foo(Base):
    def f2(self):
        self.f1()
        print('foo.f2')
        
obj = Foo()
obj.f2()

# 示例三
class Base:
    def f1(self):
        print('base.f1')
        
class Foo(Base):
    def f2(self):
        self.f1()
        print('foo.f2')
    def f1(self):
        prin
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇[Python]基于tkinter的九型人格测.. 下一篇day04_步入百万年薪的第四天——..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目