#coding=utf-8

class Person(object):
    id=12
    def __init__(self,name):
        self.name=name
        self.__inName='ads'

def __setId(self,id):
        Person.id=id*2

def getId(self):
        self.__setId(18)
        return Person.id

p=Person("prel")
print p.getId()
print "call private function from out of class"
print p.__setId()#报错

c:\Python27\Scripts>python task_test.py
36
call private function from out of class
Traceback (most recent call last):
  File "task_test.py", line 19, in <module>
    print p.__setId()
AttributeError: 'Person' object has no attribute '__setId'

python 类的私有方法例子的更多相关文章

  1. python 类的私有属性和方法 (转载)

    转载:http://www.runoob.com/python/python-object.html 类属性与方法 类的私有属性 __private_attrs:两个下划线开头,声明该属性为私有,不能 ...

  2. python类之魔法方法

    python类之魔法方法: class A(object): def __init__(self,x): self.x = x def __neg__(self): print('-v') def _ ...

  3. Python学习笔记之面向对象编程(三)Python类的魔术方法

    python类中有一些方法前后都有两个下划线,这类函数统称为魔术方法.这些方法有特殊的用途,有的不需要我们自己定义,有的则通过一些简单的定义可以实现比较神奇的功能 我主要把它们分为三个部分,下文也是分 ...

  4. python 类属性与方法

    Python 类属性与方法 标签(空格分隔): Python Python的访问限制 Python支持面向对象,其对属性的权限控制通过属性名来实现,如果一个属性有双下划线开头(__),该属性就无法被外 ...

  5. 在C#中我们能调用一个类的私有方法吗

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:在C#中我们能调用一个类的私有方法吗.

  6. 第8.3节 Python类的__init__方法深入剖析:构造方法与继承详解

    第8.3节 Python类的__init__方法深入剖析:构造方法与继承详解 一.    引言 上两节介绍了构造方法的语法及参数,说明了构造方法是Python的类创建实例后首先执行的方法,并说明如果类 ...

  7. python 类的私有变量和私有方法

    #!/usr/bin/env python # -*- coding:utf-8 -*- # @Time : 2017/11/08 8:46 # @Author : lijunjiang # @Fil ...

  8. Python面向对象 --- 新旧式类、私有方法、类属性和类方法、静态方法

    一.Python面向对象中的新旧式类 1)新式类(推荐使用):在定义类时,类后边括号里要继承基类(object).在python3.x中若没有指定父类,会默认使用的是object作为基类:在pytho ...

  9. Python 类的私有属性与私有方法

    1.隐藏的使用场景 在Python类中,有些属性和方法只希望在对象的内部被使用,而不希望在外部被访问到, 2.定义方式, 在属性名或方法名前增加两个下划线,定义的就是私有属性或方法 #其实这仅仅这是一 ...

随机推荐

  1. 【C#】简单计算器源代码

    form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.D ...

  2. powerdesigner唯一约束设置

    双击unique_pos_code

  3. CodeForce 832A Sasha and Sticks

    A. Sasha and Sticks time limit per test2 seconds memory limit per test256 megabytes inputstandard in ...

  4. ESXI虚拟机磁盘管理(精简-厚置-精简)

    VMwareESX/ESXi 精简置备(thin)与厚置备(thick)虚拟机磁盘之间转换 VMwareESX/ESXi 虚拟机磁盘thin与thick之间转换 注意:转换前请先关闭虚拟机!!! 一. ...

  5. hadoop的两类配置文件及3种启动/关闭方式

    hadoop配置文件 默认配置文件:四个模块相对应的jar包中:$HADOOP_HOME/share/hadoop        *core-default.xml        *hdfs-defa ...

  6. Taking a peek inside with the Actuator

    Taking a peek inside with the Actuator <dependency> <groupId>org.springframework.boot< ...

  7. Why is long2ip conversion important?

    Frequently Asked Questions about Convert long to IP address https://long2ip.com/faq/ What is long2ip ...

  8. 44(function pointer 2)

    #include<iostream> using namespace std; class A { public: int x; int sayhello() { cout<< ...

  9. 使用Maven导出项目依赖的jar包

    步骤1.进入项目目录(有pom.xml的目录) 2.创建存放导出jar依赖包的目录 3.地址栏输入cmd,回车 4.输入导出命令,回车mvn dependency:copy-dependencies  ...

  10. Ubuntu 16.04配置国内高速apt-get更新源

    https://www.linuxidc.com/Linux/2017-11/148627.htm Ubuntu 16.04下载软件速度有点慢,因为默认的是从国外下载软件,那就更换到国内比较好的快速更 ...