class Person(object):
__count = 0
def __init__(self,name,age):
Person.__count += 1
self.name = name
self.age = age
@classmethod
def get_count(cls):
return cls.__count
p1 = Person('张三',18)
p2 = Person('李四',18)
p3 = Person('王五',18)
print(Person.get_count)
class Animal(object):
def __init__(self,name,age):
self.name = name
self.age = age
def sleep(self):
print(self.name + '在睡觉')
class Dog(object):
def __init__(self,name,age):
self.name = name
self.age = age
def sleep(self):
print(self.name + '在睡觉')
def bark(self):
print(self.name + '正在叫')
class Student(object):
def __init__(self,name,age):
self.name = name
self.age = age
def sleep(self):
print(self.name + '在睡觉')
def study(self):
print(self.name + '爱学习')
class Animal(object):
def __init__(self,name,age):
self.name = name
self.age = age
def sleep(self):
print(self.name + '在睡觉')
class Dog(Animal):
def bark(self):
print(self.name + '正在叫')
class Student(Animal):
def study(self):
print(self.name + '爱学习')
d1 = Dog('大黄',12)
print(d1.name)
d1.sleep()
s1 = Student('小帅',18)
s1.sleep()
s1.study()
class A(object):
def demo_a(self):
print('我是A类里的方法demo_a')
def foo(self):
print('我是A类里的方法foo_a')
class B(object):
def demo_b(self):
print('我是B类里的方法demo_b')
def foo(self):
print('我是B类里的方法foo_b')
class C(B,A):
pass
c = C()
c.demo_a()
c.demo_b()
c.foo()
print(C.__mro__)
class A:
pass
class B:
def foo(self):
print('我是B类里的方法foo_b')
class C(A):
def foo(self):
print('我是C类里的方法foo_c')
class D(B):
pass
class E:
pass
class X(D,C,E):
pass
X().foo()
print(X.__mro__)
class Person:
type = 'human'
def __init__(self,name,age):
self.name = name
self.age = age
def demo(self):
print('姓名是',self.name)
@classmethod
def bar(cls):
print(cls is Person)
print(cls.type)
@staticmethod
def foo():
print('hello world')
p = Person('小帅',18)
p.demo()
Person.demo(p)
p.bar()
Person.bar()
class Animal:
def __init__(self,name,age):
self.name = name
self.age = age
self.__money = 1000
def eat(self):
print(self.name + '在吃东西')
def __test(self):
print('__test方法')
class Person(Animal):
def __demo(self):
print('__demo方法')
p = Person('小明',18)
print(p.name)
p.eat()
p._Person__demo()
p._Animal__test()
print(p._Animal__money)
class Student(object):
pass
class Dog:
pass
print('20200814')
class Animal(object):
def __init__(self,name,age):
self.name = name
self.age = age
class X(object):
pass
class Student(Animal,X):
pass
p1 = Animal('张三',18)
p2 = Animal('张三',18)
s = Student('jack',18)
print(p1 is p2)
print(type(p1))
print(type(p1) == Animal)
print(type(s) == Student)
print(type(s) == Animal)
print(isinstance(s,(Student,X)))
print(isinstance(s,Animal))
print(issubclass(Student,Animal))
class Person:
def __init__(self,name,age):
self.name = name
self.age = age
def eat(self):
print(self.name + '在吃东西')
class Student(Person):
def __init__(self,name,age,school):
super(Student,self).__init__(name,age)
self.school = school
def eat(self):
print(self.name + '休息的时候在吃东西')
def study(self):
print(self.name + '在学习')
s = Student('小明',18,'清华小学')
s.eat()
s.study()
class PoliceDog(object):
def attack_enemy(self):
print('警犬在攻击坏人')
class BlindDog(object):
def lead_road(self):
print('导盲犬在引路 ')
class DrugDog(object):
def search_drug(self):
print('缉毒犬在搜索')
class Person(object):
def __init__(self,name,dog):
self.name = name
self.dog = dog
def work_with_pd(self):
print(self.name + '在工作')
self.dog.attack_enemy()
def work_with_bd(self):
print(self.name + '在工作')
self.dog.lead_road()
pd = PoliceDog()
police = Person('张警官',pd)
police.dog = pd
police.work_with_pd()
bd = BlindDog()
police = Person('张警官',pd)
police.dog = bd
police.work_with_bd()
class Dog(object):
def work(self):
print('在工作')
class PoliceDog(Dog):
def work(self):
print('警犬在攻击坏人')
class BlindDog(Dog):
def work(self):
print('导盲犬在引路 ')
class DrugDog(Dog):
def work(self):
print('缉毒犬在搜索')
class Person(object):
def __init__(self,name):
self.name = name
self.dog = None
def work_with_dog(self):
if self.dog is not None and isinstance(self.dog,Dog):
self.dog.work()
police = Person('张警官')
pd = PoliceDog()
police.dog = pd
police.work_with_dog()
bd = BlindDog()
police.dog = bd
police.work_with_dog()
dd = DrugDog()
police.dog = dd
police.work_with_dog()
file = open('Z:\\翟少帅\\整理\\浮躁\\Python\\Notepad++配置Python开发环境.txt','r',encoding='utf8')
print(file.read())
file.close()
file = open(r'Z:\翟少帅\整理\浮躁\Python\Notepad++配置Python开发环境.txt','r',encoding='utf8')
print(file.read())
file.close()
import os
print(os.sep)
file = open('Z:/翟少帅/整理/浮躁/Python/Notepad++配置Python开发环境.txt','r',encoding='utf8')
print(file.read())
file.close()
import os
print(os.getcwd())
file = open('./xxx.txt','w')
file.write('2020')
file.close()
file = open('./xxx.txt','wb')
file.write('2021'.encode('utf8'))
file.close()
file = open('./xxx.txt','r')
print(file.read())
file.close()
file = open('yyy.txt','w+')
file.write('哈哈哈哈')
file.seek(0,0)
print(file.read())
file.close()