-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
55df9df
commit 44f5e3d
Showing
7 changed files
with
154 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
def display(message, is_warning=False): | ||
if is_warning: | ||
print("Warning!!") | ||
print(message) | ||
|
||
|
||
def foo(): | ||
return "bar" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Parent class | ||
class Animal: | ||
def __init__(self, name): | ||
print(f"Initializing animal named {name}") | ||
self.name = name | ||
|
||
def speak(self): | ||
pass | ||
|
||
|
||
# Child class inheriting from Animal | ||
class Dog(Animal): | ||
def speak(self): | ||
return f"{self.name} says woof!" | ||
|
||
|
||
class Cat(Animal): | ||
def __init__(self, name, race): | ||
super().__init__(name) | ||
self.race = race | ||
print(f"Initializing cat named {self.name} of {self.race} race") | ||
|
||
def speak(self): | ||
return f"{self.name} says meong!" | ||
|
||
|
||
my_dog = Dog("Buddy") | ||
print(my_dog.speak()) # Calls the speak method of the Dog class | ||
|
||
my_cat = Cat("Ocong", "Persian") | ||
print(my_cat.speak()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
class Loggable: | ||
def __init__(self): | ||
self.title = "" | ||
|
||
def log(self): | ||
print("Log message from " + self.title) | ||
|
||
|
||
class Connection: | ||
def __init__(self): | ||
self.server = "" | ||
|
||
def connect(self): | ||
print("Connecting to database on " + self.server) | ||
|
||
|
||
class SqlDatabase(Connection, Loggable): | ||
def __init__(self): | ||
super().__init__() | ||
self.title = "Sql Connection Demo" | ||
self.server = "Some_Server" | ||
|
||
def connect(self): | ||
print("Connecting to SQL database on " + self.server) | ||
|
||
|
||
def framework(item): | ||
if isinstance(item, Connection): | ||
item.connect() | ||
if isinstance(item, Loggable): | ||
item.log() | ||
|
||
|
||
sql_connection = SqlDatabase() | ||
framework(sql_connection) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from helpers import display as helpers_display | ||
|
||
helpers_display("Not a warning") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
"name","country","city","skills" | ||
"Aurora","Indonesia","Palembang","Python" | ||
"Jokowi","Indonesia","Solo","C++" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
# OOP Exercises | ||
|
||
Create a class called `PersonAccount`. It has `firstname`, `lastname`, `incomes`, and `expenses` properties. It also has `total_income()`, `total_expense()`, `account_info()`, `add_income()`, `add_expense()` and `account_balance()` methods. Incomes is a dict of income amount and its description. The same goes for expenses. | ||
Create a class called `PersonAccount`. It has `firstname`, `lastname`, `incomes`, and `expenses` properties. It also has `total_income()`, `total_expense()`, `account_info()`, `add_income()`, `add_expense()` and `account_balance()` methods. Incomes is a set of income amount and its description. The same goes for expenses. | ||
|
||
Reference: | ||
|
||
1. [Classes](./classes.md) | ||
2. [Sets](../python_fundamentals_1/sets.ipynb) | ||
3. [Student Grade Program](../../tutorials/student_grade_with_oo.ipynb) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters