Skip to content

Commit 0e75e1b

Browse files
updated
1 parent 49466ff commit 0e75e1b

6 files changed

Lines changed: 62 additions & 4 deletions

File tree

46-python-oop/cars.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# DUNDER means double underscore __init__ = constructor method
1111

1212
# create a class
13-
class Car:
13+
class Car: # define class name
1414
# attributes
1515
def __init__(self, make, model, year, color): # constructor method
1616
self.make = make
@@ -20,7 +20,7 @@ def __init__(self, make, model, year, color): # constructor method
2020
self.mileage = 0
2121
self.for_sale = False
2222

23-
# define methods
23+
# define class methods inside the class
2424
def drive(self):
2525
print(f"The {self.make} is driving.")
2626

47-class-variables/image.png

772 KB
Loading

47-class-variables/main.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,26 @@
1-
# Class Variables
1+
# Class Variables. = Shared among all instances of a class.
2+
# Defined within the class constructor but outside any methods.
3+
# Allow you to share data among all objects created from the class.
4+
5+
class Student:
6+
7+
class_year = "2025" # Class variable
8+
number_of_students = 0 # Class variable
9+
10+
def __init__(self, name, age): # Constructor method
11+
self.name = name # Instance variable
12+
self.age = age # Instance variable
13+
# this is how you would access a class variable inside the class
14+
Student.number_of_students += 1 # Increment class variable each time a new student object/instance is created
15+
16+
# Note: You can also use self.number_of_students, but using the class name is preferred for clarity.
17+
Student1 = Student("SpongeBob", 15)
18+
Student2 = Student("Patrick", 16)
19+
Student3 = Student("Squidward", 17)
20+
Student4 = Student("Sandy", 16)
21+
22+
print(f"{Student1.name} is in the class of {Student1.class_year}.")
23+
print(f"{Student2.name} is in the class of {Student2.class_year}.")
24+
print(f"{Student3.name} is in the class of {Student3.class_year}.")
25+
print(f"{Student4.name} is in the class of {Student4.class_year}.")
26+
print(f"Total number of students: {Student.number_of_students}")

48-inheritance/animal-class.png

88.9 KB
Loading

48-inheritance/image.png

284 KB
Loading

48-inheritance/main.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,34 @@
1-
# Inheritance
1+
# Inheritance = Allows a class to inherit attributes and methods from another class.
2+
# Helps to reuse code and establish a relationship between classes.
3+
# The class that is inherited from is called the "parent" or "base" class.
4+
# The class that inherits is called the "child" or "derived" class.
5+
# Example:
6+
7+
# Parent class
8+
class Animal:
9+
# lets start a constructor
10+
def __init__(self, name):
11+
self.name = name
12+
self.is_alive = True
13+
def eat(self):
14+
return f"{self.name} is eating."
15+
def sleep(self):
16+
return f"{self.name} is sleeping."
17+
18+
class Dog(Animal): # Dog inherits from Animal
19+
pass
20+
21+
class Cat(Animal): # Cat inherits from Animal
22+
pass
23+
24+
class Mouse(Animal): # Mouse inherits from Animal
25+
pass
26+
27+
dog = Dog("Scooby")
28+
cat = Cat("Garfield")
29+
mouse = Mouse("Jerry")
30+
print(dog.name) # Output: Scooby
31+
print(dog.is_alive) # Output: True
32+
print(dog.eat()) # Output: Scooby is eating.
33+
print(cat.sleep()) # Output: Garfield is sleeping.
34+

0 commit comments

Comments
 (0)