대상을 대상으로 프로그래밍하는 - 계승(조합, 다중, 모든 대상, 대상을 대상으로 하는 고급 내장 함수 & 방법, 반사)
조합
:
ps: :is-a
:has-a
class Teacher:
def __init__(self, name, age, gender, level):
self.name = name
self.age = age
self.gender = gender
self.level = level
def tell(self):
print("%s:%s" % (self.name, self.age))
class Student:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
class Course:
def __init__(self, name, price, period):
self.name = name
self.price = price
self.period = period
def tell(self):
print('' % (self.name, self.price, self.period))
tea1 = Teacher("egon", 18, "male", 10)
stu1 = Student("xxx", 19, "male")
python = Course("python ", 30000, "3mons")
linux = Course("linux ", 30000, "3mons")
tea1.courses = [python,linux]
stu1.course = python
# tea,stu #
# stu1.course.tell()
for course_obj in tea1.courses:
course_obj.tell()
2. 다태
: , \ \
: ,
1、 ,
# import abc
#
# class Animal(metaclass=abc.ABCMeta):
# @abc.abstractmethod
# def speak(self):
# pass
#
# @abc.abstractmethod
# def run(self):
# pass
#
# # Animal() # Animal
#
# class People(Animal):
# def speak(self):
# print(" ")
#
# def run(self):
# print(" ...")
#
# class Dog(Animal):
# def giao(self):
# print(" ")
#
# class Pig(Animal):
# def heheng(self):
# print(" ")
# peo1=People()
# d1=Dog()
# p1=Pig()
# peo1.jiao()
# d1.giao()
# p1.heheng()
# peo1.speak()
# d1.speak()
# p1.speak()
# def speak(animal):
# animal.speak()
#
# speak(peo1)
# speak(d1)
#
# speak(p1)
2、 :duck
# class People:
# def speak(self):
# print(" ")
#
# def run(self):
# print(" ...")
#
# class Dog:
# def speak(self):
# print(" ")
#
# def run(self):
# print(" ...")
#
# class Pig:
# def speak(self):
# print(" ")
#
# def run(self):
# print(" ...")
#
#
# peo1=People()
# d1=Dog()
# p1=Pig()
# peo1.run()
# d1.run()
# p1.run()
class Cpu:
def read(self):
pass
def write(self):
pass
class Process:
def read(self):
pass
def write(self):
pass
class Disk:
def read(self):
pass
def write(self):
pass
3. 모든 대상
==
# x = 11 # x=int(11)
# print(int)
# class Foo:
# pass
# print(Foo)
x = [1,2,3] # list([1,2,3])
y = [111,222] # list([1,2,3])
# x.append(4)
# y.append(3333)
# list.append(x,4)
# list.append(y,333)
# print(x)
# print(y)
print(type(x))
4. 대상을 대상으로 하는 고급
1. 내장 함수
is isinstance issubclass
x = 111
print(type(x) is int)
print(isinstance(x,int))
class Bar:
pass
class Foo(Bar):
pass
print(issubclass(Foo,Bar))
2. 내장 방법
(1)__str__
class People:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
# print('===>')
return "" %(self.name,self.age)
obj = People("egon", 18)
print(obj) # print(obj.__str__())
(2)__del__
class People:
def __init__(self, name, age,f):
self.name = name
self.age = age
self.f = f
def __del__(self): #
print('===>')
#
self.f.close()
obj = People("egon", 18,open("a.txt",'w',encoding='utf-8'))
del obj
# print(' ...')
5. 반사dir(대상)
hasattr getattr setattr delattr
class Foo:
def __init__(self, x, y):
self.x = x
self.y = y
def f1(self):
print('from f1')
obj = Foo(111, 222)
#
# res = dir(obj)
# print(res)
# print(obj.__dict__[res[-1]])
# print(obj.__dict__['x'])
# import re
#
# for attr in dir(obj):
# if not re.search("^__.*__$",attr):
# res=getattr(obj,attr)
# print(res)
# print(hasattr(obj,'x'))
# print(getattr(obj,'xxx',None)) # obj.xxx
# setattr(obj,"xxx",1111) # obj.xxx = 1111
#
# delattr(obj,"xxx") # del obj.xxx
# m=__import__("time")
# m.sleep(3)
# getattr(m,'sleep')(3)
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.