Python class 与c++ 之类的区别

1.类里每个方法第一个参数都是self

class Foo:
  empCount
  def __init__(self, name, age):
    self.name = name
    self.age = age
  
  def detail(self):
    print(self.name)
    print(self.age)

2. 构造函数的名字是 __init__

3. self.xx 就是私有变量,写在最上面的就变成了所有实例之间共享!

4.继承写法

class Child(Parent): # 定义子类
def __init__(self):
print "调用子类构造方法"

def childMethod(self):
print '调用子类方法'

5. 基础重载方法

 

class Vector:
   def __init__(self, a, b):
      self.a = a
      self.b = b
 
   def __str__(self):
      return 'Vector (%d, %d)' % (self.a, self.b)
   
   def __add__(self,other):
      return Vector(self.a + other.a, self.b + other.b)

   def __del__( self ):
      pass

http://www.waitingfy.com/archives/3269

3269

Leave a Reply

Name and Email Address are required fields.
Your email will not be published or shared with third parties.