본문 바로가기

Python

Python 클래스, 객체, GUI

반응형

 파이썬 클래스, 객체 개념, 코드 연습


class Television:
    def __init__(self, channel, volume, on):
        self.channel = channel
        self.volume = volume
        self.on = on

    def show(self):
        print(self.channel,self.volume, self.on)

    def setChannel(self, channel):
        self.channel = channel

    def getChannel(self):
        return self.channel
t = Television(9, 10, True)
t.show()


t.setChannel(90)
t.show()

 

'''

    원을 클래스로 구현하시오.
    클래스 이름은 Circle로 한다.
        - 생성자
        - 반지름 (radius)
        - getArea() 원 넓이
        - getPrimeter() 원 둘레

    출력 예시:  (반지름이 10인 원의 면적과 원의 둘레를 출력하시오)
        원의 면적 :
        원의 둘레 :

'''
import math

class Circle():
    def __init__(self, radius = 0):
        self.radius = radius
    def getArea(self):
        return math.pi*self.radius*self.radius
    def getPrimeter(self):
        return math.pi*2*self.radius

c = Circle(10)
print("원의 면적 : ", c.getArea())
print("원의 둘레 : ", c.getPrimeter())

 

파이썬언어의 의미는 뱀이다. _를 많이 씀. 객체를 private으로 지정하고 싶으면 언더바를 두번 붙이면 된다 . __ 가 변수 앞에 붙으면 외부에서 변경 불가능하다. 

 

class Student:
    def __init__(self, name=None, age=0):
        self.__name = name      #__가 붙으면 외부에서 변경 불가
        self.__age = age

    def getName(self):
        return self.__name

    def setAge(self, age):
        self.__age = age

    def getAge(self):
        return self.__age

    def setName(self, name):
        self.__name = name

stu = Student("Admiral", 25)
print(stu.getName())
print(stu.getAge())

 

 

 


 파이썬 GUI : 


2. entry widget
    1) 사용자가 키보드로 입력한 내용을 전달하는 위젯

entry 객체가 1개, button 객체가 1개, label 객체가 1개. 총 3개의 객체를 생성했다. 

반응형