鱼C论坛

 找回密码
 立即注册
查看: 5175|回复: 2

[已解决]Python的课程里好像没有讲抽象类啊

[复制链接]
发表于 2017-4-5 21:19:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Python的课程里好像没有讲抽象类啊
最佳答案
2017-4-5 21:43:55
抽象类是什么,表示不知道,Python没有抽象类
  1. # -*- coding: utf-8 -*-
  2. # Python抽象类的基本模板

  3. from abc import ABCMeta, abstractmethod

  4. # 定义抽象类,继承object
  5. class People(object):
  6.     # 定义为抽象类
  7.     __metaclass__ = ABCMeta

  8.     # 公有变量
  9.     name = 'marco'
  10.     # 私有变量,双下划线开头
  11.     __age = 20

  12.     def __init__(self, name = 'Mike'):
  13.         self._name = name
  14.         self.__age = 20

  15.     # 如果不设置setter,则属性为只读
  16.     # _name为可读写,而_age为只读
  17.     @property
  18.     def Name(self):
  19.         print 'get name...'
  20.         return self._name

  21.     @Name.setter
  22.     def Name(self, name):
  23.         print 'set name...'
  24.         self._name = name

  25.     @property
  26.     def Age(self):
  27.         print 'get age...'
  28.         return self.__age

  29.     # 定义抽象方法,子类继承时需实现
  30.     @abstractmethod
  31.     def sayHello(self):
  32.         pass



  33. # 定义实现类, 继承和实现抽象类和其方法
  34. class Student(People):
  35.     def __init__(self, name = 'Leo'):
  36.         self.name = name
  37.         self.age = 18

  38.     def sayHello(self):
  39.         print 'Student say hello to you'

  40. s = Student()
  41. s.sayHello()
复制代码

只能这样实现
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-4-5 21:43:55 | 显示全部楼层    本楼为最佳答案   
抽象类是什么,表示不知道,Python没有抽象类
  1. # -*- coding: utf-8 -*-
  2. # Python抽象类的基本模板

  3. from abc import ABCMeta, abstractmethod

  4. # 定义抽象类,继承object
  5. class People(object):
  6.     # 定义为抽象类
  7.     __metaclass__ = ABCMeta

  8.     # 公有变量
  9.     name = 'marco'
  10.     # 私有变量,双下划线开头
  11.     __age = 20

  12.     def __init__(self, name = 'Mike'):
  13.         self._name = name
  14.         self.__age = 20

  15.     # 如果不设置setter,则属性为只读
  16.     # _name为可读写,而_age为只读
  17.     @property
  18.     def Name(self):
  19.         print 'get name...'
  20.         return self._name

  21.     @Name.setter
  22.     def Name(self, name):
  23.         print 'set name...'
  24.         self._name = name

  25.     @property
  26.     def Age(self):
  27.         print 'get age...'
  28.         return self.__age

  29.     # 定义抽象方法,子类继承时需实现
  30.     @abstractmethod
  31.     def sayHello(self):
  32.         pass



  33. # 定义实现类, 继承和实现抽象类和其方法
  34. class Student(People):
  35.     def __init__(self, name = 'Leo'):
  36.         self.name = name
  37.         self.age = 18

  38.     def sayHello(self):
  39.         print 'Student say hello to you'

  40. s = Student()
  41. s.sayHello()
复制代码

只能这样实现
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-6 11:32:19 | 显示全部楼层
python没有C语言中的虚类 但是每个方法好像就是C语言中的虚函数 继承的时候是可以重新定义的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-17 10:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表