[0826 | Day 23] objects / classes and object-oriented / generation object / objects Everything

Object-Oriented

A process-oriented programming object-oriented programming vs

Process-oriented programming:

  • Benefits: streamlined
  • Disadvantages: poor scalability

Object-Oriented Programming:

  • Advantages: high scalability
  • Cons: write complex

Classes and Objects

First, the basic introduction

Combination of properties / methods: Object

Class: number of common methods and attributes (represented by hump material)

Note: In the process, the first class object, represented by the variable properties, methods represented by a function.

#生成对象:类加括号生成对象
class Student()

#赋变量名
stu1 = Student()
stu2 = Student()

#获取属性和方法:通过'.'获取
print(stu1.school)
print(stu2.choose)  #绑定方法

#查看类中的属性和方法
print(Student.__dict__) #属性和方法丢进字典
print(Student.__dict__['school']) #打印oldboy
print(Student.__dict__['choose']) #打印内存地址
Student.choose(123) #类调用加参数

Second, the difference between the two

---> class

  1. And get property methods of the class
    • By 类.__dict__()acquiring
  2. Class to call the properties and methods
    • By dict[' ']acquiring (but more complex)
    • By 类名.属性和函数acquiring (parameter passing) (for the class is, study / choose a function only)

Note: Print is the result function.

---> Object

  1. Gets the object properties and methods
    • By 对象.__dict__acquiring (empty dictionary)
  2. Object to call the properties and methods
    • By 对象.属性/方法acquiring (without parameter passing) (for the object, study / choose method)

Note: Print is the result of method.

  1. The object's own properties and methods
stu1.Student()
stu1.name = 'nick'
stu1.school = 'oldgirl'  #若类要修改Student.school = 'xxx'
print(stu1.__dict__)

#{'name': nick, 'school': oldgirl}
  1. Adding attributes to object

    • stu1.Student()
      stu1.name = 'nick'

    • __init__method

      class Student:
          School = 'oldboy'
          def __init__(self,name):
              self.name = name

      Note: when an object instance of the object is generated, the first parameter is a parameter passing

  2. Property search order
    • Object ---> --- class> not on the error, for example print (stu1.oldboy)
  3. Produced objects

    stu1.Student('nick')
    stu2.Student('jason')
  4. Binding approach

    • The method defined within the class
    • If the class is called, it is an ordinary function; if the object is called binding method is called object, the first parameter does not need to pass, automatic incoming

Everything Objects

#类实例化产生对象3
Q = [1, 2, 3]
list.append(Q,5) #list是类,传两个参数

Q = list([1, 2, 3])
Q.append(5) #Q是具体对象,传一个参数

People and dogs shootout

#人狗大战
#定义一个狗类
class Dog:
    type_dog='金毛'
    def __init__(self, name, aggr, hp=100):
        self.name=name
        self.aggr = aggr
        self.hp=hp
    def bite(self, target):
        #当期狗的攻击力:self.aggr
        #人的血量:target.hp
        target.hp-=self.aggr
        print('''
        狗的品种:%s
        %s狗咬了一下%s人,
        人掉血:%s
        人的血量剩余:%s
        '''%(self.type_dog,self.name,target.name, self.aggr,target.hp))
#人类
class Human:
    def __init__(self,name,aggr,hp=100):
        self.name=name
        self.aggr=aggr
        self.hp=hp
    def bite(self,target):
        target.hp-=self.aggr
        print('''
        %s人咬了一下%s狗,
        狗掉血:%s
        狗的血量剩余:%s
        '''%(self.name,target.name,self.aggr,target.hp))


#实例化产生狗对象
# dog1=Dog('旺财',10)
dog1=Dog('旺旺财',10,200)
nick=Human('nick',50)
dog1.bite(nick)
print(nick.hp)
dog1.bite(nick)
print(nick.hp)
nick.bite(dog1)

Guess you like

Origin www.cnblogs.com/fxyadela/p/11415379.html