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(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");
如果您觉得不错,欢迎扫码支持下。
作者:许强1. 本博客中的文章均是个人在学习和项目开发中总结。其中难免存在不足之处 ,欢迎留言指正。 2. 本文版权归作者和博客园共有,转载时,请保留本文链接。