zoukankan      html  css  js  c++  java
  • python笔记之类

    python不直接支持私有方式,可以在方法或者属性之前加上双下划线,将其变为私有,即外部无法直接调用
    访问私有方法或者属性,方法是: _<类名><变量名>

    首先类定义

    # -*- coding: UTF-8 -*- 
    #!/usr/bin/python
    #__metaclass__ = type
    
    class Person:
    	#self是对象自身的引用,将name值放在自己的命名空间中
    	def setName(self, name): 
    		self.name = name
    
    	def getName(self):
    		print self.name
    		return self.name
    		
    
    class NewPerson(object):
    	def setNewName(self, name): 
    		self.__name = name
    
    	def __getName(self):
    		print self.__name
    		return self.__name		
    
    
    class Student(Person):
    	def sayHello(self):
    		print "hello, my name is " + self.name
    
    
    #多继承
    class LazyBoy(NewPerson, Student):
    	pass
    
    

    练习

    shoren = Person() #初始化对象
    shoren.setName("shoren's name ... ") #设置
    shoren.getName()
    print shoren.name  #直接调取用户名
    #方法也可以当做一个变量,是可以随意绑定的
    getMyName = shoren.getName
    getMyName()
    
    np = NewPerson();
    np.setNewName("new name")
    try:
    	print np.__name #不能直接访问私有变量
    except Exception, e:
    	print e
    
    try:
    	print np.__getName #不能直接调用私有方法
    except Exception, e:
    	print e
    
    print np._NewPerson__name  #使用 _<类名><变量名> 访问私有变量
    np._NewPerson__getName()
    
    stu = Student();
    stu.setName("踏岚")
    stu.sayHello()
    
    lb = LazyBoy();		
    lb.name = "lazy boy"
    lb.sayHello()
    

    结果:

    shoren's name ... 
    shoren's name ... 
    shoren's name ... 
    
    'NewPerson' object has no attribute '__name'
    'NewPerson' object has no attribute '__getName'
    new name
    new name
    
    hello, my name is 踏岚
    
    hello, my name is lazy boy
    

    可用的方法:

    • isinstance(object, class) 对象是否是类的实例
    • issubclass(A, B) A是否为B的子类
    • hasattr(object, name) 对象是否有给定的属性
    • getattr(object, name[, default]) 获取属性的值,可以提供默认值
    • setattr(object, name, value) 设置对象属性值
    • type(object) 返回对象类型 [使用__metaclass__=type 或从object继承的类,可以使用此方法查看类型]
    #相关函数练习
    print isinstance(stu, Student)
    print issubclass(Student, Person)
    print hasattr(stu, "name")
    print hasattr(stu, "getName")
    print dir(np)
    print NewPerson.__base__
    print dir(stu)
    print stu.__class__ 
    print type(stu)
    print type(np)
    

    结果:

    True
    True
    True
    True
    ['_NewPerson__getName', '_NewPerson__name', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'setNewName']
    <type 'object'>
    ['__doc__', '__module__', 'getName', 'name', 'sayHello', 'setName']
    __main__.Student
    <type 'instance'>
    <class '__main__.NewPerson'>
    

    由上,NewPerson继承至object,所以含有更多的属性,且使用type()得到正确的值。如果在文件中加入__metaclass__ = type,则stu.class 和type(stu)值均为 <class 'main.Student'>

  • 相关阅读:
    shell基础之更改IP
    shell基础之if语句
    shell基础之变量及表达式
    shell基础之shell相关概念
    shell基础之bus实战(if 练习)
    shell基础之pxe批量部署
    shell基础之编译安装nginx
    Razor视图引擎基础语法
    EF三层
    EF简单增删改查
  • 原文地址:https://www.cnblogs.com/shoren/p/pyth-class.html
Copyright © 2011-2022 走看看