“Getters and Setters in Python” Kode Jawaban

Getters and Setters in 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

Getters dan setter Python

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

Jawaban yang mirip dengan “Getters and Setters in Python”

Pertanyaan yang mirip dengan “Getters and Setters in Python”

Lebih banyak jawaban terkait untuk “Getters and Setters in Python” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya