class Student:
def __init__(self,name,age):
self.name=name
self.__age=age
def show(self):
print(self.name,self.__age)
stu=Student('张三',20)
stu.show()
print(stu.name)
print(dir(stu))
print(stu._Student__age)
class Person(object):
def __init__(self,name,age):
self.name=name
self.age=age
def info(self):
print(self.name,self.age)
class Student(Person):
def __init__(self,name,age,stu_num):
super().__init__(name,age)
self.stu_num=stu_num
def info(self):
super().info()
print(self.stu_num)
class Teacher(Person):
def __init__(self,name,age,teachofyear):
super().__init__(name,age)
self.teachofyear=teachofyear
def info(self):
super().info()
print(self.teachofyear)
stu=Student('张三',20,'1002')
tea=Teacher('李四',56,20)
stu.info()
tea.info()
class Stu:
def __init__(self,name,age):
self.name=name
self.age=age
def __str__(self):
return '我的名字是{0},今年{1}岁了!'.format(self.name,self.age)
stu1=Stu('秀月',25)
print(dir(stu1))
print(stu1)
class Animal(object):
def eat(self):
print('动物会吃')
class Dog(Animal):
def eat(self):
print('狗吃骨头')
class Cat(Animal):
def eat(self):
print('猫吃鱼')
class Person:
def eat(self):
print('人吃五谷杂粮')
def fun(obj):
obj.eat()
fun(Cat())
fun(Dog())
fun(Animal())
print('--------------------')
fun(Person())
print(dir(object))
class A:
pass
class B:
pass
class C(A,B):
def __init__(self,name,age):
self.name=name
self.age=age
class D(A):
pass
x=C('Jack',20)
print(x.__dict__)
print(C.__dict__)
print(x.__class__)
print(C.__bases__)
print(C.__base__)
print(C.__mro__)
print(A.__subclasses__())
a=20
b=100
c=a+b
d=a.__add__(b)
print(c)
print(d)
class Stu2:
def __init__(self,name):
self.name=name
def __add__(self, other):
return self.name+other.name
def __len__(self):
return len(self.name)
stu5=Stu2('张三')
stu6=Stu2('李四')
s=stu5.__add__(stu6)
print(s)
print('-----------------')
lst=[11,22,33,44]
print(len(lst))
print(lst.__len__())
print(len(stu5))
class Person(object):
def __new__(cls, *args, **kwargs):
print('__new__被调用执行了,cls的id值为:{0}'.format(id(cls)))
obj=super().__new__(cls)
print('创建的对象的id为:{0}'.format(id(obj)))
return obj
def __init__(self,name,age):
print('__init__被调用了,self的id值为:{0}'.format(id(self)))
self.name=name
self.age=age
print('object这个类对象的id为:{0}'.format(id(object)))
print('Person这个类对象的Id为:{0}'.format(id(Person)))
p1=Person('张三',20)
print('p1这个Person类的实例对象的id为:{0}'.format(id(p1)))
class CPU:
pass
class Disk:
pass
class Computer:
def __init__(self,cpu,disk):
self.cpu=cpu
self.disk=disk
cpu1=CPU()
cpu2=cpu1
print(cpu1)
print(cpu2)
print('------------------------')
disk=Disk()
computer=Computer(cpu1,disk)
import copy
computer2=copy.copy(computer)
print(computer,computer.cpu,computer.disk)
print(computer2,computer2.cpu,computer2.disk)
print('-------------------')
computer3=copy.deepcopy(computer)
print(computer,computer.cpu,computer.disk)
print(computer3,computer3.cpu,computer3.disk)
