iT邦幫忙

1

30天程式語言研究

  • 分享至 

  • xImage
  •  

今天是30天程式語言研究的第十二天,研究的語言一樣是python,今天主要學習的是物件函式和繼承

網址:https://www.youtube.com/watch?v=zdMUJJKFdsU&ab_channel=GrandmaCan-%E6%88%91%E9%98%BF%E5%AC%A4%E9%83%BD%E6%9C%83

筆記:

物件函式

class Phone:
def init(self, os, number, is_waterproof): #初始函數
self.os = os
self.number = number
self.is_waterproof
def is_ios(self): #self代表物件本身
if seld.os == "ios":
return True
else:
return False
def add(self, number1, number2):
return number1+number2

phone1 = Phone("ios", 123, True)
print(phone1.is_ios()) #回傳true
print(phone1.add(5,6)) #11

繼承

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

def print_name(self):
    print(self.name)
def print_age(self):
    print(self.age)

class Student:
def init(self, name, age, school):
self.name = name
self.age = age
self.school = school

def print_name(self):
    print(self.name)
def print_age(self):
    print(self.age)
def print_school(self):
    print(self.school)

#內容很像可以學生繼承人 改class Student如下
from person import Person
class Student(Person):
#相當於複製一份Person的內容到個位置
def init(self, name, age, school):
self.name = name
self.age = age
self.school = school

def print_school(self):
    print(self.school)

#測試
from student import Student
student1 = Student("小白", 87, "小白國小")
student1.print_school()
student1.print_name()


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言