class A():
name = 'jjc'
age = 18
for k,v in A.__dict__.items():
print(k,"--",v)
class Student():
name = "jjc"
age = 18
# 注意say的写法
def say(self):
self.name = 'jjc1'
self.age = 200
print('My name is {0}'.format(self.name))
print('My age is {0}'.format(self.age))
def sayagain(s):
s.name = '666'
s.age = 31
print('My name is {0}'.format(s.name))
print('My age is {0}'.format(s.age))
yueyue = Student()
yueyue.say()
yueyue.sayagain()
print(yueyue.name)
for k,v in Student.__dict__.items():
print(k,"--",v)
# 属性案例
# 创建Student类,描述学生类
# 学生具有Student.name属性
# 但 named格式不统一
class Student():
def __init__(self,name,age):
self.name = name
self.age = age
#self.setName(name)
def intro(self):
print('Hi, my name is {0}'.format(self.name.title()))
def setName(self,name):
self.name = name.title()
s1 = Student('jJC',19)
s2 = Student('wcx',18)
s1.intro()
s2.intro()
# -*- coding: GBK -*-
# -*- coding: UTF-8 -*-
'''
class A():
name = 'jjc'
age = 18
for k,v in A.__dict__.items():
print(k,"--",v)
class Student():
name = "jjc"
age = 18
# 注意say的写法
def say(self):
self.name = 'jjc1'
self.age = 81
print('My name is {0}'.format(self.name))
print('My age is {0}'.format(self.age))
def sayagain(s):
s.name = '666'
s.age = 31
print('My name is {0}'.format(s.name))
print('My age is {0}'.format(s.age))
yueyue = Student()
#yueyue.say()
Student.say(Student)
yueyue.sayagain()
'''
# 属性案例
# 创建Student类,描述学生类
# 学生具有Student.name属性
# 但 named格式不统一
# 可以用增加一个函数
'''
class Student():
def __init__(self,name,age):
self.name = name
self.age = age
#self.setName(name)
def intro(self):
print('Hi, my name is {0}'.format(self.name.upper()))
def setName(self,name):
self.name = name.title()
s1 = Student('jJC',19)
s2 = Student('wcx',18.0)
s1.intro()
s2.intro()
'''
'''
# property案例
# 定义一个property类,具有name,age属性
# 对于任意输入的姓名,我们希望都用大写方式保存
# 年龄,我们希望内部统一用整数保存
# x = property(fget, fset, fdel, doc)
class Person():
# 说明文档
#这是一个人,一个高速钢的人
# 函数的名称可以任意
def fget(self):
return self._name * 2
def fset(self,name):
# 所有输入的姓名以大写形式保存
self._name = name.upper()
def fdel(self):
self._name = "NoName"
name = property(fget, fset, fdel, "对name进行下下操作啦")
p1 = Person()
p1.name = "jjc"
print(p1.name)
# 类的内置属性距离
print(Person.__dict__)
print(Person.__doc__)
print(Person.__name__)
print(Person.__bases__)
'''
'''
# __init__ 举例
class A():
def __getattr__(self,name):
print('没找到')
print(name)
a = A()
print(a.name)
print(a.addr)
'''
'''
# __setattr__案例
class Person():
def __init__(self):
pass
def __setattr__(self, name, value):
print('设置属性:{0}'.format(name))
# 下面语句会导致问题,死循环
#self.name = value
# 此种情况,为了避免死循环,规定统一调用父类魔法函数
super().__setattr__(name,value)
p = Person()
print(p.__dict__)
p.age = 18
'''
'''
# __get__
class Student():
def __init__(self,name):
self._name = name
def __gt__(self,obj):
print("haha, {0}会比{1}大吗?".format(self,obj))
return self._name > obj._name
# 字符串的比较是按什么规则
stu1 = Student('one')
stu2 = Student('two')
print(stu1 > stu2)
'''
'''
# 三种方法案例
class Person:
# 实例方法
def eat(self):
print(self)
print('Eating……')
# 类方法
# 类方法的第一个参数,一般命名为cls,区别于self
@classmethod
def play(cls):
print(cls)
print('Playing……')
# 静态方法
# 不需要用第一个参数表示自身或者类
@staticmethod
def say():
print('Saying……')
yueyue = Person()
#实例方法
yueyue.eat()
# 类方法
Person.play()
yueyue.play()
# 静态方法
Person.say()
yueyue.say()
'''
'''
class A():
def __init__(self):
self._name = 'haha'
self._age = 18
# 此功能,是对类变量进行读取操作的时候应该执行的函数功能
def fget(self):
print("{0}被读取了".format(self._name))
return self._name
# 模拟的是对变量进行写操作的时候执行的功能
def fset(self, val):
print("{0}我被写入了,但是还可以做好多事情".format(val))
self._name = '我是' + val
# fdel模拟的是删除变量的时候进行的操作
def fdel(self):
del self._name
name = property(fget, fset, fdel, "这是一个property的例子")
a = A()
a.name = "jjc"
print(a.name)
'''
'''
#上面的代码等价于下面的代码
class A(object):
@property
def name(self):
print("{0}被读取了".format(self._name))
return self._name
@name.setter
def name(self,val):
print("{0}我被写入了,但是还可以做好多事情".format(val))
self._name = '我是' + val
@name.deleter
def name(self):
del self._name
a = A()
a.name = "jjc"
print(a.name)
'''
'''
# 抽象类的实现
import abc
# 申明一个类并且制定当前类的元类
class Human(metaclass = abc.ABCMeta):
# 定义一个抽象的方法
@abc.abstractmethod
def smoking(self):
pass
# 定义类抽象方法
@abc.abctractclassmethod
def drink():
pass
# 定义静态抽象方法
@abc.abstractstaticmethod
def play():
pass
'''
'''
# 自己组装一个类
# 如果要绑定对象,用下面一句代码,绑定类,不需要
from types import MethodType
class A():
pass
def say(self):
print("saying……")
a = A()
a.tt = MethodType(say,A)
a.tt()
'''
'''
# 利用type造一个类
# 先定义类应该具有的成员函数
def say(self):
print("saying……")
def talk(self):
print("Talking……")
# 用type来创建一个类
A = type("AName",(object,),{"class_say":say, "class_talk":talk})
# 然后可以像正常访问一样使用类
a = A()
a.class_say()
a.class_talk()
'''
# 元类演示
# 元类写法是固定的,必须继承自type
# 元类一般命名以MetaClass结尾
class TulingMetaClass(type):
# 注意一下写法
def __new__(cls, name, bases, attrs):
# 自己的业务处理
print("我是元类")
attrs['id'] = '000000'
attrs['addr'] = "苏州市"
return type.__new__(cls, name, bases, attrs)
# 元类定义完就可以使用,使用注意写法
class Teacher(object, metaclass = TulingMetaClass):
pass
t = Teacher()
t.id