import car car1 = car.Van("Taro") car1.turn_left() car1.show_status() car2 = car.Camper("Jiro") car2.turn_right() car2.show_status() car2.make_ice()
# 정의한 클래스를 포함한 모듈을 임포트한다 import car # 밴을 생성한다 car1 = car.Van("Gildong") car1.turn_left() # --- (*1) 슈퍼 클래스에 있는 메서드를 사용할 수 있다 car1.show_status() # 캠핑카를 생성한다 car2 = car.Camper("Chulsoo") car2.turn_right() car2.show_status() car2.make_ice()
# 定義したクラスを含むモジュールを取り込む import car # ワゴン車を作成 car1 = car.Van('Taro') car1.turn_left() # 基底クラスのメソッドが使える car1.show_status() # キャンピングカーを作成 car2 = car.Camper("Jiro") car2.turn_right() car2.show_status() car2.make_ice()
class Car: # **** class Van(Car): def __init__(self, owner): super().__init__(owner) # **** import car car1 = car.Van("john") # -------- class Game: def __goal(self): print("") def play(self): print("") game = Game() game.play() game.__goal() # error # -------- class Empty: pass o = Empty() # -------- class Hoge: @staticmethod def introduce(): print("Hoge") Hoge.introduce() # -------- import json import urllib.request class Kawase: API = "" def __get_api(self): res = urllib.request.urlopen(Kawase.API) return res.read().decode('utf8') def __analize_result(self, json_str): return json.loads(json_str) def get_result(self): json_str = self.__get_api() return self.__analize_result(json_str) @staticmethod def get_usd_jpy(): kawase = Kawase() data = kawase.get_result() usd = data.get("JPY", -1) return usd print("USD:JPY = 1:", Kawase.get_usd_jpy()) # -------- class Pos: def __init__(self, x, y): self.x = x self.y = y def __add__(self, other): x2 = self.x + other.x y2 = self.y + other.y return Pos(x2, y2) def __mul__(self, other): if isinstance(other, (int, float)): x2 = self.x * other y2 = self.y * other return Pos(x2, y2) def __str__(self): return "({0}, {1})".format(self.x, self.y) # -------- class Tukimei: tuki = ["", "", ""] def __getitem__(self, key): i = int(key) return self.tuki[i - 1] def __setitem__(self,key,value): i = int(key) self.tuki[i - 1] = value t = Tukimei() print(t[3]) t[10] = "" # -------- class PrimeIter: def __init__(self, max): self.max = max def __iter__(self): self.n = 1 return self def __next__(self): is_prime = False self.n += 1 while not is_prime: is_prime = True for i in range(2, self.n): if self.n % i == 0: is_prime = False break if is_prime: break self.n += 1 if self.n >= self.max: raise StopIteration return self.n it = PrimeIter(100) for no in it: print(no, end = ",") # -------- class Clock: def __init__(self, hour): self._hour = hour self._ampm = "am" @property def hour(self): return self._hour @hour.setter def hour(self, value): self._hour = value % 12 self._ampm = "am" if value <= 12 else "pm" @property def ampm(self): return self._ampm # -------- from abc import ABCMeta, abstractmethod class MazeRobot(metaclass=ABCMeta): @abstractmethod def init_robot(self): pass @abstractmethod def choose_dir(self): pass from abc_robot import MazeRobot class MazeRobotTest(MazeRobot): def init_robot(self): print() def choose_dir(self): print() robot = MazeRobotTest() robot.init_robot() # -------- import unittest, turukame class TestTurukame(unittest.TestCase): def setUp(self): def tearDown(self): # pass def test_turukame(self): turu, kame = turukame.calc_turukame( turukame.Turu(), turukame.Kame(), heads = 10, legs = 28 ) self.assertEqual(turu, 6, "") self.assertEqual(kame, 4, "")