标签: setter

c类中的类的getter和setter

假设我们有一个带有属性和getter/setter的类InnerClass.我们还有一个包含InnerClass的类OuterClass.

例如

class InnerClass
{
    private int m_a;
    private int m_b;

    public int M_A
    {
        get
        {
             return m_a;
        }
        set
        {
             m_a = value;
        }
     }
}

class OuterClass
{
    private InnerClass innerClass
}
Run Code Online (Sandbox Code Playgroud)

我如何为OuterClass的innerClass成员实现正确的getter和setter?

提前致谢!

c# getter setter class member

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

如何使Style.Triggers触发要应用的不同命名样式

可以说我有以下内容:

<Style TargetType="{x:Type TextBox}">
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="BorderBrush" Value="Gray" />
    <Style.Triggers>
        <Trigger Property="IsFocused" Value="true"> 
            <Setter Property="BorderBrush" Value="Green" />
            <Setter Property="BorderThickness" Value="2" />
        </Trigger>
    </Style.Triggers> 
</Style>
Run Code Online (Sandbox Code Playgroud)

这很好用,这里没有太多错误,但这是一个相当简单的案例.如果我想将IsFocused风格状态列为exsplicit风格,如何将该风格引用为IsFocused风格,即

<Style x:key="ActiveStyle" TargetType="{x:Type TextBox}">
    <Setter Property="BorderBrush" Value="Green" />
    <Setter Property="BorderThickness" Value="2" />
</Style>

<Style TargetType="{x:Type TextBox}">
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="BorderBrush" Value="Gray" />
    <Style.Triggers>
        <Trigger Property="IsFocused" Value="true">
           -- Here I want to reference ActiveStyle and not copy the copy the setters
        </Trigger>
    </Style.Triggers> 
</Style>
Run Code Online (Sandbox Code Playgroud)

wpf setter triggers styles

14
推荐指数
3
解决办法
3万
查看次数

c#setter中的堆栈溢出异常

这很容易解释:这很有效

using System;
using ConstraintSet = System.Collections.Generic.Dictionary<System.String, double>;

namespace ConsoleApplication2
{
    class test
    {
        public ConstraintSet a { get; set; }
        public test()
        {
            a = new ConstraintSet();
        }
        static void Main(string[] args)
        {
            test abc = new test();
            Console.WriteLine("done");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这不

using System;
using ConstraintSet = System.Collections.Generic.Dictionary<System.String, double>;

namespace ConsoleApplication2
{
    class test
    {
        public ConstraintSet a { get { return a; } set { a = value; } }
        public test()
        {
            a = new ConstraintSet(); …
Run Code Online (Sandbox Code Playgroud)

c# stack-overflow setter callstack

14
推荐指数
3
解决办法
8133
查看次数

在WPF中使用Setter的EventTrigger?

我在WPF窗口中有一个普通的Button和TextBox,我想要一个Button的模板,一个EventTrigger监听Button.Click,然后设置TextBox的boolean属性.没有代码隐藏.

像这样的东西:

<ControlTemplate.Triggers>
  <EventTrigger SourceName="MyButton" RoutedEvent="Button.Click">
    <Setter TargetName="MyTextBox" Property="Focusable" Value="False" />
  </EventTrigger>
Run Code Online (Sandbox Code Playgroud)

wpf setter templates triggers

14
推荐指数
1
解决办法
3万
查看次数

春天autowiring setter /构造函数PROs和CONs

使用@Autowired(不是xml配置)时,是否有人可以比较set/constructor绑定的优缺点?

请参阅以下示例:

public class Example{
   private Logger log;
   // constructor wiring
   @Autowired 
   public Example(Logger log){
      this.log = log;
   }
}

public class Example{
   // setter wiring
   @Autowired 
   private Logger log;
}
Run Code Online (Sandbox Code Playgroud)

setter spring constructor autowired

14
推荐指数
2
解决办法
3826
查看次数

Java中的setter约定(返回void或this)

我已经写了近一年的Java了,我已经看到了两种不同的约定,用于人们如何实现他们的setter.

为了说明这一点,以下是两种惯例的示例.(我也很想知道这两个模式的简洁名称)

使用第一个约定的类,不返回其"set"方法.像这样:

public class Classic{
    private double _x;
    private double _y;
    public Classic(){
        x = 0;
        y = 0;
    }
    public void setX(double d){//or boolean with a type check on input
        x = d;
    }
    public void sety(double d){
        y = d;
    }
}
Run Code Online (Sandbox Code Playgroud)

使用替代约定的类从其setter方法返回.像这样:

public class Alternative{
    private double _x;
    private double _y;
    public Alternative(){
        x = 0;
        y = 0;
    }
    public Alternative setX(double d){
        x = d;
        return(this);
    }
    public Alternative sety(double d){
        y = d; …
Run Code Online (Sandbox Code Playgroud)

java setter coding-style

14
推荐指数
1
解决办法
4475
查看次数

什么是ES6类getter和setter呢?

什么是ES6类定义中的getter和setter方法?他们是原型道具吗?为了考试:

class Person{
  constructor(){};
  get name(){
    return 'jack';
  }
  set name(){
    // ???
  }
}
Run Code Online (Sandbox Code Playgroud)

这等于Person.prototype.name ='jack';

另一个问题,我已经看到了使用实例的prop的setter的例子:

class Person{
  constructor(){
    this._name = 'jack';
  };
  get name(){
    return this._name;
  }
  set name(val){
    this._name = val;
  }
}
Run Code Online (Sandbox Code Playgroud)

我不想这样做,我想要的东西:

class Person{
  constructor(){};
  get name(){
    return 'jack';
  }
  set name(val){
    // like this
    // name = val;
  }
}
Run Code Online (Sandbox Code Playgroud)

怎么办?

javascript setter ecmascript-6

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

混合基于构造函数和基于setter的注入是一件坏事吗?

我有一个从CSV文件操作导入产品的类,需要大约7个参数.这是进口商绝对需要的信息.

所有这些参数都具有相同的使用寿命.最后,我们必须有一个不可变对象.

我太害怕在构造函数中列出所有这些因为它对可读性的影响并且决定将其中的3个移动到setter注入.但显然这不是一个优雅的解决方案.

问题:

1)混合基于构造函数和基于setter的注射是不好的做法吗?

2)如何解决这个特殊问题?

我正在考虑应用Martin Fowler的"引入参数对象"重构,但这有一个问题.

4参数可以很容易地移动到Parameter对象(customerId,projectId,languageId等) - 所有整数.

其他3个参数是我注入的对象(模拟单元测试需要它).

oop setter refactoring dependency-injection

13
推荐指数
1
解决办法
4631
查看次数

我如何将scala setter方法'myvar_ $ eq(myval)'别名为在java中更令人满意的东西?

我最近将一些代码从java转换为scala,试图自学语言.

假设我们有这个scala类:

class Person() {
  var name:String = "joebob"
}
Run Code Online (Sandbox Code Playgroud)

现在我想从java访问它,所以我不能像我在scala那样使用点符号.

所以我可以通过发出以下内容来获取我的var的内容:

person = Person.new();
System.out.println(person.name());
Run Code Online (Sandbox Code Playgroud)

并通过以下方式设置:

person = Person.new();
person.name_$eq("sallysue");
System.out.println(person.name());
Run Code Online (Sandbox Code Playgroud)

这是正确的,因为我们的Person类在javap中看起来像这样:

Compiled from "Person.scala"
public class Person extends java.lang.Object implements scala.ScalaObject{
    public Person();
    public void name_$eq(java.lang.String);
    public java.lang.String name();
}
Run Code Online (Sandbox Code Playgroud)

是的,我可以编写自己的getter/setter但是我讨厌用这个来填充类,考虑到我已经拥有它们并没有多大意义 - 我只想更好地为_ $ eq方法添加别名.(当你处理像antlr这样的东西时,这实际上会变得更糟,因为那时你必须逃避它,它最终看起来像person.name _\$ eq("newname");

注意:我宁愿忍受这个,而不是用更多的setter方法填充我的类.

那么在这种情况下你会做什么?

java setter scala javap scalac

13
推荐指数
1
解决办法
1173
查看次数

在Eclipse中自动生成的Getter/Setter之间放置空间

我正在使用Eclipse EE IDE - Indigo.我填写了所有类变量,然后右键单击页面并选择 - > Source - > Generate Getters and Setters.这工作正常,但它把方法放在彼此之上ex:

public String getValue1() {  
    return value1;  
}  
public void setValue1(String value1) {  
    $this.value1 = value1  
}  
public String getValue2() {  
    return value2;  
}  
public void setValue2(String value2) {  
    $this.value2 = value2
}  
Run Code Online (Sandbox Code Playgroud)

有没有办法告诉Eclipse在自动生成时在方法之间放置一个空格?

eclipse methods getter setter space

13
推荐指数
2
解决办法
2914
查看次数