classPerson: """ 사람을 표현하는 클래스 ... Attributes ---------- name : str name of the person age : int age of the person ... ... Methods ---------- info(additional=""): prints the person's name and age """ def__init__(self, name, age): """ Constructs all the neccessary attributes for the person object Parameters ---------- name : str name of the person age : int age of the person """
self.name = name self.age = age
definfo(self, additional=None): """ 귀찮음... Parameters ---------- additional : str, optional more info to be displayed (Default is None) Returns ------- None """
print(f'My name is {self.name}. i am {self.age} years old.' + additional)
if __name__ == "__main__": person = Person("Evan", age = 20) person.info("나의 직장은 00이야") help(Person)
My name is Evan. i am 20 years old.나의 직장은 00이야
Help on class Person in module __main__:
class Person(builtins.object)
| Person(name, age)
|
| 사람을 표현하는 클래스
| ...
|
| Attributes
| ----------
| name : str
| name of the person
|
| age : int
| age of the person
| ...
| ...
|
| Methods
| ----------
|
| info(additional=""):
| prints the person's name and age
|
| Methods defined here:
|
| __init__(self, name, age)
| Constructs all the neccessary attributes for the person object
|
| Parameters
| ----------
| name : str
| name of the person
|
| age : int
| age of the person
|
| info(self, additional=None)
| 귀찮음...
|
| Parameters
| ----------
| additional : str, optional
| more info to be displayed (Default is None)
|
| Returns
| -------
| None
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| person = <__main__.Person object>
# 더하기, 빼기 기능이 있는 클래스 classCalculator: def__init__(self): self.result = 0 defadd(self,num): self.result += num return self.result defsub(self,num): self.result -= num return self.result
# 4칙연산 기능이 달린 클래스 정의 # 일단 더하기만 구현 classFourCal: defsetdata(self, first, second): self.first = first self.second = second defadd(self): result = self.first + self.second return result
# 4칙연산 기능이 달린 클래스 정의 classFourCal: defsetdata(self, first, second): self.first = first self.second = second defadd(self): result = self.first + self.second return result defmul(self): result = self.first * self.second return result defsub(self): result = self.first - self.second return result defdiv(self): result = self.first / self.second return result
# setdata를 생성자 __init__으로 변경 classFourCal: def__init__(self, first, second): self.first = first self.second = second defadd(self): result = self.first + self.second return result defmul(self): result = self.first * self.second return result defsub(self): result = self.first - self.second return result defdiv(self): result = self.first / self.second return result
a = FourCal(4, 2) # 이전과 달리 매개변수도 작성해야 작동. print(a.mul()) print(a.sub()) print(a.div())
8
2
2.0
클래스의 상속
FourCal 클래스는 만들어 놓았으므로 FourCal 클래스를 상속하는 MoreFourCal 클래스는 다음과 같이 간단하게 만들 수 있다.