12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
ADADADADAD
编程知识 时间:2024-12-03 15:18:43
作者:文/会员上传
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
在Python中,类是一种数据结构,用来封装数据和行为。类定义了对象的属性和方法,可以创建多个具有相同属性和方法的对象实例。类的用法包括以下几个方面:定义类:使用关键字class来
以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。
在Python中,类是一种数据结构,用来封装数据和行为。类定义了对象的属性和方法,可以创建多个具有相同属性和方法的对象实例。类的用法包括以下几个方面:
class Person:def __init__(self, name, age):self.name = nameself.age = agedef greet(self):print("Hello, my name is", self.name)person1 = Person("Alice", 25)person1.greet()
person2 = Person("Bob", 30)person2.greet()
print(person1.name)person1.greet()
class Student(Person):def __init__(self, name, age, student_id):super().__init__(name, age)self.student_id = student_iddef study(self):print("I am studying")student1 = Student("Alice", 25, 12345)student1.greet()student1.study()
class BankAccount:def __init__(self, balance):self.__balance = balancedef deposit(self, amount):self.__balance += amountdef withdraw(self, amount):if amount <= self.__balance:self.__balance -= amountelse:print("Insufficient balance")account1 = BankAccount(1000)account1.deposit(500)account1.withdraw(200)
11-20
11-19
11-20
11-20
11-20
11-19
11-20
11-20
11-19
11-20
11-19
11-19
11-19
11-19
11-19
11-19