举例

面向对象的合理解释就是:我是人这个类,对象化以后我就是一个个体OLIVER

对象化就是在人这个大类中,将某个人指名道姓,具体到某个人

下面是一个具体的实例一:

#!/usr/bin/python
#-*- coding:UTF-8 -*-
class CompanyMember:
#总人数,这个类的变量
MemberCount = 29
#__ini__方法在类的对象被创建时候执行
def __init__(self,name):
self.name = name
CompanyMember.MemberCount +=1
print ("公司来了一个新成员:%s" % self.name)
print ("现有成员%d人" % CompanyMember.MemberCount)
#新建一个方法,新员工介绍
def introduce(self):
print("我的名字叫做:%s" % self.name) #领导继承公司成员类
class Leader(CompanyMember):
def __init__(self,name,salary):
CompanyMember.__init__(self,name)
self.salary = salary
self.name=name
def introduce(self):
CompanyMember.introduce(self)
print("%s是领导:我的工资是%d" % (self.name,self.salary)) #部门经理继承公司成员类
class Manager(CompanyMember):
def __init__(self,name,Location):
CompanyMember.__init__(self,name)
self.Location=Location
self.name=name
def introduce(self):
CompanyMember.introduce(self)
print("%s是%s部门的部门经理" % (self.name,self.Location))
# 创建一个新员工对象
CompanyMember1=CompanyMember("OLIVER")
CompanyMember1.introduce()
print("--------------------------")
#创建一个领导对象
Leader1=Leader("jack",360000)
Leader1.introduce()
print("--------------------------")
#创建一个部门经理对象
Manager1=Manager("john","Sales")
Manager1.introduce()

实例二:

#!/usr/bin/python
#-*- coding:UTF-8 -*- class Employee:
'所有员工的基类'
empCount=0
#定义方法
def __init__(self,name,salary):
self.name = name
self.salary = salary
Employee.empCount+=1 def displayCount(self):
print("total Employee %d" %Employee.empCount)
def displayEmployee(self):
print("Name :",self.name,",Salary:",self.salary)
"#创建 Employee 类的第一个对象"
emp1=Employee("OLIVER","") "创建 Employee的第二个对象"
emp2=Employee("QIN",'') emp1.displayEmployee()
emp2.displayEmployee() print ("Total Employee %d" % Employee.empCount)

总结

将东西根据属性归类 ( 将object归为class )

方法是一种属性,表示动作

用继承来说明父类-子类关系。子类自动具有父类的所有属性。

self代表了根据类定义而创建的对象。

建立对一个对象: 对象名 = 类名()

引用对象的属性: object.attribute

最新文章

  1. 《徐徐道来话Java》:PriorityQueue和最小堆
  2. (转)windows系统下Python环境的搭建
  3. 转载:《TypeScript 中文入门教程》 2、枚举
  4. How to use aws CloudFront for CDN
  5. wmic 命令的一个汇总,功能很强大
  6. Emule Xtreme Kid eD2K 设置
  7. Nginx reopen reload作用及工作过程
  8. RDMA的ibv_post_send() 函数
  9. ASP.NET上传大文件的问题
  10. java.sql.SQLException: null, message from server: “Host ‘xxx’ is not allowed to connect
  11. 怎样搭建本地svn服务器环境-轻松掌握版本管理
  12. [转]fedora启动telnet服务
  13. @Component @Repository @Service @Controller
  14. The calculation of GPA
  15. Linux---More命令 初级实现
  16. LINUX 内核调试基础+编程基础
  17. Anaconda更新源
  18. k8s Kubernetes v1.10
  19. 学JAVA第十天,一维数组及二维数组的使用。
  20. Spark Standalone spark-env.sh

热门文章

  1. POJ - 1835 宇航员(模拟题)
  2. [xsy2123]毛毛虫
  3. 【动态规划】mr359-最大公约数之和
  4. nginx和php-fpm的用户权限
  5. 将int型数值拆分成4字节
  6. 邮件发送javamail
  7. [WikiOI "天梯"1281] Xn数列
  8. 基于tiny4412的Linux内核移植 -- MMA7660驱动移植(九)
  9. ImageWriter制作ubuntu的U盘启动盘
  10. matlab中cell array的理解