From entry to imprisonment-object-oriented (1)

Facial process programming:

Use algorithms or logic to solve problems-logic

Functional programming:

Use functions to solve problems, if you don't have one, define your own-function

Object-Oriented Programming:

Solve the problem with objects that have the ability to solve the problem, create a class and object without it

---- Class ----

A class is a collection of
objects with the same functions and properties. Objects are the use of classes.

Defined class—Use code to describe clearly what the defined class is a collection of objects with the same attributes

Syntax:
class class name:
class description document
class content

Explanation:
class—keyword; fixed wording.
Class name—name yourself.
Requirement: an identifier, not a keyword.
Specification: capitalize the first letter and name it in camel case. See the meaning of the name, do not use the system function name, type name, module name,
class Documentation—essentially using three double quotation marks at the beginning and end of comments

The content of the class—mainly includes methods (object methods, class methods, and static methods) and properties (object properties and object properties)
methods: the essence is the function defined in the class, which is used to describe the function of the class. The
attribute: the essence is Variables that store data in the class are used to describe the properties of the class

class Person:
    """
    描述类的内容和使用说明
    """
    def sleep(self):
        print('人在睡觉')

    def eat(self):
        print('人在吃')

# 定义对象
'''
类名()
'''
p1=Person()
p2=Person()
print(p1)   # <__main__.Person object at 0x0000000001E22A48>
print(p2)  # <__main__.Person object at 0x0000000001E22A88>

Object method:

Definition: directly define the function in the class.
Call: call by means of "object.method name".
Features: self-contained parameter self, when calling method with object, parameter self does not need to be passed. The
system will automatically pass the current object to self (self: whoever calls it will point to whom)

Define the class:

class Dog:
    def eat(self,x='啃骨头'):
        print(self)  # <__main__.Dog object at 0x00000000021B27C8>
        print(f'狗在{x}')


#  创建一个Dog类的对象保存在d1中
d1=Dog()
print(d1)    # <__main__.Dog object at 0x00000000021B27C8>
# 通过对象来调用对象方法
d1.eat()     # 狗在啃骨头
d1.eat('喝水') # 狗在喝水

Construction method (function)

The function name has the same name as the class, and the function specifically used to create an object is the constructor (method). When
Python defines the class, the system will automatically create the constructor corresponding to this class. When
Python calls the constructor to create an object, three operations are completed internally:
a. Call the __new__ method to create the object
b. Use the created object to call the __init__ method to initialize the object
c. Return the object after initialization

__init__ method

Magic method: The method whose name in the class starts with __ and ends with __ is called magic method. The
magic method does not need to be called actively, the system will automatically call
init under certain circumstances : the system will automatically call init every time an object is created through the class this method is
time to create the object constructor has no arguments there are a few to see the corresponding
init method other than self there are no additional parameters have a few
if you need to add the class init method is mainly to ensure that the method name is __init__
nature and method is an object Method is fine

#  init的自动调用
class Person:
    def __init__(self):
        print('init方法被调用')


p1 = Person()  # init方法被调用
p2 = Person()  # init方法被调用

Attributes

Class attribute-also called the field of the class
Definition: Directly define the variable outside the function in the class
Use: Use in the form of'class. Attribute' When in
use: Need a property whose value will not be different because of different objects

Object attribute
Definition: Defined in the __init__ method in the form of'self.attribute name=value'
Use: Used in the form of'object.Attribute ' When
used: Need an attribute whose value will be different because of the object

class Person:
    num = 10  # 类属性

    def func1(self):
        pass

    a = 'abc'  # 类属性

    def __init__(self, name, age):  # 对象属性
        self.name1 = '小明'
        self.age1 = 18
        self.name = name
        self.age = age


print(Person.num, Person.a)  # 10 abc
p1 = Person('小王', 0)
print(p1.name1, p1.age1)  # 小明 18
print(p1.name, p1.age)  # 小王 0


# 练习:定义一个点类,有属性:x坐标、y坐标,方法:打印点的信息 x:?,y:?
# 要求创建点对象的时候可以给坐标赋值,如果不赋值,x:0,y:0
class dot:
    def __init__(self, x=0, y=0):   # 对象属性
        self.x = x
        self.y = y

    def print_info(self):     # 打印坐标方法
        print(f'x:{self.x},y:{self.y}')  # 这是重点

    def distance(self, self2):  # 计算坐标距离方法
        return ((self.x - self2.x) ** 2 + (self.y - self2.y) ** 2) ** 0.5


d1 = dot()
d2 = dot(4, 3)
print(d2.distance(d1))

Guess you like

Origin blog.csdn.net/weixin_44628421/article/details/109207751