- 다음 코드는 __init__(self, name, color) 메서드가 상속되고, - self.name과 self.color 도 __init__ 실행시 생성됨
class Figure:
def __init__(self, name, color):
self.__name = name
self.__color = color
class Quadrangle(Figure):
def set_area(self, width, height):
self.__width = width
self.__height = height
def get_info(self):
print (self.__name, self.__color, self.__width * self.__height)
square = Quadrangle('dave', 'blue')
square.set_area(5, 5)
square.get_info()
class Quadrangle:
def __init__(self, name, color):
self.name = name
self.color = color
def set_area(self, width, height):
self.__width = width
self.__height = height
def get_info(self):
print (self.name, self.color, self.__width * self.__height)
square = Quadrangle()
class Quadrangle(Figure):
def set_area(self, width, height):
self.__width = width
self.__height = height
def get_info(self):
print (self.name, self.color, self.__width * self.__height)
square = Quadrangle('dave', 'blue')
square.name
square = Quadrangle('dave', 'blue')
square.set_area(5, 5)
square.get_info()
# Quadrangle 클래스가 Figure 클래스의 자식 클래스인지 확인
issubclass(Quadrangle, Figure)
figure1 = Figure('figure1', 'black')
square = Quadrangle('square', 'red')
print(isinstance(figure1, Figure))
print(isinstance(square, Figure))
print(isinstance(figure1, Quadrangle))
print(isinstance(square, Quadrangle))
class Quadrangle(Figure):
def set_area(self, width, height):
self.__width = width
self.__height = height
def get_info(self):
print (self.name, self.color, self.__width * self.__height)
square = Quadrangle("square1", "black")
square.set_area(5, 5)
square.get_info()
# 클래스 선언
class Person:
def __init__(self, name):
self.name = name
class Student(Person):
def study(self):
print (self.name + " studies hard")
class Employee(Person):
def work(self):
print (self.name + " works hard")
# 객체 생성
student1 = Student("Dave")
employee1 = Employee("David")
# 객체 실행
student1.study()
employee1.work()
# 클래스 선언
class Person:
def __init__(self, name):
self.name = name
def work(self):
print (self.name + " works hard")
class Student(Person):
def work(self):
print (self.name + " studies hard")
# 객체 생성
student1 = Student("Dave")
# 자식 클래스(Student)의 재정의된 work(self) 호출
student1.work()
class Person:
def work(self):
print('work hard')
class Student(Person):
def work(self):
print('Study hard')
def go_to_school(self):
print('Go to school')
p1 = Person()
s1 = Student()
p1.work()
#p1.go_to_school()
s1.work()
s1.go_to_school()
class Car:
def __init__(self, name):
self.name = name
def get_info(self):
print (self.name)
class ElecCar(Car):
def get_info(self):
print (self.name, 'Fuel: Eletronic')
class GasoCar(Car):
def get_info(self):
print (self.name, 'Fuel: Gasoline')
elec = ElecCar('dave')
gaso = GasoCar('david')
print (elec.get_info(), gaso.get_info())
# 클래스 선언
class Person:
def work(self):
print('work hard')
class Student(Person):
def work(self):
print('Study hard')
def parttime(self):
super().work()
student1 = Student()
student1.work()
student1.parttime()
# 클래스 선언
class Person:
def work(self):
print('work hard')
class Student(Person):
def work(self):
print('Study hard')
def parttime(self):
super().work()
def general(self):
self.work()
student1 = Student()
student1.work()
student1.parttime()
student1.general()
# 클래스 선언
class Person:
def work(self):
print('work hard')
class Student(Person):
def work(self):
Person.work(self) # 부모 클래스 메서드 호출
print('Study hard')
student1 = Student()
student1.work()
class Figure:
# 생성자(initializer)
def __init__(self, width, height):
# self.* : 인스턴스변수
self.width = width
self.height = height
def print_info(self):
print ('사이즈:', self.width, self.height)
class Rectangle(Figure):
def print_info(self):
Figure.print_info(self) # 부모 클래스 메서드 호출
print ('너비:', self.width * self.height)
rectangle1 = Rectangle(2, 3)
rectangle1.print_info()
# 추상 클래스 선언하기
from abc import *
class Character(metaclass=ABCMeta):
@abstractmethod
def attack(self):
pass
@abstractmethod
def move(self):
pass
# 추상 클래스 상속하기
class Elf(Character):
def attack(self):
print ("practice the black art")
def move(self):
print ("fly")
class Human(Character):
def attack(self):
print ("plunge a knife")
def move(self):
print ("run")
# 객체 실행하기
elf1 = Elf()
human1 = Human()
elf1.attack()
elf1.move()
human1.attack()
human1.move()
# 추상 클래스 선언하기
from abc import *
class Character(metaclass=ABCMeta):
def __init__(self, hp):
self.hp = hp
def get_hp(self):
return self.hp
@abstractmethod
def attack(self):
pass
@abstractmethod
def move(self):
pass
character = Character(10) # 추상 클래스는 객체로 만들 수 없어요!!! 추상 메서드를 다 채워야 합니다.
# 추상 클래스 상속하기
class Elf(Character):
def attack(self):
print ("practice the black art")
def move(self):
print ("fly")
character = Elf(10)
print(character.get_hp())
# 추상 클래스 선언하기
from abc import *
class Car(metaclass=ABCMeta):
def __init__(self, name):
self.name = name
def get_info(self):
return self.name
@abstractmethod
def fuel(self):
pass
class ElecCar(Car):
def fuel(self):
return 'Electronic'
elec_car = ElecCar('dave')
print (elec_car.get_info(), elec_car.fuel())