zoukankan      html  css  js  c++  java
  • 第一周Python85

    面向对象编程:

    1、Python中用可以使用class关键字来定义类,然后加上函数来定义方法,这样就可以将对象动态的描述出来。

    2、Python2中object一定要写,但在Python3中可写可不写(默认就继承了object)      

             class Student(obiect):
    3、一个简单的类:
    class Student(object):
        #第二步初始化,初始化自己
        #初始化的作用:当你的类有一些公用变量的时候,你就可以初始化
        #初始化中经常放入公用的变量
        def __init__(self):
            print('hello')
    if __name__=='__main__':
        #实例
        student=Student()
    4、
    class Student(object):
        def __init__(self):
            self.name ='abc'#再类中,一切的变量和函数都要印记(self)
        def def1(self):#self不是参数,只是说明def1、def2都属于Student类
            print(self.name)
        def def2(self):
            print(self.name)
    if __name__=='__main__':
        #实例,类名带括号‘Student()’,直接运行初始化
        student=Student()
        student.def1()
        student.def2()
    

      

    class Student(object):
        def __init__(self,name):
            self.name=name
        def def1(self,num):
            self.num=num
            print(self.name)
            print(num)
        def def2(self):
            print(self.name)
            print(self.num)
    if __name__=='__main__':
        student=Student('abc')
        student.def1(100)
        student.def2()
    

    5、

    #王者荣耀,人机对战
    #1.点击人机对战的按钮(输入)
    #2.挑选人物(典韦、赵云、鲁班)
    #3.显示战力,技能,防御
    #4.人物确定,随机出一个人机(包括战力、名字等信息显示)
    #5.点击开始按钮
    #6.进入加载页面
    import numpy as np
    class wangzhe:
        def __init__(self):
            """
            初始化王者农药类.
            Arguments:
            ----------
                zhaoyun[zhaoyun_xx]:关于人物赵云的参数
                dianwei[dianwei_xx]:关于人物典韦的参数
                luban[luban_xx]:关于人物鲁班的参数
            """
            self.zhaoyun = '[1] 赵云'
            self.zhaoyun_zhanli = 250
            self.zhaoyun_fangyu = 250
            self.dianwei = '[2] 典韦'
            self.dianwei_zhanli = 300
            self.dianwei_fangyu = 1000
            self.luban = '[3] 鲁班'
            self.luban_zhanli = 10000
            self.luban_fangyu = 10
        def choose_model(self):
            """
            选择模式
            -------
                可选模式暂时只有一个1.
            """
            mode = input('可选模式[1/2]:1.人机对战,2.多人对战')
            if mode == '1':
                self.choose_people()
            else:
                print(' 正在开发中...不要着急!')
        def choose_people(self):
            """
            用户选择人物..
            """
            people = input('请选择你的英雄:(%s,%s,%s)'%(self.zhaoyun,self.dianwei,self.luban))
            if people == '1':
                print(' 赵云:')
                self.information(self.zhaoyun_zhanli,self.zhaoyun_fangyu)
                self.random_people()
            elif people == '2':
                print(' 典韦:')
                self.information(self.dianwei_zhanli,self.dianwei_fangyu)
                self.random_people()
            elif people == '3':
                print(' 鲁班:')
                self.information(self.luban_zhanli,self.luban_fangyu)
                self.random_people()
            else:
                print('(⊙_⊙)? 不懂你的选择')
                self.choose_people()
            
        def information(self,zhanli,fangyu):
            """
            打印各个人物的信息
            Arguments:
            ---------
                zhanli: 人物的战力参数
                fangyu: 人物的防御参数
            """
            print('战力:%s,防御%s'%(zhanli,fangyu))
    
        def random_people(self):
            """
            电脑随机选择人物..
            """
            print(' 电脑开始选择英雄:')
            people = np.random.choice(['1','2','3'])
            if people == '1':
                print(' 和你对战的是赵云:')
                self.information(self.zhaoyun_zhanli,self.zhaoyun_fangyu)
            elif people == '2':
                print(' 和你对战的是典韦:')
                self.information(self.dianwei_zhanli,self.dianwei_fangyu)
            elif people == '3':
                print(' 和你对战的是鲁班:')
                self.information(self.luban_zhanli,self.luban_fangyu)
            self.start()
        def start(self):
            """
            运行选择的模式
            """
            print('[+] 马上进入游戏...')
    
        def Runing(self):
            """
            启动函数...
            """
            print('[+] 正在加载...')
            self.choose_model()
    
    if __name__ == "__main__":
        runing = wangzhe()
        runing.Runing()
    

    6、私有变量:变量名前面加“__”

     
  • 相关阅读:
    命令安装mongod作为服务启动
    npm命令
    win服务
    win进程操作
    【数据结构与算法之美】7.排序算法之冒泡、选择、插入
    【leetcode】23.合并K个排序链表
    【leetcode】141.环形链表
    【数据结构与算法之美】6.环形队列
    【数据结构与算法之美】5.基于链表实现队列
    【数据结构与算法之美】4.基于数组实现队列
  • 原文地址:https://www.cnblogs.com/cqyyyyy/p/11301943.html
Copyright © 2011-2022 走看看