Summary eighteen

Object-oriented programming ideas oop

What is object-oriented:

  Is an object-oriented predecessors summed up the experience of programming ideas, is that programmers know how to write better programs, the core of the object, the program is a collection of objects, the programmer is responsible for scheduling these objects to interact with complete task.

Case: the elephant put into the refrigerator

  Process-oriented:

    1. Open the refrigerator

    2. Load elephant

    3. Close the refrigerator

  Object-oriented:

    Find the object loaded with elephants skills

 

In the object-oriented programmer's angle is changed from the operator into a specific commander

He stressed: object is not created out of thin air, we need to design their own

Case:

  Learn from the west

  Tathagata through a pile of books need to spread themselves too lazy to do, to find five objects (Tang master five) to help him do it, as long as the Tathagata is responsible for controlling the scheduling of the object can be, if an object is changed, it will not affect other objects (scalability)

 

Object-oriented three major advantages:

1. Scalability

2. Flexibility

3. reusability

Disadvantages:

1. The complexity of the program improved

2. not accurately predict the results

 

scenes to be used

High scalability requirements of the procedure, typically directly to the user, e.g. qq, micro-Letter

 

Process-oriented programming ideas

The core concern is the process, the process is step by step to step, which is to re-doing doing

Advantages: clear logic, simplifying complex issues, process

Disadvantages: poor scalability, maintainability

 

scenes to be used:

Requirements for lower extension procedures, for example: system kernel, Git, calculator

 

Note: Not all program objects to be oriented, have specific needs analysis

 

Classes and Objects: This is the core concepts in oop

class

I.e., type, class, an abstract concept, is a set of features and the same behavior with the same object

 

Objects

Is a thing existed specific, have their own characteristics and behavior

The object is a combination of characteristics and skills

 

The relationship between classes and objects

Class contains a series of objects

The object belongs to a class

In life there is a first, and then the object class

While in the program is the first class in order to have an object, we must first tell the computer such objects have characteristics and behaviors

A summary conclusion: When using object-oriented programming, the first step is to think about what kind of object needs, what kind of an object with characteristics and behavior, which summed up the type of information required in accordance with

 

Creating classes and objects

The method defined in class

'''
class 类的名称:
    # 类中的内容  描述属性和技能
    # 描述属性用变量
    # 描述行为用函数

# 类名称  书写规范  首先是见名知意  名称是大驼峰命名法
#驼峰就是单词首字母大写,大驼峰是第一个字母大写,小驼峰是第一个字母小写
'''

创建对象的语法:

'''
class Student
    pass

# 创建对象的语法 调用类 类名加括号
stu = Student()
print(stu)
print(Student)
'''

创建对象

p = Person()

 

属性的写法:

属性可以写在类中,类中的属性是所有对象公共的

也可以写在对象中,对象中的属性,是每个对象独特的(不一样的)

class Person:
    """这是一个人类型"""
    desc = "都会吃饭"

    name = "张三分"
    # age = 20
    # sex = "male"
    print("class run....")


p1 = Person()
print(p1.name)

p2 = Person()
print(p2.name)

p3 = Person()
p3.name = "李四"

p4 = Person()
# p4.name = "王五"

print(p3.name)

 

 

访问顺序:

如果类中和对象中存在同样的属性,先访问对象,如果没有再访问类

# 练习:描述一个老师类,需要包含一个公共属性和一个独特属性

class Teacher:
    school = "alade"

t1 = Teacher()
t1.name = "jack"
t1.age = 18

 

属性的增删改查

增加属性:对象变量名称.属性名称 = 属性值

删除属性:del 对象的变量名称.属性名称

修改:对象.属性 = 新的值

查看属性:

访问的是对象的所有属性:print(对象.__dict__)

访问对象的类信息:print(对象.__class__)

 

init 方法

叫做初始化方法,本质上就是一个函数

特点1:当实例化对象时,会自动执行init方法

特点2:会自动将对象作为第一个参数传入,参数名称为self,self可以是别的名字,但不建议改

功能:用户给对象赋初始值

# 练习:创建一个类具备几个属性,通过初始化方法来给它设置属性

class Dog:
    def __init__(self,kind,color,age):
        self.kind = kind
        self.color = color
        self.age = age

d1 = Dog("二哈","黑白",1)
d2 = Dog("阿拉斯加","棕白",2)

# 注意:改函数不能有任何返回值,只能是None(别问为啥,规定就这样)

 

初始化方法不仅仅用于赋值

class PC:
    def __init__(self,kind,price,sys):
        self.kind = kind
        self.price = price
        self.sys = sys
        # 处理赋值意外的初始化操作  例如,启动BIOS等
        print("启动BIOS")
        print("启动系统分区")
        print("加载系统界面")
        print("启动成功......")

p = PC("apple",16888,"macOS")
print()
class Student:

    school = "oldgirl"

    def __init__(self,name,age,gender):
        self.name = name
        self.age = age
        self.gender = gender

    # def study(self):
    #     print(self)

    def say_hi(self):
        # print(self)
        print("hello i am a student! my name is %s" % self.name)

    def a(self):
        pass


stu1 = Student("jack",20,"male")
stu2 = Student("rose",18,"female")


stu1.say_hi()
stu2.say_hi()
print(stu1)
stu2.say_hi()

Student.say_hi(stu1)

print(type(Student.say_hi))
print(type(stu1.say_hi))
为对象定制行为

 

对象的精髓就是将数据和处理数据的函数整合到一起,这样一来拿到一个对象就同时拿到了需要处理的数据以及处理数据的函数

 

对象的绑定方法

默认情况下类中的方法都是对象绑定方法,其特殊之处在于:

1.当使用对象调用该函数时,会自动传入对象本身,作为第一个参数

2.当使用类名来调用时,它就是一个普通函数,有几个参数就得传几个参数

# 练习:写一个学生类,具备一个打招呼的技能,要能输出自己的名字

class Student:
    def __init__(self,name):
        self.name = name
    
    def say_hi(self):
        print("hello i am a student! my name is %s" % self.name)
stu = Student("jack")

 

类绑定方法

类绑定方法用@classmethod来装饰

特殊之处:不管用类还是对象调用,都会自动传入类本身,作为第一个参数

什么时候绑定给对象:当函数逻辑需要访问对象中的数据时

什么时候绑定给类:当函数逻辑需要访问类中的数据时

 

非绑定方法

又叫做静态方法,就是既不需要访问类的数据,也不需要访问对象的数据

语法:@staticmethod    不常用

 

class Student:
    school = "alade"
    
    def __init__(self,name):
        self.name = name
    
    @classmethod
    def show_school(cls):
        # print(self.school)
        print(cls)

    @staticmethod
    def print_hello():
        print("hello world")

Student.show_school(stu)
Student.show_school()
print(Student)

stu = Student("jack")
# stu.show_school()

Student.print_hello()
stu.print_hello()

 

 

 

 

# 练习:为学生类添加一个save方法(save是将对象存储到文件中)  一个get方法(get是从文件中获取对象) 

import os
import pickle
import time

class Student:
    def __init__(self,name):
        self.name = name
 
    def say_hi(self):
        print("name:",self.name)
    
    def save(self):
        with open(self.name,"wb") as f:
            pickle.dump(self,f)

    @staticmethod
    def get(name):
        with open(name,"rb") as f:
            obj = pickle.load(f)
            return obj
stu = Student("jack")
stu.save()
stu1 = Student("rose")
stu1.save()

obj = Student.get("jack")
print(obj.name)
obj1 = Student.get("rose")
print(obj.name)

print(Studern.__name__)

 

 

 

对象交互练习

 

import random
import time

class Hero:
    def __init__(self,name,level,blood,att,q_hurt,e_hurt,e_hurt):
        # 简便写法
        lcs = locals()
        lcs.pop("self")
        self.__dict__.update(lcs)
    
    def attack(self,enemy):
        enemy.blood -= self.att
        print("%s对%s释放了普通攻击 造成了%s的伤害 敌人剩余血量%s" % (self.name, enemy.name, self.att, enemy.blood))
        if enemy.blood <= 0:
            print("%s被%s使用普通攻击击杀了" % (enemy.name,self.name))

    def Q(self,enemy):
        enemy.blood -= self.q_hurt
        print("%s对%s释放了Q 造成了%s的伤害 敌人剩余血量%s" % (self.name, enemy.name, self.q_hurt, enemy.blood)
        if enemy.blood <= 0:
            print("%s被%s使用Q技能击杀了" % (enemy.name,self.name)

    def W(self,enemy):
        enemy.blood -= self.w_hurt
        print("%s对%s释放了W 造成了%s的伤害 敌人剩余血量%s" % (self.name, enemy.name, self.w_hurt, enemy.blood))
        if enemy.blood <= 0:
            print("%s被%s使用W技能击杀了" % (enemy.name, self.name))

    def E(self,enemy):
        enemy.blood -= self.e_hurt
        print("%s对%s释放了E 造成了%s的伤害 敌人剩余血量%s" % (self.name,enemy.name,self.e_hurt,enemy.blood))
        if enemy.blood <= 0:
            print("%s被%s使用E技能击杀了" % (enemy.name, self.name))

h1 = Hero("亚索",20,2500,300,600,0,1000)
h2 = Hero("妲己",20,2000,100,600,500,1000)
h3 = Hero("鲁班",20,1500,700,300,200,300)
h4 = Hero("蔡文姬",20,3000,100,100,100,0)

# h1.attack(h2)
# h2.Q(h1)
# h2.E(h1)
# h2.W(h1)

# 从字典中随机拿出一个值

def  random_hero(heros):
    hero_index = random.randint(1, len(heros))
    return heros[hero_index]


while True:
    # 把所有的攻击方法装到字典里  为了随机取出一个
    funcs = {1: Hero.Q, 2: Hero.W, 3: Hero.E, 4: Hero.attack}
    func_index = random.randint(1, 4)
    func = funcs[func_index]


    # 把所有的英雄方法装到字典里  为了随机取出一个
    heros = {1: h1, 2: h2, 3: h3, 4: h4}
    hero = random_hero(heros)

    # 剩余的英雄们
    other_heros = {}
    new_index = 1
    for k, v in heros.items():
        if v != hero:
            other_heros[new_index] = v
            new_index += 1

    # 从剩余的英雄中随机挑出一个英雄来挨打
    enemy = random_hero(other_heros)
    # 打他
    func(hero, enemy)
    if enemy.blood <= 0:
        break
    time.sleep(0.5)

 

Guess you like

Origin www.cnblogs.com/TZZ1995/p/11241253.html