Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

refactor(encapsulation-3.py): Add encapsulation and applies srp#2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
RotRotAl wants to merge1 commit intoShikha-code36:main
base:main
Choose a base branch
Loading
fromRotRotAl:patch-2
Open
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions06.Encapsulation/encapsulation-3.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -27,31 +27,33 @@ def set_val(self, val):
self.val = val

def get_val(self):
print(self.val)
returnself.val

def increment_val(self):
self.val = self.val + 1
print(self.val)
self.set_val(self.get_val()+1)


a = MyInteger()
a.set_val(10)
a.get_val()
print(a.get_val())
a.increment_val()
print(a.get_val())
print("Before Break")

# Trying to break encapsulation in a new instance with an int
c = MyInteger()
c.val = 15
c.get_val()
print(c.get_val())
c.increment_val()
print(c.get_val())
print("After Break")

# Trying to break encapsulation in a new instance with a str
b = MyInteger()
b.val = "MyString" # <== Breaking encapsulation, works fine
b.get_val() # <== Prints the val set by breaking encap
print(b.get_val()) # <== Prints the val set by breaking encap
b.increment_val() # This will fail, since str + int wont work
print(b.get_val())
print("Changing DataType")
'''
O/P-
Expand All@@ -65,4 +67,4 @@ def increment_val(self):
Traceback (most recent call last):
self.val = self.val + 1
TypeError: can only concatenate str (not "int") to str
'''
'''

[8]ページ先頭

©2009-2025 Movatter.jp