“Getters and Setters в Python” Ответ

Getters and Setters в Python

class Person:
	# Constructor
	def __init__(self, name, age):
    	# Since we making getters and setters, we should mangle the varible names to make them "private" by adding 2 underscores.
    	self.__name = name
        self.__age = age
    
    # Getters
	def get_name(self):
    	return __self.name
	
    def get_age(self):
    	return __self.age
    
    # Setters
    def set_name(self, new_name):
    	self.__name = new_name
    
    def set_age(self, new_age):
    	self.__age = new_age
Liam Welsh

Python Getters and Setters

class Car():
    def __init__(self):
        self._seat = None

    @property # getter
    def seat(self):
        return self._seat

    @seat.setter # setter
    def seat(self, seat):
        self._seat = seat


c = Car()
c.seat = 6
print(c.seat)
Wojak's distant cousin

Ответы похожие на “Getters and Setters в Python”

Вопросы похожие на “Getters and Setters в Python”

Больше похожих ответов на “Getters and Setters в Python” по Python

Смотреть популярные ответы по языку

Смотреть другие языки программирования