标签: setter

安装者和吸气剂的模板

我不熟悉模板,但我想知道,是否可以将它们用于setter和getter方法.例如在这种情况下:

double exmlClass::getA(void) const
{
    return a_;
}



void exmlClass::setA(const double& a)
{
    a_ = a;
}



double exmlClass::getB(void) const
{
    return b_;
}
Run Code Online (Sandbox Code Playgroud)

如您所见,方法几乎相同,除了它们引用另一个私有变量(a_,b_,c_).是否有更优雅的方式来编写这些函数,或者在这种情况下通常如上所述?如果它常用于使用模板,我将很感激您将如何在上面的代码中使用它们.

我要问的另一个问题是如何恰当地宣布吸气剂和二传手.编码风格好吗?

double getA(void) const;
void setA(const double& a);

double getB(void) const;
void setB(const double& b);

double getC(void) const;
void setC(const double& c);
Run Code Online (Sandbox Code Playgroud)

我的意思是应该getter总是const和setter作为对象的参数引用,而不是复制它,这可能会慢一点?

c++ getter setter templates

8
推荐指数
1
解决办法
8332
查看次数

将Javascript getters/setter复制到另一个原型对象

// Base class
var Base = function() {
    this._value = 'base';
};
Base.prototype = {
    constructor: Base,
    // By function
    getValue: function() {
        return this._value;
    },
    // By getter
    get value() {
        return this._value;
    }
};

// Sub class extends Base
var Sub = function() {
    this._value = 'sub';
};
Sub.prototype = {
    constructor: Sub
};
// Pass over methods
Sub.prototype.getValue = Base.prototype.getValue;
Sub.prototype.value = Base.prototype.value;

// ---

var mySub = new Sub();
alert(mySub.getValue()); // Returns 'sub'
alert(mySub.value);      // Returns …
Run Code Online (Sandbox Code Playgroud)

javascript getter setter extending prototype-programming

8
推荐指数
3
解决办法
5489
查看次数

如果在C++ setter中使用它是否重要?

假设我有一个带有私有变量x的c ++类.对于它的制定者,使用有什么区别this吗?是否有可能出现意外/意外行为this?我不使用?

二传手:

void setX(double input)
{
   x = input;
}
Run Code Online (Sandbox Code Playgroud)

二传手使用this:

void setX(double x)
{
   this->x = x;
}
Run Code Online (Sandbox Code Playgroud)

c++ setter this

8
推荐指数
2
解决办法
193
查看次数

php自动setter和getter

我正在尝试为php对象实现一些自动getter和setter.

我的目标是自动为每个属性的方法getProperty()setProperty(value),这样,如果没有为脚本只是设置一个属性来实现或获得的价值的方法.

一个例子,让自己清楚:

class Foo {
    public $Bar;
}

$A = new A();
$A->setBar("bar");
$A->getBar(); // -> output "bar"
Run Code Online (Sandbox Code Playgroud)

要么

class Foo {
    public $Bar;
    public function setBar($bar) { $Bar = $bar; }
    public function getBar($bar) { return 'the value is: ' . $bar; }
}

$A = new A();
$A->setBar("bar");
$A->getBar(); // -> output "the value is: bar"
Run Code Online (Sandbox Code Playgroud)

关于如何实现这一点的任何想法/提示?

php oop getter setter

8
推荐指数
1
解决办法
9279
查看次数

C#速记吸气剂和二传手

C#中的Setter和Getters如何实现封装?对于这些setter和getter,我不是新手,我有编程背景,特别是java.在java中你使用像这样的setter和getter

public class Person {
    private String fName;

    public void setName(String someName) {
        fName = someName;
    }

    public String getName() {
        return fName;
    }
}

public class Test {

    public static void main(String[] args) {
        Person p = new Person();

        p.setName("Bob");
        System.out.println(p.getName());
    }
}
Run Code Online (Sandbox Code Playgroud)

而在C#中使用速记

public class Person {
    public string fName{ get; set;}
}
Run Code Online (Sandbox Code Playgroud)

C#速记getter和setter如何实现封装?我如何实现与上面的java代码相同的C#代码?对它有任何限制吗?根据我的观察,我只能使用"fName",如果它设置为public,特别是"public string fName {get; set;}",但是当涉及到private时我不能.但是当我将它设置为私有时,我无法再以其他方式访问它.

c# java getter setter getter-setter

8
推荐指数
2
解决办法
2万
查看次数

流畅的Nhibernate和Sql Server私有setter错误

我正在尝试用流畅的nhibernate和sql server express完成第一步,在我的项目中添加这些功能.但是有些错误.因为我是nhibernate的新手,一段时间无法解决问题.我有一个实体

Id { get; private set; }
Run Code Online (Sandbox Code Playgroud)

存取.并且此实体映射到sql server中具有标识{1,1}列的表.但在创建Session工厂期间,我收到一个错误:

The following types may not be used as proxies:Entity: method set_Id should be 'public/protected virtual' or 'protected internal virtual'
Run Code Online (Sandbox Code Playgroud)

我知道私有的setter用于封装这个属性的设置,但为什么我会得到这个错误呢?PS:关于nhibernate的流利网站的例子是使用sqllite db,一切都很好.

setter compiler-errors fluent-nhibernate sql-server-2008

8
推荐指数
2
解决办法
1547
查看次数

objective-c中copy属性的自定义setter

我是否必须使用objective-c中的copy属性手动复制自定义setter中的对象?例如,

我有一个属性:

@property (copy, nonatomic) NSString *someString;
Run Code Online (Sandbox Code Playgroud)

和自定义setter:

- (void)setSomeString:(NSString *)someString
{
  //option 1
  _someString = someString;
  //option 2
  _someString = [someString copy];
  //do other stuff
}
Run Code Online (Sandbox Code Playgroud)

是自定义setter的选项1还是我必须使用选项2才能复制对象?

setter properties objective-c ios

8
推荐指数
1
解决办法
2759
查看次数

没有setter的属性的TypeScript接口签名.

我们在其中一个课程中有一个典型的getter,比方说

class Employee implements IEmployee {
    private _fullName: string;

    get fullName(): string {
        return this._fullName;
    }
}
Run Code Online (Sandbox Code Playgroud)

以及使用它的界面

interface IEmployee{
     fullName: string; 
}
Run Code Online (Sandbox Code Playgroud)

当通过此接口使用实例时,如果我们尝试分配给fullName,编译器将不会警告我们没有setter,并且JS运行时只是吞下任何赋值并且不会抛出错误.有没有办法将接口成员标记为只有getter或者只有setter?

我看过这篇文章,但它已经很老了,我想知道,如果有什么改进的话.

getter setter properties interface typescript

8
推荐指数
1
解决办法
8533
查看次数

Java-针对不同变量的getter,single getter或multiple的最佳实践?

我相对较新的Android编程(约2个月)是否有必要为几十个不同的变量获取?

例如 -

//Yes I realise that this isn't 'dozens'
public float getX() {
    return position.x;
}

public float getY() {
    return position.y;
}

public float getWidth() {
    return width;
}

public float getHeight() {
    return height;
}

public float getRotation() {
    return rotation;
}
Run Code Online (Sandbox Code Playgroud)

虽然有必要为浮点数和字符串设置不同的getter和setter,但这是不好的做法,如果是这样,为什么要使用类似switch语句的东西来返回不同的变量?

public float returnSomething(String theThing) {
    switch (theThing) {
        case "height":
            return height;
        case "rotation" :
            return  rotation;
        case "width":
            return  width;

        default:
            return 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

那么,上面的代码被认为是坏的吗?如果是这样,请解释原因.

感谢任何帮助,这不是一个真正的问题,因为无论哪种方式都运行正常,我只是不明白为什么人们会使用几十个getter和setter如果没有充分的理由.

我想同样的问题适用于制定者

java getter setter android

8
推荐指数
1
解决办法
2552
查看次数

字段getter/setter与基类中的表达式树

继例子这篇文章它的后续问题,我试图创建一个使用编译表达式场getter/setter方法.

getter工作得很好,但是我被困在了setter中,因为我需要setter来分配任何类型的字段.

在这里我的setter-action builder:

public static Action<T1, T2> GetFieldSetter<T1, T2>(this FieldInfo fieldInfo) {
  if (typeof(T1) != fieldInfo.DeclaringType && !typeof(T1).IsSubclassOf(fieldInfo.DeclaringType)) {
    throw new ArgumentException();
  }
  ParameterExpression targetExp = Expression.Parameter(typeof(T1), "target");
  ParameterExpression valueExp = Expression.Parameter(typeof(T2), "value");
  //
  // Expression.Property can be used here as well
  MemberExpression fieldExp = Expression.Field(targetExp, fieldInfo);
  BinaryExpression assignExp = Expression.Assign(fieldExp, valueExp);
  //
  return Expression.Lambda<Action<T1, T2>> (assignExp, targetExp, valueExp).Compile();
}
Run Code Online (Sandbox Code Playgroud)

现在,我将通用setter存储到缓存列表中(当然,每次构建setter都是性能杀手),我将它们转换为简单的"对象":

 // initialization of the setters dictionary
 Dictionary<string, object> setters = new Dictionary(string, object)();
 Dictionary<string, …
Run Code Online (Sandbox Code Playgroud)

c# reflection setter expression-trees fieldinfo

8
推荐指数
1
解决办法
779
查看次数