“Python sollte ich Getter und Setter verwenden” 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 sollte ich Getter und Setter verwenden

class Protective(object):
    """protected property demo"""
    #
    def __init__(self, start_protected_value=0):
        self.protected_value = start_protected_value
    # 
    @property
    def protected_value(self):
        return self._protected_value
    #
    @protected_value.setter
    def protected_value(self, value):
        if value != int(value):
            raise TypeError("protected_value must be an integer")
        if 0 <= value <= 100:
            self._protected_value = int(value)
        else:
            raise ValueError("protected_value must be " +
                             "between 0 and 100 inclusive")
    #
    @protected_value.deleter
    def protected_value(self):
        raise AttributeError("do not delete, protected_value can be set to 0")
Combative Camel

Getters und Setter

For IntelliJ IDEA TO generate getters and setters:
Refactor-->EncapsulatFields 
OR
use Keyboard Shortcut: alt + insert
AL momani

Ähnliche Antworten wie “Python sollte ich Getter und Setter verwenden”

Fragen ähnlich wie “Python sollte ich Getter und Setter verwenden”

Weitere verwandte Antworten zu “Python sollte ich Getter und Setter verwenden” auf Python

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen