Python Class

'''
Created on 2011-4-30

@author: xuqiang
'''

class Student:
    
# attribute 
    i = 12345;
    
    
# member function
    def func(self):
        
print('in class member function : func');
    
    
# init
    def __init__(self):
        
print("in __init__ function");
        
        
# class instance
stu = Student();
stu.func();

class Complex :
    
# init 
    def __init__(self, real, img):
        self.real 
= real;
        self.img 
= img;
    
    
    
# operator
    def add(self, other):
        
return Complex(self.real + other.real, 
                       self.img 
+ other.img);

c1 
= Complex(11);
c2 
= Complex(11);
c3 
= c1.add(c2);
print(c3.img);

# empty class object
class Employee :
    
pass

class MyClass:
    
"""  a simple class example """
    i 
= 123456;
    
    
def f(self):
        
return 'hello world';


= MyClass();
print(c.f());


class Base:
    
"""  base class """
    
def __init__(self):
        
print("in base init.");
    
    
def override_func(self):
        
print("in base : overrride_func");
    
class Sub(Base):
    
def __init__(self):
        
print("in sub init");

    
def override_func(self):
        
print("in sub : override_func");

= Sub();
s.override_func();

if isinstance(s, Sub):
    
print("s is an instance of class Sub");

if issubclass(Sub, Base):
    
print("Sub is a sub class of class Base");
    
posted @ 2011-05-04 19:22  qiang.xu  阅读(534)  评论(0编辑  收藏  举报