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学习之面向对象基础的更多相关文章

  1. Python学习笔记——面向对象基础

    1.类和实例 1.1类的定义 类的定义使用class关键字,其后紧跟类名(通常大写开头),紧接着是(object),object是该类继承的类名,没有就继承object类. 实例化时就是类名+(),有 ...

  2. Python学习课程零基础学Python

    python学习课程,零基础Python初学者应该怎么去学习Python语言编程?python学习路线这里了解一下吧.想python学习课程?学习路线网免费下载海量python教程,上班族也能在家自学 ...

  3. 8.python笔记之面向对象基础

    title: 8.Python笔记之面向对象基础 date: 2016-02-21 15:10:35 tags: Python categories: Python --- 面向对象思维导图 (来自1 ...

  4. python学习日记(基础数据类型及其方法01)

    数字 int 主要是用于计算的,常用的方法有一种 #既十进制数值用二进制表示时,最少使用的位数i = 3#3的ASCII为:0000 0011,即两位 s = i.bit_length() print ...

  5. Python学习之==>面向对象编程(二)

    一.类的特殊成员 我们在Python学习之==>面向对象编程(一)中已经介绍过了构造方法和析构方法,构造方法是在实例化时自动执行的方法,而析构方法是在实例被销毁的时候被执行,Python类成员中 ...

  6. Python学习day16-模块基础

    <!doctype html>day16 - 博客 figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { ...

  7. Python学习day12-函数基础(2)

    <!doctype html>day12博客 figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { pos ...

  8. Python学习day11-函数基础(1)

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  9. Python学习笔记之基础篇(-)python介绍与安装

    Python学习笔记之基础篇(-)初识python Python的理念:崇尚优美.清晰.简单,是一个优秀并广泛使用的语言. python的历史: 1989年,为了打发圣诞节假期,作者Guido开始写P ...

随机推荐

  1. STM 32 内部功能回顾

    EXTI   外部中断 NVIC 嵌套的向量式中断控制器 AHB 是高级高性能内部总线,主要是用在CPU.DMA.DSP(数字信号处理) APB 是外围总线,I2C. 串口 APB 分为高速APB2( ...

  2. HTTP协议常用状态码

    HTTP协议常用状态码   <?php    send_http_status(404);   /**  * HTTP Protocol defined status codes * HTTP协 ...

  3. selenium 百度登录

    private String baseUrl="http://www.baidu.com/"; --------- @Test public void testLoginB() t ...

  4. poj-2253(最小瓶颈路问题)

    题目链接 Description Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fion ...

  5. LaTeX 文字带边框

    1.使用framed宏包 \usepackage{framed} 可以使用verb|...|和verbatim环境而不使用cprotect宏包的cprotect命令 \begin{framed} \v ...

  6. Python连接操作数据库

    步骤: 1.创建与数据库的连接对象: 2.创建游标: 3.通过游标执行语句 4.增删改需要提交(commit)数据 5.关闭连接 如: import MySQLdb   # Python通过MySQL ...

  7. 题解 【Codeforces988E】Divisibility by 25

    本题是数论好题! 首先我们需要了解一个关于数论的性质: 一个数只要后两位能被25(或4)整除,这个数就能被25(或4)整除. 同理,后三位:(或8).后四位:(或16)亦是如此. 所以,我们只需要判断 ...

  8. mysql中循环插入数据

    循环插入1w条数据 表结构: mysql> desc time_table; +-------+-----------+------+-----+-------------------+---- ...

  9. C++ 实例练习-替换原生数组

    C++ 实例练习-替换原生数组 main.cpp #include <stdio.h> #include "intarray.h" int main(int argc, ...

  10. 解决使用git出现 The file will have its original line endings in your working directory

    执行以下命令即可解决 git rm -r --cached . git config core.autocrlf false git add . . 代表当前目录