一 最简单的类
python语法31[类]>>> class c(object): pass
python语法31[类]
python语法31[类]
>>> x = c()
python语法31[类]
>>> issubclass(c,object)
python语法31[类]True
python语法31[类]
>>> type(10)
python语法31[类]
<class 'int'>
python语法31[类]
>>> issubclass(int,object)
python语法31[类]True
python语法31[类]
>>> type('aaa')
python语法31[类]
<class 'str'>
python语法31[类]
>>> issubclass(str,object)
python语法31[类]True
python语法31[类]
>>>
注意:
python与C#一样,为纯面向对象语言,所有的类都有共同的基类object。
python的内置类型都有对应的class对应,例如整形对应的类为int,字符串对应的类为str等。
类的定义使用关键字class,类名后面()为基类的名字。

二 简单类

>>>class MyClass:
      """
This is the simple class for testing"""
python语法31[类]
    i=100
python语法31[类]    
def PrintI():
python语法31[类]        
print(i)
python语法31[类]    
def __init__(self):
python语法31[类]
        self.x=10
python语法31[类]
        self.y=20
python语法31[类]
        self.__total=self.x+self.y
python语法31[类]        
print("constructor!")
python语法31[类]    
def __del__(self):
python语法31[类]        
print("deconstructor!")
python语法31[类]    
def __PrintTotal(self):
python语法31[类]        
print(self.__total)
python语法31[类]    
def PrintSelf(self):
python语法31[类]        
print("print x ,y and total")
python语法31[类]        
print(self.x)
python语法31[类]        
print(self.y)
python语法31[类]
        self.__PrintTotal()
python语法31[类]
python语法31[类]        
python语法31[类]
>>> print(MyClass.__name__)
python语法31[类]
MyClass
python语法31[类]
>>> print(MyClass.__doc__)
python语法31[类]
This is the simple class for testingpython语法31[类]
python语法31[类]
>>> myC=MyClass()
python语法31[类]
constructor!
python语法31[类]
>>> myC.i
python语法31[类]
100
python语法31[类]
>>> MyClass.i
python语法31[类]
100
python语法31[类]
>>> myC.x
python语法31[类]
10
python语法31[类]
>>> myC.y
python语法31[类]
20
python语法31[类]
>>> myC.PrintSelf()
python语法31[类]
print x ,y and total
python语法31[类]
10
python语法31[类]
20
python语法31[类]
30
python语法31[类]
>>> myC._MyClass__total
python语法31[类]
30
python语法31[类]
>>> myC._MyClass__PrintTotal()
python语法31[类]
30
python语法31[类]
>>> myC.z=30
python语法31[类]
>>> myC.z
python语法31[类]
30
python语法31[类]
>>> del myC.z
python语法31[类]
>>> del myC
python语法31[类]
deconstructor!
python语法31[类]
>>>

 注意:
一些默认的属性__name__表示类的名字,__doc__表示类的说明字符串,__dict__类的整个dictionary。
__init__(self)和__del__为类的默认的构造和析构函数。
myC=MyClass()用来定义实例。
i为MyClass的静态变量,可以使用MyClass.i 或myC.i来访问。
x,y为类MyClass的成员变量。
PrintSelf()为类MyClass的成员方法。
__total和__PrintTotal()为类MyClass的私有成员和方法,但是这个只是一个约定,可以使用myC._MyClass__total和myC._MyClass__PrintTotal()来访问。
z为myC的实例的成员,不属于类MyClass。
使用del来删除类对象或实例的成员变量。


三 类和对象的属性的判定和函数的修改

class Class:
    answer 
= 42
    
def __init__(self):
        self.x 
= 10
    
def method(self):
        
print'Hey a method')
    
print(hasattr(Class, 'answer'))
#True
print(hasattr(Class, 'question'))
#False
print(hasattr(Class(), 'x'))
#True
print(hasattr(Class, 'method'))
#True

print(getattr(Class, 'answer'))
#42
print(getattr(Class, 'question''What is six times nine?'))
#'What is six times nine?'
print(getattr(Class(), 'x'))
#10
getattr(Class(),'method')()
#'Hey a method'

class MyClass:
   
def method(self):
        
print'Hey a method')

instance 
= MyClass()
instance.method()
#'Hey a method'

def new_method(self):
    
print'New method wins!')


MyClass.method 
= new_method
instance.method()
#'New method wins!'

del MyClass.method
print(hasattr(MyClass, 'method'))
#False
#
instance.method()

instance.y 
= 20
print(instance.y)
del instance.y


可以使用hasattr来判断类和实例的属性是否存在,如果存在可以使用getattr来获得值,此时的属性包含变量和方法。

类和实例的属性可以在使用的过程中进行增删改,此时的属性包含变量和方法。

 

四 类的静态和类方法

class MyClass2:
    @classmethod
    
def a_class_method(cls):
        
print ('I was called from class %s' % cls)

    @staticmethod
    
def a_static_method():
        
print ('I have no idea where I was called from')
        
    
def a_static_method2():
        
print('i am just called by the class')


instance2 
= MyClass2()

MyClass2.a_class_method()
instance2.a_class_method()
# both print 'I was called from class __main__.MyClass2'

MyClass2.a_static_method()
instance2.a_static_method()
# both print 'I have no idea where I was called from'

MyClass2.a_static_method2()
#'i am just called by the class'
#
instance2.a_static_method2() # throw exception

类方法和使用staticmethod修饰的静态方法,调用方法相同,均可以使用类或对象调用。

对于没有staticmethod修饰的静态方法,只能使用类来调用。

 

五 类的继承

python语法31[类]>>> class Employee:
python语法31[类]    companyname
="microsoft"
python语法31[类]    
def printCompany():
python语法31[类]        
print(companyname)
python语法31[类]    
def __init__(self,name,salary):
python语法31[类]        self.name
=name
python语法31[类]        self.salary
=salary
python语法31[类]        
print("Constructor Employee")
python语法31[类]    
def __del__(self):
python语法31[类]        
print("DeConstructor Employee")
python语法31[类]    
def PrintSelf(self):
python语法31[类]        
print(self.name)
python语法31[类]        
print(self.salary)
python语法31[类]
python语法31[类]
>>> class Developer(Employee):
python语法31[类]    
def __init__(self,name,salary,area):
python语法31[类]        Employee.
__init__(self,name,salary)
python语法31[类]        self.area
=area
python语法31[类]        
print("Constructor Developer")
python语法31[类]    
def __del__(self):
python语法31[类]        Employee.
__del__(self)
python语法31[类]        
print("DeConstructor Developer ")
python语法31[类]    
def PrintSelf(self):
python语法31[类]        Employee.PrintSelf(self)
python语法31[类]        
print(self.area)
python语法31[类]
python语法31[类]
>>> d=Developer("bill",10000,"c")
python语法31[类]Constructor Employee
python语法31[类]Constructor Developer
python语法31[类]
>>> d.PrintSelf()
python语法31[类]bill
python语法31[类]
10000
python语法31[类]c
python语法31[类]
>>> del d
python语法31[类]DeConstructor Employee
python语法31[类]DeConstructor Developer 
python语法31[类]
>>>

注意:继承还有多继承和C++,C#的相似。

相关文章:

  • 2021-11-22
  • 2022-02-23
  • 2022-02-21
  • 2022-12-23
  • 2021-08-22
  • 2021-07-25
  • 2021-10-15
  • 2022-02-18
猜你喜欢
  • 2021-12-02
  • 2022-02-05
  • 2021-08-13
  • 2021-08-31
  • 2021-09-04
  • 2022-01-06
  • 2022-03-08
相关资源
相似解决方案