程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Basics - classes

編輯:Python

Catalog

1. Create and use classes

2. Use classes and instances

3. Inherit

4. Import class


1. Create and use classes

1.1 Create a class

class person():
def __init__(self,name,age):
self.name = name
self.age = age
def sit(self):
print(self.name.title() + "is now sitting")
def sleep(self):
print(self.name.title() + "is now sleeping!")

Method __init__() It's a special method , Whenever a new instance is created from a class ,Python Will run it automatically . In the name of the method , There are two underscores at the beginning and the end , It's a convention , To avoid Python The default method has a name conflict with the normal method .

We're going to approach __init__() It is defined to contain three parameters :self 、name and age . In the definition of this method , Shape parameter self essential ,__init__ The first parameter of the method is always self, Must precede other formal parameters . Why do I have to package With formal parameters self Well ? because Python Call this __init__() Method to create Dog When an instance , Will be automatically passed in Actual parameters self . Each method call associated with a class automatically passes arguments self , It is a reference to the instance itself , Gives instances access to properties and methods in the class .       

self.name = name、self.age = age, Variables that can be accessed through instances are called attribute . The functions in the class are called Method ,sit()、roll_over() and _init_( )-- Refer to use and not use init() Links to methods :python And objects (3)_weixin_30687811 The blog of -CSDN Blog

1.2 Create an instance based on the class

class person():
def __init__(self,name,age):
self.name = name
self.age = age
def sit(self):
print(self.name.title() + "is now sitting")
def sleep(self):
print(self.name.title() + "is now sleeping!")
my_friend = person('kk', 6)
print("My friend's name is " + my_friend.name.title() + ".")
print("My friend is " + str(my_friend.age) + " years old.")
my_friend.sit()
my_friend.sleep()

And 1.1 combination , The output result is :

Access properties , have access to Period notation , similar my_friend visit name, Use my_friend.name

Calling method , After the class creates an instance , You can call it using period notation dog Any method defined in

Create multiple instances , You can add something like... In the class my_friend Example

2. Use classes and instances

2.1 Modify the value of the property

2.1.1 Directly modifying

2.1.2 Method to modify the value of the property

2.1.3 Method increments the value of the property

3. Inherit

When writing classes , You don't have to start from scratch . You can use Inherit , When one class inherits another , It automatically gets all the properties and methods of another class ; The original class is called the parent class , The new class is called a subclass . A subclass inherits all the properties and methods of its parent class , You can also define your own properties and methods .

3.1 Subclass method _init_()

3.2 Define properties and methods for subclasses

3.3 Overrides the method of the parent class

4. Import class

4.1 Import a single class

First create a module that contains only one class , Creating another .py file , And then use Import Statement import

from xxx import A

4.2 Import multiple classes from a module

from xxx import A, B

4.3 Import the entire module

keyword :import x

4.4 Import all the classes in the module

from xxx import *

The learning content of this article refers to 《Python Programming : From introduction to practice 》


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved