Python学习之面向对象基础
python的面向对象和以前学的c++,Java都是一般,大同小异,面向对象基础先谈谈类的构造,编写,属性和方法的可见性等等
1.定义类,创建和使用对象
#定义类
class Student(object):
#类student的初始化方法
def __init__(self,name,age):
self._name=name
self._age=age
def study(self,cname):
print("%s正在学习:%s"%(self._name,cname))
def movie(self):
if self._age <= 18:
print("%s只能看熊大熊二!"%self._name)
else:
print("%s正在看爱情动作片!"%self._name)
#创建和使用对象
def main():
student1=Student('wbb',21)
student1.study('python程序设计')
student1.movie()
if __name__=='__main__':
main()

2.属性和方法的可见性
Python在这个地方和其他语言还是有些区别的,其他语言可以设置属性为私有的(private)或者受保护的(protected),来限制对属性和方法的直接访问,但是python只有公有的和私有的,而且python中私有的只是一种形式,外界仍然可以访问到。所以,建议在python中将属性和方法的可见性定为公有的,但是你可以在属性名前加个单下划线,但这仅仅只是一种暗示,提醒你访问时要小心了,并不是python的语法,毕竟你得为自己的行为负责
私有属性和方法外部不能直接访问
class Test:
#属性名以双下划线开头表示私有的
def __init__(self,foo):
self.__foo=foo
#私有方法
def __bar(self):
print(self.__foo)
print('__bar')
def main():
test=Test('hello')
#AttributeError: 'Test' object has no attribute '__bar'
test.__bar()
print(test.__foo) if __name__=='__main__':
main()
python的私有并不是完全不能访问,其实也可以访问,所有不建议设为私有的,可以以单下划线开头,表示一种暗示
class Test:
#属性名以双下划线开头表示私有的
def __init__(self,foo):
self.__foo=foo
#私有方法
def __bar(self):
print(self.__foo)
print('__bar')
def main():
test=Test('hello')
test._Test__bar()
print(test._Test__foo) if __name__=='__main__':
main()
运行结果如下图:

Python学习之面向对象基础的更多相关文章
- Python学习笔记——面向对象基础
1.类和实例 1.1类的定义 类的定义使用class关键字,其后紧跟类名(通常大写开头),紧接着是(object),object是该类继承的类名,没有就继承object类. 实例化时就是类名+(),有 ...
- Python学习课程零基础学Python
python学习课程,零基础Python初学者应该怎么去学习Python语言编程?python学习路线这里了解一下吧.想python学习课程?学习路线网免费下载海量python教程,上班族也能在家自学 ...
- 8.python笔记之面向对象基础
title: 8.Python笔记之面向对象基础 date: 2016-02-21 15:10:35 tags: Python categories: Python --- 面向对象思维导图 (来自1 ...
- python学习日记(基础数据类型及其方法01)
数字 int 主要是用于计算的,常用的方法有一种 #既十进制数值用二进制表示时,最少使用的位数i = 3#3的ASCII为:0000 0011,即两位 s = i.bit_length() print ...
- Python学习之==>面向对象编程(二)
一.类的特殊成员 我们在Python学习之==>面向对象编程(一)中已经介绍过了构造方法和析构方法,构造方法是在实例化时自动执行的方法,而析构方法是在实例被销毁的时候被执行,Python类成员中 ...
- Python学习day16-模块基础
<!doctype html>day16 - 博客 figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { ...
- Python学习day12-函数基础(2)
<!doctype html>day12博客 figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { pos ...
- Python学习day11-函数基础(1)
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- Python学习笔记之基础篇(-)python介绍与安装
Python学习笔记之基础篇(-)初识python Python的理念:崇尚优美.清晰.简单,是一个优秀并广泛使用的语言. python的历史: 1989年,为了打发圣诞节假期,作者Guido开始写P ...
随机推荐
- unittest框架下的HTMLTestRunner报告模块使用及优化
引言 在做接口自动化测试的时候,使用python单元测试框架unittest下HTMLTestRunner报告模板,可以很好的展示我们测试结果的数据. 官方的标准版模板地址:http://tungwa ...
- HandlerMethod解析
api写HandlerMethod的作用: Encapsulates information about a handler method consisting of a method and a b ...
- python:文件、目录遍历器
#!/usr/bin/python# -*- coding:utf-8 -*- import osimport json file = open('a.txt','w')for root,dirs,f ...
- C++-POJ2777-Count Color[线段树][lazy标记][区间修改]
分析:https://www.bilibili.com/read/cv4777102 #include <cstdio> #include <algorithm> using ...
- JS定义类的六种方式详解
转载自: http://www.jb51.net/article/84089.htm 在前端开发中,经常需要定义JS类.那么在JavaScript中,定义类的方式有几种,分别是什么呢?本文就JS定义类 ...
- c++/cli mixed codes for standard c++ and csharp
混合DotNet与Win32API来实现的Hidlibrary,c/c++可直接使用,c#可直接使用 异步IO,拔插事件订阅,数据读取事件订阅 工程目录结构 HidEvent.h #pragma on ...
- 变色html css js
<!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...
- jquery easyui combogrid Uncaught TypeError:Cannot read property
================================ ©Copyright 蕃薯耀 2020-01-07 https://www.cnblogs.com/fanshuyao/ 一.问题描述 ...
- Git的基本使用 -- 分支管理
查看分支 git branch 前面带 * 的为当前所在分支 创建分支 git branch 分支名 切换分支 git checkout 分支名 创建并切换到此分支 git checkout -b 分 ...
- HttpRunner接口自动化框架的使用
简介: HttpRunner 是一款面向 HTTP(S) 协议的通用测试框架,只需编写维护一份 YAML/JSON 脚本,即可实现自动化测试.性能测试.线上监控.持续集成等多种测试需求. HttpRu ...