class 클래스명:
def __init__(self):
self.전역변수명1 = 데이터
def 함수명1:
self.전역변수명1 = 데이터
# java의 main
if __name__ == '__main__':
클래스명이름 = 클래스명()
print(클래스명이름.전역변수명1)
단일 상속시 사용법
class 클래스명:
def __init__(self):
self.전역변수명1 = 데이터
def 함수명1:
self.전역변수명1 = 데이터
# 상속
class 클래스2(클래스명):
def __init__(self):
super().__init__()
self.전역변수명2 = 데이터
def 함수명2:
self.전역변수명2 = 데이터
# java의 main
if __name__ == '__main__':
클래스명이름 = 클래스2()
print(클래스명이름.전역변수명1)
print(클래스명이름.전역변수명2)
다중 상속 사용법
class 클래스1:
def __init__(self):
self.전역변수명1 = 데이터
def 함수명1:
self.전역변수명1 = 데이터
class 클래스2:
def __init__(self):
self.전역변수명2 = 데이터
def 함수명2:
self.전역변수명2 = 데이터
# 다중 상속
class 클래스3(클래스1, 클래스2):
def __init__(self):
클래스1.__init__(self)
클래스2.__init__(self)
# java의 main
if __name__ == '__main__':
클래스이름 = 클래스3()
print(클래스이름.전역변수명1)
print(클래스이름.전역변수명2)
클래스 사용 예시
Java 코드 -> Python 코드1
package day03;
public class Animal {
int life_span = 10;
void fightTiger() {
life_span = 0;
}
}
package day03;
public class OopTest01 {
public static void main(String[] args) {
Animal ani = new Animal();
System.out.println("life_span : " + ani.life_span);
ani.fightTiger();
System.out.println("life_span : " + ani.life_span);
}
}
결과 화면1
class Animal:
def __init__(self):
self.life_span = 10
def fightTiger(self):
self.life_span = 0
if __name__ == '__main__':
ani = Animal()
print("life_span : ", ani.life_span)
ani.fightTiger()
print("life_span : ", ani.life_span)
결과 화면3
Java 코드 -> Python 코드2
package day03;
public class Human extends Animal{
int money = 0;
void work() {
money += 30000;
}
}
package day03;
public class OopTest02 {
public static void main(String[] args) {
Human h = new Human();
System.out.println("life_span : " + h.life_span);
System.out.println("money : " + h.money);
h.fightTiger();
h.work();
System.out.println("life_span : " + h.life_span);
System.out.println("money : " + h.money);
}
}
from day03.ooptest03 import Wonbin
from day03.ooptest04 import Mavely
class YoungHun(Wonbin, Mavely):
def __init__(self):
Wonbin.__init__(self)
Mavely.__init__(self)
# super().__init__() # 이런 경우 Wonbin만 반복받기에 둘 다 상속 받을 수 있게 해줘야 함
if __name__ == '__main__':
yh = YoungHun()
print("weight",yh.weight)
print("ugly",yh.flag_ugly)
yh.eat(100)
yh.plastic()
print("weight",yh.weight)
print("ugly",yh.flag_ugly)
# 가위/바위/보를 선택하세요 가위
# 나:가위
# 컴:보
# 결과:이김
from random import random
me = input("가위/바위/보를 선택하세요 ")
com = random();
if com > 0.66:
com = "가위"
elif com > 0.33:
com = "바위"
else:
com = "보"
#print(com)
if (me == "가위" and com == "보")|(me == "바위" and com == "가위")|(me == "보" and com == "바위"):
result = "이김"
elif (me == "가위" and com == "바위")|(me == "바위" and com == "보")|(me == "보" and com == "가위"):
result = "짐"
else:
result = "비김"
print("나:{}".format(me))
print("컴:{}".format(com))
print("결과:{}".format(result))
# com = (1 ~ 99까지를 넣어줌) 50
# 수를 입력하시오 40
# 40 UP
# 수를 입력하시오 60
# 60 DW
# 수를 입력하시오 50
# 50 ANSWER
from random import random
com = int(random()*99)
#print(com)
while True:
su = int(input("수를 입력하시오 "))
if su > com:
print(str(su) + "\tDW")
elif su < com:
print(str(su) + "\tUP")
else:
print(str(su) + "\tANSWER")
break
# 첫별수를 입력하세요 3
# 끝별수를 입력하세요 5
# ★★★
# ★★★★
# ★★★★★
su1 = int(input("첫별수를 입력하세요 "))
su2 = int(input("끝별수를 입력하세요 "))
def getStar(cnt):
ret = ""
for i in range(cnt):
ret +="★"
return ret
for i in range(su1, su2):
print(getStar(i))