zoukankan      html  css  js  c++  java
  • 创建子类

    13.10.1  创建子类 
    
    创建子类的语法看起来与普通(新式)类没有区别,一个类名,后跟一个或多个需要从其中派生的父类:
    
    class SubClassName (ParentClass1[, ParentClass2, ...]):
     'optional class documentation string'  
      class_suite
    
    如果你的类没有从任何祖先类派出,可以使用object作为父类的名字。  经典类的声明唯一不同之处
    
    在于其没有从祖先类派生  
    
    至此,我们已经看到了一些类和子类的例子,下年还有一个简单的例子:
    
    class Parent(object): # define parent class 定义父类
      def parentMethod(self):
        print 'calling parent method'
    	
    class Child(Parent): # define child class 定义子类
       def childMethod(self):      
         print 'calling child method'	
    	
    	
    
    # -*- coding:utf-8 -*-
    # !/usr/bin/python
    class Parent(object): # define parent class 定义父类
      def parentMethod(self):
        print 'calling parent method'
    
    class Child(Parent): # define child class 定义子类
      def childMethod(self):
        print 'calling child method'
    p=Parent() # instance of parent 父类的实例
    print p.parentMethod()
    
    c = Child() # instance of child 子类的实例
    print c.childMethod()  # child calls its method 子类调用它的方法
    print c.parentMethod() # calls parent's method 调用父类的方法
    
    
    
  • 相关阅读:
    File类
    Java运算符
    JAVA语法
    数据库-子查询
    爬取笔趣阁_完本书籍
    爬取动物图片源码
    爬取电影天堂上最新电影的下载链接的源码
    pyinstaller的安装、使用、出错解决办法
    Emmet插件使用方法总结
    Markdown基本语法
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349143.html
Copyright © 2011-2022 走看看