为什么Ruby设置者需要“Self”班里的资格?
(c)attr_accessor
self.
self
/this
self
/this
self
/this
foo.x = y
, y = foo.x
class A def qwerty; @q; end # manual getter def qwerty=(value); @q = value; end # manual setter, but attr_accessor is same def asdf; self.qwerty = 4; end # "self." is necessary in ruby? def xxx; asdf; end # we can invoke nonsetters w/o "self." def dump; puts "qwerty = #{qwerty}"; endenda = A.new a.xxx a.dump
self.qwerty =()
using System;public class A { public A() {} int q; public int qwerty { get { return q; } set { q = value; } } public void asdf() { qwerty = 4; } // C# setters work w/o "this." public void xxx() { asdf(); } // are just like other methods public void dump() { Console.WriteLine("qwerty = {0}", qwerty); }}public class Test { public static void Main() { A a = new A(); a.xxx(); a.dump(); }}
using System;public class A { public A() {} public int test { get { return 4; }} public int useVariable() { int test = 5; return test; } public int useMethod() { int test = 5; return this.test; }}public class Test { public static void Main() { A a = new A(); Console.WriteLine("{0}", a.useVariable()); // prints 5 Console.WriteLine("{0}", a.useMethod()); // prints 4 }}
有
不
qwerty = 4
浮云间
慕斯王
相关分类