class Car:
def __init__(self,brand):
self.brand=brand
def start(self):
print('汽车已启动')
car=Car('宝马x5')
car.start()
print(car.brand)
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_no):
super().__init__(name,age)
self.stu_no=stu_no
class Teacher(Person):
def __init__(self,name,age,teachofyer):
super().__init__(name,age)
self.teachofyer=teachofyer
stu=Student('zhangsan',20,'1001')
teacher=Teacher('lisi',40,10)
stu.info()
teacher.info()
class A(object):
pass
class B(object):
pass
class C(A,B):
pass
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_no):
super().__init__(name,age)
self.stu_no=stu_no
def info(self):
super().info()
print(self.stu_no)
class Teacher(Person):
def __init__(self,name,age,teachofyer):
super().__init__(name,age)
self.teachofyer=teachofyer
def info(self):
super().info()
print('教龄',self.teachofyer)
stu=Student('张三',20,'1001')
teacher=Teacher('李四',40,10)
stu.info()
print('---------')
teacher.info()
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return '我的名字是{0},今年{1}岁了'.format(self.name, self.age)
stu=Student('张三', 20)
print(dir(stu))
print(stu)
print(type(stu))
class Animal(object):
def eat(self):
print('动物会吃')
class Dog(Animal):
def eat(self):
print('狗吃骨头')
class Cat(Animal):
def eat(self):
print('猫吃鱼')
class Person(object):
def eat(self):
print('人吃五谷杂粮')
def fun(obj):
obj.eat()
fun(Cat())
fun(Dog())
fun(Person())
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('---------')
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 Student:
def __init__(self,name):
self.name=name
def __add__(self, other):
return self.name+other.name
def __len__(self):
return len(self.name)
stu1=Student('张三')
stu2=Student('李四')
s=stu1+stu2
print(s)
s=stu1.__add__(stu2)
print(s)
print('--------------')
lst=[11,2,2,333,44]
print(len(lst))
print(lst.__len__())
print(len(stu1))
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,id(cpu1))
print(cpu2,id(cpu2))
print('-------------')
disk=Disk()
computer=Computer(cpu1,disk)
import copy
print(disk)
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)