Skip to content

Property declaration

irxground edited this page Sep 9, 2013 · 1 revision

静的型付け言語

C#

public class User {
  private string _Name;

  public string Name {
    get { return _Name; }
    protected set { _Name = value; }
  }
}

Scala

class User {
  private var _name: String = _

  def name = _name
  protected def name_= (value: String) {
    _name = value
  }
}

F#

type User() =
  let mutable name = ""

  member this.Name = name
  member private this.Name
    with set (value) = name <- value

動的型付け言語

Ruby

class User
  def name
    @name
  end

protected
  def name=(value)
    @name = value
  end
end

Python

class PropertyTest1(object):
    def __init__(self):
        self._x = None
    def get_x(self):
        return self._x
    def set_x(self, value):
        self._x = value
    x = property(get_x, set_x)

_sidebar

Clone this wiki locally