encapsulation example 1

This commit is contained in:
Mert Gör 🇹🇷 2023-07-15 00:25:02 +03:00
parent 2a2ed48501
commit ee4300f2e4
No known key found for this signature in database
GPG key ID: 2100A876D55B39B9
2 changed files with 32 additions and 0 deletions

View file

@ -0,0 +1,16 @@
class Sample:
def __init__(self, a):
self._a = a
def disp(self):
print(self._a)
def __foo(self):
print('Sample.__foo')
s = Sample(10)
s.disp()
s._a = 20 # kötü teknik, _ ile başlatılan değişkenler dışarıdan kullanılmamalıdır
s._Sample__foo() #
#s.__foo() # exception!

View file

@ -0,0 +1,16 @@
class Sample:
def __init__(self, a):
self._a = a
def disp(self):
print(self._a)
def __foo(self):
print('Sample.__foo')
s = Sample(10)
s.disp()
s._a = 20 # kötü teknik, _ ile başlatılan değişkenler dışarıdan kullanılmamalıdır
s._Sample__foo() #
s.__foo() # exception!