python - Object Oriented Programming (class, object)

one type

Class is used to describe a collection of objects having the same properties and methods.

It defines the properties and methods of each object in the collection jointly owned.

Class is an independent unit, which has a class name, which includes an internal member variable and methods were described for the properties and behavior of an object.

Class definition syntax:

class ClassName:

  Statement 1

  Statement 2

  .......  

  Statement N

Class name naming convention is the first letter of each word capitalized, the rest lowercase

 

Second, the object

Objects are instances of classes, creating an object is known as instantiating the class.

Instantiate an object syntax:

    obj = ClassName()

In python, the isinstance built-in method may be used () to test whether an object instance of a class:

    isinstance(obj,ClassName)

Each object comprising: an identification code, object type, properties, and methods.

Using the built-in function id (obj), the identification code may return the object.

 

--------------------------- # 
# Object-Oriented Programming
# ----------------- ----------
# define
# Python 2.x
# class FirstClass (Object):
# Python 3.x
class FirstClass:
"" "a simple first class." "" # class help You can view .__ doc__ by ClassName
name = "FirstClass" # class properties (class variables)
Intro = "This iS My First class!"

DEF First (Self):
members return self.intro # function inside the class

# View class the annotation information
Print (FirstClass .__ doc__ displays a)
# class is instantiated to produce an object
first_one = FirstClass ()
# get the object identifier
print ( "object identification number:", ID (first_one))
# acquired object type
print ( "object type: ", of the type (first_one))

# access class properties - variable
print (" class attribute access FirstClass.name:", FirstClass.name)
print ( "class attribute access first_one.name:", first_one.name)

# call member methods
print ( "call the method Output:", first_one.first ())

# isinstance (): test whether an object is a class examples of
print (isinstance (first_one, FirstClass) ) # returns bool value

 

Guess you like

Origin www.cnblogs.com/Teachertao/p/11221291.html