Python Class
时间:2011-05-04 来源:qiang.xu
'''
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(1, 1);
c2 = Complex(1, 1);
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';
c = 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");
s = 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");
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(1, 1);
c2 = Complex(1, 1);
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';
c = 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");
s = 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");
相关阅读 更多 +