ข้ามไปที่เนื้อหาหลัก

OOP W

python object oriented 

Student Class

Attributes

Student ID

First name

Last name

Credits

GPA

Behaviors

Enroll()

Take_examination()



class Student:

    def __init__(self, student_id, first_name, last_name):

        self.student_id = student_id

        self.first_name = first_name

        self.last_name = last_name

        self.credits = 0

        self.GPA = 0.0


    def enroll(self, credits):

        self.credits += credits


    def take_examination(self, grade_points):

        # Perform examination logic and calculate GPA based on grades

        # For simplicity, let's assume grade_points are added to GPA directly

        total_points = self.GPA * self.credits

        self.credits += 1  # Assuming 1 credit for the examination

        total_points += grade_points

        self.GPA = total_points / self.credits


# Example usage:

student1 = Student("001", "John", "Doe")

student1.enroll(3)  # Enroll in 3 credits

student1.take_examination(4)  # Take an examination with grade points 4


print(f"{student1.first_name} {student1.last_name} - Student ID: {student1.student_id}")

print(f"Credits: {student1.credits}")

print(f"GPA: {student1.GPA}")


2. 

class Student:

    def __init__(self, student_id, first_name, last_name, credits=0, gpa=0.0):

        self.student_id = student_id

        self.first_name = first_name

        self.last_name = last_name

        self.credits = credits

        self.gpa = gpa


    def enroll(self, course_credits):

        self.credits += course_credits


    def take_examination(self, grade_points):

        # Calculate GPA based on the new grade

        total_grade_points = self.gpa * self.credits

        total_grade_points += grade_points

        self.credits += 1  # Assuming one course credit for the exam

        self.gpa = total_grade_points / self.credits


    def display_info(self):

        print(f"Student ID: {self.student_id}")

        print(f"Name: {self.first_name} {self.last_name}")

        print(f"Credits: {self.credits}")

        print(f"GPA: {self.gpa:.2f}")



# Example usage:

student1 = Student(1, "John", "Doe")

student1.enroll(3)  # Enroll in courses worth 3 credits

student1.take_examination(4.0)  # Grade points earned in an exam


student1.display_info()  # Display student information







# Encapsulation example

class Car:

    def __init__(self, brand, mileage):

        self.__brand = brand  # Private attribute

        self.mileage = mileage


    def get_brand(self):

        return self.__brand  # Accessing private attribute through a method


    def set_brand(self, new_brand):

        self.__brand = new_brand  # Modifying private attribute through a method



# Inheritance example

class ElectricCar(Car):  # Inheriting from the Car class

    def __init__(self, brand, mileage, battery_capacity):

        super().__init__(brand, mileage)  # Calling the base class constructor

        self.battery_capacity = battery_capacity


    def display_info(self):

        print(f"Brand: {self.get_brand()}")  # Accessing the base class method

        print(f"Mileage: {self.mileage}")

        print(f"Battery Capacity: {self.battery_capacity}")



# Polymorphism example

class Pet:

    def make_sound(self):

        pass  # Abstract method


class Dog(Pet):

    def make_sound(self):

        print("Woof!")


class Cat(Pet):

    def make_sound(self):

        print("Meow!")


# Example usage

# Encapsulation

car = Car("Toyota", 50000)

print(car.get_brand())  # Accessing private attribute through a method

car.set_brand("Honda")  # Modifying private attribute through a method

print(car.get_brand())


# Inheritance

electric_car = ElectricCar("Tesla", 20000, "75 kWh")

electric_car.display_info()


# Polymorphism

pets = [Dog(), Cat()]

for pet in pets:

    pet.make_sound()  # Calling the overridden method





Encapsulation: Using private attributes and public methods to control access to the class members.

Inheritance: Creating a derived class that inherits attributes and methods from a base class.

Polymorphism: Implementing a method in the derived class that overrides a method in the base class.


ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

OOP 22023

 https://github.com/omiejung01/PythonOO2_2023/tree/master01 https://github.com/omiejung01/PythonOO2_2023/tree/master02 Master 1 PythonOO2_2023/walletapp/data.py class Account:     def __init__(self,account_name,opening_balance):         self.account_name = account_name         self.balance = opening_balance     #Constructor     def display(self):         # Balance Inquiry         return self.account_name + ' ' + f"{self.balance:,.2f}"     def setAccountName(self, new_name):         self.account_name = new_name class Character:     def __init__(self, character_name, vision, weapon_type):         self.__character_name = character_name         self.__vision = vision         self.__weapon = None         self.__weapon_type = weapon_type     def displa...

Web Class note

https://github.com/omiejung01/Nuxt3WD2_2023/tree/master02 https://docs.google.com/document/d/1qikYS-WSpRosjIlUZEoOhzguigWx2-Mqamfyxcs67aE/edit Web Design https://drive.google.com/drive/folders/1kJvORymDwvcDtGoliUOHifsza3CdWsmH DBM Course https://drive.google.com/drive/folders/119ZWerKRLYNwf7G7dEA7ZKcBx9rOQoqV Web Design - Class note 13 Nov 2023 Course Syllabus Please go to https://tinyurl.com/webdesign002 And go to slides at  https://tinyurl.com/dbmcourse001 Slide: 01-How computer works Slide: 02-Programming Languages Slide #11: Try "Create game using PHP" Change PHP to C++ Change PHP to Unity Let's student try some queries <Solution> + <Languages Tools> Answer in “Student Interactive Board” Google Doc for your ATTN Todo: Check Visual Studio Code in your machine Goto Search Box type → VS Code Create new file Save file as hello.html Hello World in HTML Hello World in JavaScript Next Class - Slide: 03-Coding 101 Class: 20 Nov 2023 Slide: 03-Coding 101 Slide #19 ...