the python object-oriented classes, objects,

class

Category one is abstraction

A small friends

Is a collection of columns has the same features and the same behavior of the object

 

Objects

Is a thing to do this, have their own characteristics and rows

A friend of small: Xiao Ming

 

 

The relationship between classes and objects

Class contains a series of objects

The object belongs to a class

 

In reality the object first, and then have class

 

The program is, first class, in order to have the object

 

While in the program is the first class to an object, we must first tell the machine count usurp what kind of object features what behavior

 

Summary: When using object-oriented programming: The first step is to think about what kind of object you want, what kind of an object with characteristics and behavior, which summed up the type of information required in accordance with

 

 

 

 

Creating classes and objects

Although the first class has only an object, but we must first think about objects

 

Defined syntax:

class name of the class:

# Class code

# Describe attributes and skills

# Description attribute with variable

# Describe the behavior with variables

# Written specification class names:

1: See name EENOW

2: large hump nomenclature

# Large hump: capitalize the first letter

# Small hump: small hump lowercase

 

class the Person: 

Pass 

# create the object 

the p- = the Person ()

 

 

 

 

 

Properties wording:

Properties can be written in class,

The attribute class, is common to all objects

 

It can also be written in an object

The number of objects, each object unique (different)

 

If the presence of the same class and object attributes, will first access the object, if not it will access values ​​(with access class)

 

Exercise: teacher describes a class that contains a public property, a unique property

 

 

 

 

A class definition, the class code which will be executed

(Execute it once?)

 

Deletions attribute change search

Increase the property:

The variable name of the object. Attribute name = attribute value

 

Delete Attribute:

Variable name del objects. Attribute name

 

modify:

Object = new attribute value

 

View object properties:

print (Object. dict )

# See the objects (own) the property is not public property access #

 

View object classes:

print (Object. class )

 

View properties of the class

print (class. dict )

 

__init__ method:

#  Which can only be init can not be changed, and can not have a return value

# When you create an object of execution

# And put their (target) pass in the

 

 

Initialization method is called, is essentially a function

Features 1: When the object is instantiated, the init method of automatically

Feature 2: automatically object as the first argument, the parameter name is self, self can be another name right, but not recommended to change

 

Features: user to assign an initial value of an object

 

Exercise: create a class with several properties, set the property to him by the initialization method

 

 

 

 

Guess you like

Origin www.cnblogs.com/pscly/p/11240879.html