09-15-周一_11-25-51
This commit is contained in:
@@ -169,8 +169,8 @@ class Human:
|
|||||||
def work(self):
|
def work(self):
|
||||||
print(self,'会工作')
|
print(self,'会工作')
|
||||||
|
|
||||||
Human.work('chensong')
|
Human.work('张三')
|
||||||
Human.__dict__['work']('chensong')
|
Human.__dict__['work']('张三')
|
||||||
```
|
```
|
||||||
|
|
||||||
## 从对象的角度研究类
|
## 从对象的角度研究类
|
||||||
@@ -210,7 +210,7 @@ class Human:
|
|||||||
self.a = age
|
self.a = age
|
||||||
self.h = hobby
|
self.h = hobby
|
||||||
|
|
||||||
obj = Human('chensong','男','18','男')
|
obj = Human('张三','男','18','男')
|
||||||
```
|
```
|
||||||
|
|
||||||
### 对象操作对象空间属性
|
### 对象操作对象空间属性
|
||||||
@@ -229,7 +229,7 @@ class Human:
|
|||||||
self.a = age
|
self.a = age
|
||||||
self.h = hobby
|
self.h = hobby
|
||||||
|
|
||||||
obj = Human('chensong','男',18,'男')
|
obj = Human('张三','男',18,'男')
|
||||||
print(obj.__dict__)
|
print(obj.__dict__)
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -247,7 +247,7 @@ class Human:
|
|||||||
self.a = age
|
self.a = age
|
||||||
self.h = hobby
|
self.h = hobby
|
||||||
|
|
||||||
obj = Human('chensong','男',18,'男')
|
obj = Human('张三','男',18,'男')
|
||||||
obj.job = 'IT'
|
obj.job = 'IT'
|
||||||
del obj.n
|
del obj.n
|
||||||
obj.s = '女'
|
obj.s = '女'
|
||||||
@@ -269,7 +269,7 @@ class Human:
|
|||||||
self.a = age
|
self.a = age
|
||||||
self.h = hobby
|
self.h = hobby
|
||||||
|
|
||||||
obj = Human('chensong','男',18,'男')
|
obj = Human('张三','男',18,'男')
|
||||||
print(obj.mind)
|
print(obj.mind)
|
||||||
print(obj.language)
|
print(obj.language)
|
||||||
obj.a = 666
|
obj.a = 666
|
||||||
@@ -297,7 +297,7 @@ class Human:
|
|||||||
def tools(self):
|
def tools(self):
|
||||||
print(self.n,'会使用工具')
|
print(self.n,'会使用工具')
|
||||||
|
|
||||||
obj = Human('chensong','男',18,'男')
|
obj = Human('张三','男',18,'男')
|
||||||
obj.work()
|
obj.work()
|
||||||
obj.tools()
|
obj.tools()
|
||||||
```
|
```
|
||||||
|
@@ -98,7 +98,7 @@ print(Person.type_name)
|
|||||||
Person.eat('东西')
|
Person.eat('东西')
|
||||||
print(Person.type_name)
|
print(Person.type_name)
|
||||||
|
|
||||||
p1 = Person('aaron','男',18)
|
p1 = Person('张三','男',18)
|
||||||
print(p1.__dict__)
|
print(p1.__dict__)
|
||||||
print(p1.type_name)
|
print(p1.type_name)
|
||||||
p1.type_name = '666'
|
p1.type_name = '666'
|
||||||
@@ -131,7 +131,7 @@ class Cat(Aniaml):
|
|||||||
class Dog(Aniaml):
|
class Dog(Aniaml):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
p1 = Person('eagle','男',18)
|
p1 = Person('张三','男',18)
|
||||||
p1.eat()
|
p1.eat()
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -164,7 +164,7 @@ class Cat(Aniaml):
|
|||||||
class Dog(Aniaml):
|
class Dog(Aniaml):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
p1 = Person('aaron','男',18,'想吃东西')
|
p1 = Person('张三','男',18,'想吃东西')
|
||||||
p1.eat()
|
p1.eat()
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -196,7 +196,7 @@ class Cat(Aniaml):
|
|||||||
class Dog(Aniaml):
|
class Dog(Aniaml):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
p1 = Person('aaron','男',18,'想吃东西')
|
p1 = Person('张三','男',18,'想吃东西')
|
||||||
p1.eat()
|
p1.eat()
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@@ -20,8 +20,8 @@ class Foo:
|
|||||||
self.name = name
|
self.name = name
|
||||||
self.age = age
|
self.age = age
|
||||||
|
|
||||||
obj1 = Foo('chensong',18)
|
obj1 = Foo('张三',18)
|
||||||
obj2 = Foo('aaron',16)
|
obj2 = Foo('李四',16)
|
||||||
```
|
```
|
||||||
|
|
||||||
第二步:从某处调用被封装的内容
|
第二步:从某处调用被封装的内容
|
||||||
@@ -36,8 +36,8 @@ class Foo:
|
|||||||
print(self.name)
|
print(self.name)
|
||||||
print(self.age)
|
print(self.age)
|
||||||
|
|
||||||
obj1 = Foo('chensong',18)
|
obj1 = Foo('张三',18)
|
||||||
obj2 = Foo('aaron',16)
|
obj2 = Foo('李四',16)
|
||||||
|
|
||||||
print(obj1.name)
|
print(obj1.name)
|
||||||
print(obj2.age)
|
print(obj2.age)
|
||||||
|
@@ -311,9 +311,9 @@ class Student:
|
|||||||
return cls.__num
|
return cls.__num
|
||||||
|
|
||||||
|
|
||||||
Student('陈松', 18)
|
Student('张三', 18)
|
||||||
Student('阿松', 36)
|
Student('李四', 36)
|
||||||
Student('松松', 73)
|
Student('王五', 73)
|
||||||
print(Student.getNum())
|
print(Student.getNum())
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -370,7 +370,7 @@ class People:
|
|||||||
def bmi(self):
|
def bmi(self):
|
||||||
return self.weight / (self.height**2)
|
return self.weight / (self.height**2)
|
||||||
|
|
||||||
p1=People('陈松',75,1.85)
|
p1=People('张三',75,1.85)
|
||||||
print(p1.bmi)
|
print(p1.bmi)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@@ -20,7 +20,7 @@ class Foo:
|
|||||||
def say_hi(self):
|
def say_hi(self):
|
||||||
print('hi,%s'%self.name)
|
print('hi,%s'%self.name)
|
||||||
|
|
||||||
obj=Foo('陈松',73)
|
obj=Foo('张三',73)
|
||||||
|
|
||||||
# 检测是否含有某属性
|
# 检测是否含有某属性
|
||||||
print(hasattr(obj,'name'))
|
print(hasattr(obj,'name'))
|
||||||
@@ -55,7 +55,7 @@ class Foo(object):
|
|||||||
staticField = "test"
|
staticField = "test"
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.name = '陈松'
|
self.name = '张三'
|
||||||
|
|
||||||
def func(self):
|
def func(self):
|
||||||
return 'func'
|
return 'func'
|
||||||
@@ -293,7 +293,7 @@ class A:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
pass
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return '陈松'
|
return '张三'
|
||||||
a = A()
|
a = A()
|
||||||
print(a)
|
print(a)
|
||||||
print('%s' % a)
|
print('%s' % a)
|
||||||
@@ -308,7 +308,7 @@ class A:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
pass
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '陈松'
|
return '张三'
|
||||||
a = A()
|
a = A()
|
||||||
print(repr(a))
|
print(repr(a))
|
||||||
print('%r'%a)
|
print('%r'%a)
|
||||||
|
Reference in New Issue
Block a user