222. Getter与Setter

Description

Implement a class School, including the following attributes and methods:

  1. A private attribute name of type string.
  2. A setter method setName which expect a parameter name of type string.
  3. A getter method getName which expect no parameter and return the name of the school.
    Have you met this question in a real interview?

Example

Java:

    School school = new School();
    school.setName("MIT");
    school.getName(); // should return "MIT" as a result.

Python:

    school = School();
    school.setName("MIT")
    school.getName() # should return "MIT" as a result.
public class School {
    /*
     * Declare a private attribute *name* of type string.
     */
    // write your code here

    /**
     * Declare a setter method `setName` which expect a parameter *name*.
     */
    // write your code here

    /**
     * Declare a getter method `getName` which expect no parameter and return
     * the name of this school
     */
    // write your code here
    private String name;
    
    public  void setName(String name){
        this.name = name;
    }
    
    public String getName(){
        return this.name;
    }
     
    
}
描述
实现一个School的类,包含下面的这些属性和方法:

一个string类型的私有成员name.
一个setter方法setName,包含一个参数name.
一个getter方法getName,返回该对象的name。

猜你喜欢

转载自www.cnblogs.com/browselife/p/10645711.html