zoukankan      html  css  js  c++  java
  • python2学习------基础语法3(类、类的继承、类成员函数、防御式编程)

    1、类的定义以及实例化

    # 类定义
    class p:
        """ this is a basic class """
    basicInfo={"name":"lxh","nation":"China"}; # 类成员变量
        def __init__(self): # 类成员函数需要传入self关键字
            """ this is a init function of basic class """
            print "this is a init function ... ";
        def sub(self,a,b): # 类成员函数需要传入self关键字
            """ this is a common function of basic class """
            return a-b;
    # 类实例化
    a=p();
    # 调用类方法
    print a.sub(40,5); # -1

    2、类的继承

    # 继承类
    class m(p): # 参数为需要被继承的类的名称
        """ this is a sub-class of basic class """
        def __init__(self):# 专用函数(函数名首尾带有双下划线)
            print "sub class init function ... ";
            self.__test();        
        def loopOutputFun(self,n):
            if n<0:
                print n,"is not a postive integer "
                retrun;
            for i in range(1,n,3):
                print i;
            else:
                print "loop output over ... ";
        def __test(self):# 私有函数(仅函数首部带有双下划綫)
            print "类的私有函数,不能被类的实例直接调用,只能被专有函数调用(需要带self关键字)";
        def __my__(self):# 专用函数(函数名首尾带有双下划线)
            self.__test();    
        def dev__(self,a,b):
            """演示异常"""
            try:
                return a/b;
            except Exception,e:
                print e.message;
        def __passTest__(seLf):
            """函数逻辑暂未规划好时,采用pass关键字进行处理"""
            pass;
    # 子类实例化
    b=m();
    # 调用子类方法
    print b.sub(100, 55); 
    b.loopOutputFun(10);
    b.__my__();
    print b.dev__(10,2);
    b.__passTest__();

    3、

  • 相关阅读:
    -bash: fork: Cannot allocate memory 问题的处理
    Docker top 命令
    docker常见问题修复方法
    The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
    What's the difference between encoding and charset?
    hexcode of é î Latin-1 Supplement
    炉石Advanced rulebook
    炉石bug反馈
    Sidecar pattern
    SQL JOIN
  • 原文地址:https://www.cnblogs.com/lvlin241/p/9443019.html
Copyright © 2011-2022 走看看