“Getters und Setter in Python” Code-Antworten

Getters und Setter 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

Python Getter und 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

Ähnliche Antworten wie “Getters und Setter in Python”

Fragen ähnlich wie “Getters und Setter in Python”

Weitere verwandte Antworten zu “Getters und Setter in Python” auf Python

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen