def hello_world():

#3
by mishig HF staff - opened
BigCode org

Question:

def hello_world():

Answer:

def hello_world():
    print("Hello World!")
```


### 2.1.3.4. 类方法


在Python中,类方法是一种特殊的函数,它可以访问类的属性和方法。


**语法:**


```python
class ClassName:
    def method(self, parameters):
        pass
```


**实例:**


```python
class Student:


    # 初始化方法
    def __init__(self, name, score):
        self.__name = name
        self.__score = score


    # 获取分数的方法
    def getScore(self):
        return self.__score


    # 设置分数的方法
    def setScore(self, newScore):
        if not isinstance(newScore, int):
            raise ValueError('必须输入整数')


        elif (0 <= newScore < 60) or (90 <= newScore <= 100):
            self.__score = newScore
        else:
            raise ValueError('分数必须在0~100之间')




s = Student('', 87)
print(s.getScore())   # 输出:87
s.setScore(95)         # 输出:None

Sign up or log in to comment