Object-oriented Programming

decal·2023년 1월 12일
0

study notes

목록 보기
10/12

OOP(Object-oriented programming)

  • programming style where we bundle together related data and functionality
  • we use it when we want to group data and functionality inside an object

inheritance

  • lets us create classes that have different properties and behaviors without coding each one from scratch.
class Parent:
  def __init__(self):
    self.eyes = "green"

class Child(Parent):
  def __init__(self):
    super().__init__()
    self.age = 7

child = Child()
print(child.eyes)
print(child.age)

Output:

green
7

class Greetings:
  def greet(self):
    print("Hi!")

class Person(Greetings):
  name = "George"

p = Person()
p.greet()

Output:

Hi!

class Car: 
  def start_car(self): 
    print("Starting car") 
 
class Hybrid(Car): 
  def charge(self): 
    print("Using fuel to charge battery") 
 
class Electric(Car): 
  def fuel(self): 
    print("No fuel necessary") 
 
prius = Hybrid() 
electric = Electric() 
 
prius.start_car() 
electric.start_car() 
 
prius.charge() 
electric.fuel()

Output:

Starting car 
Starting car 
Using fuel to charge battery 
No fuel necessary

class Dog:
  def bark(self):
    print("Woof!")

class Cat:
  def meow(self):
    print("Meow!")

pet1 = Cat()
pet2 = ()

pet1.meow();
pet2.bark();

Output:

Meow! 
Woof!

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age
  def greet(self):
     print("Hi!")

class Nurse(Person):
  def __init__(self, name, age):
    super().__init__("Nurse " + name, age)
  def intro(self):
    print("Hi, I'm", self.name)

person1 = Nurse("Sam", 23)
person2 = Person("Tom", 30)

person1.intro()
person2.greet()

Output:

Hi, I'm Nurse Sam
Hi!

Making a subclass:

super().init(name, age)

to use existing constructors to set the same properties

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def greet(self):
    print("Hi!")

class Student(Person):
  def __init__(self, name, age, major):
    super().__init__(name, age)
    self.major = major

student = Student("Sam", 23, "chemistry")
print()
student.greet()

Output:

chemistry
Hi!

class Phone:
  def call(self, contact):
    print("Calling " + contact)

phone = Phone()

phone.call("Mom")

Output:

Calling Mom

abstraction

  • hiding details (low-level functionality)
  • allows other developers to use a class without having to know what low-level methods it has or how they even work
class Car:
  def __init__(self):
    self.on = False

  def injectFuel(self):
    print('Spraying fuel')

  def igniteFuel(self):
    print('Boom!')

  def startUp(self):
    self.on = True
    while self.on:
      self.injectFuel()
      self.igniteFuel()

class IceCreamMaker:
  def churn(self):
   print('Churning cream')

  def freeze(self):
   print('Freezing cream')

  def makeIceCream(self):
    self.churn()
    self.freeze()

iceCreamMaker = IceCreamMaker()
iceCreamMaker.makeIceCream()

Output:

Churning cream 
Freezing cream

class Slideshow: 
  def __init__(self, slides): 
    self.slides = slides 
    self.current = 1 
  
  def viewNextSlide(self): 
    self.current += 1 
  
  def play(self): 
    while self.current <= self.slides: 
      print('Slide', self.current) 
      self.viewNextSlide() 
 
slideshow = Slideshow(5) 
slideshow.play()

Output:

Slide 1
Slide 2
Slide 3
Slide 4
Slide 5

Methods represent behaviors, for example displaying text in the console like the following:

class Feline:
  def speak(self):
    print("Meow")

class Cat(Feline):
  def lick(self):
    print("Licking paw")

cat = Cat()
cat.speak()

Output:

Meow

polymorphism

  • for different behavior depending on which class

A subclass can override the methods it inherits from its superclass. We simply set the same name method on the subclass.

class Feline:
  def speak(self):
    print("Meow")

class Cat(Feline):
  def lick(self):
    print("Licking paw")

class Lion(Feline):
  def prey(self):
    print("Pounces on prey")
  def speak(self):
    print("ROAR!")
    
cat = Cat()
cat.speak()
lion = Lion()
lion.speak()

Output:

Meow
ROAR!

0개의 댓글