Setter and Getter

题目:

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

A private attribute name of type string.
A setter method setName which expect a parameter name of type string.
A getter method getName which expect no parameter and return the name of the school.

分析:

这个题目是个基本训练,关于java中static关键字的含义:http://lavasoft.blog.51cto.com/62575/18771

解法:

public class School {
    /*
     * Declare a private attribute *name* of type string.
     */
    // write your code here
    private String name;

    /**
     * Declare a setter method `setName` which expect a parameter *name*.
     */
    // write your code here
    public void setName(String val) {
        name = val;
    }
    /**
     * Declare a getter method `getName` which expect no parameter and return
     * the name of this school
     */
    // write your code here
    public String getName() {
        return name;
    }
}

results matching ""

    No results matching ""