我正在写一个有很多getter和setter的课程,并且想知道人们对以下内容的看法:
通常的方法是编码如下:
public function setChangeSort($changeSort)
{
self::$changeSort = $changeSort;
}
public function getChangeSort()
{
return self::$changeSort;
}
Run Code Online (Sandbox Code Playgroud)
您对以下内容有何看法:
public function setChangeSort($changeSort) { self::$changeSort = $changeSort; }
public function getChangeSort() { return self::$changeSort; }
Run Code Online (Sandbox Code Playgroud)
以原始的方式做这件事我没有问题,因为这就是它应该如何完成的.它似乎在我的classon函数中占用了很多空间,这些空间在他们的工作中非常明显.
提前致谢.
我想替换这个:
self.fajerImage = [UIImage imageNamed:@"FirstViewBG_5N.png"];
self.shrogImage = [UIImage imageNamed:@"FirstViewBG_4N.png"];
self.dohorImage = [UIImage imageNamed:@"FirstViewBG_3N.png"];
self.aaserImage = [UIImage imageNamed:@"FirstViewBG_2N.png"];
self.mgribImage = [UIImage imageNamed:@"FirstViewBG_1N.png"];
self.eeshaImage = [UIImage imageNamed:@"FirstViewBG_0N.png"];
Run Code Online (Sandbox Code Playgroud)
有一个for循环..我不知道如何在循环中一个接一个地调用ivars ..
请注意,在循环之前将它们放在一个数组中是个好主意,我没有成功实现..
谢谢!
我有一个这样的课:
Public Class MyClass
Private _intList As New List(Of Integer)
Private _avg As Decimal
Public Sub Add(ByVal anInt As Integer)
_intList.Add(anInt)
End Sub
Public Property Avg() As Decimal
Get
Dim _sum As Integer = 0
For Each anInt In _intList
_sum += anInt
Next
Avg = If((_intList.Count > 0), _sum / _intList.Count, _avg)
Return _avg
End Get
Set(ByVal value As Decimal)
If _avg <> value Then
_avg = value
Console.WriteLine("Value changed")
End If
End Set
End Property
End Class
Run Code Online (Sandbox Code Playgroud)
Getter正在计算平均值并调用Setter来保存该值.由于某种原因,我无法理解,平均值始终为0.例如: …
我基本上想做这样的事情
public string Password
{
set { password = value; }
}
internal string Password
{
get { return password; }
}
Run Code Online (Sandbox Code Playgroud)
但是不必调用我的第二个函数别的东西(上面的代码不能编译,因为我使用了密码两次).我可以有setPassword和getPassword,但这似乎有点,好吧,垃圾:(
我的场景是我有一个包含核心业务逻辑的独立程序集(类库).我不希望任何使用类库的项目一旦设置就能查看密码.
我想在后面的代码中调用一个Setter(WPF特定).
void Invoke(Setter setter)
{
//I am interested what to do here, ?
}
Run Code Online (Sandbox Code Playgroud)
二传手就像是
<Setter TargetName="SomeUiElement" Property="SomeProperty" Value="{Binding SomeValue}" />
//resolution similar to
void Call(Setter setter)
{
setter.Property.SetValue(setter.TargetObject, setter.Value.GetValue())
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.请具体说明.
代码:
HEADER
class Parser{
private:
unsigned int cant_documentos;
unsigned int cant_terminos;
std::map<std::string,short> dicc_stopwords;
std::map<std::string,unsigned int> hash_frecuencias_globales;
std::map<std::string,std::map<std::string,unsigned int> > hash_frecuencias_locales;
std::map<std::string,std::string> hash_apariciones_unicas;
public:
Parser();
~Parser();
public:
void setFrecuenciasGlobales(std::map<std::string,std::map<std::string,unsigned int> > frecuencias);
};
Run Code Online (Sandbox Code Playgroud)
头顶
.CPP
void Parser::setFrecuenciasGlobales(map<string,map<string,unsigned int> > frecuencias){
hash_frecuencias_globales = frecuencias;
cant_terminos = frecuencias.size();
}
Run Code Online (Sandbox Code Playgroud)
CPP结束
编译器输出:
parser/parser.cpp:102:30: error: no match for ‘operator=’ in ‘((Parser*)this)->Parser::hash_frecuencias_globales = frecuencias’
parser/parser.cpp:102:30: note: candidate is:
/usr/include/c++/4.6/bits/stl_map.h:253:7: note: std::map<_Key, _Tp, _Compare, _Alloc>& std::map<_Key, _Tp, _Compare, _Alloc>::operator=(const std::map<_Key, _Tp, _Compare, _Alloc>&) [with _Key = std::basic_string<char>, …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个非常简单的程序,为所购买的商品生成发票.我有一个带有我的setter和getter的驱动程序类,以及我应该调用它们的对象类.但是,当我打印出发票时,它只会为所有内容生成空响应.
这是我的驱动程序类:
public class Invoice {
private String partNumber; //part number of item
private String description; //description of item
private int quantity; //quantity being purchased
private double itemPrice; //price of item
//constructor
public Invoice(String partNumber, String description, int quantity, double itemPrice) {
partNumber = partNumber;
description = description;
if (quantity < 0) {
quantity = 0;
}
if (itemPrice < 0.0) {
itemPrice = 0.0;
}
}
public void setPartNumber(String number) {
partNumber = number; //store the partNumber
}
public …Run Code Online (Sandbox Code Playgroud) 我正在使用这样的定制吸气剂
var currentShowLiked: Bool {
get {
return [some condition met] ? true : false
}
set {
self.currentShowLiked = newValue
}
}
Run Code Online (Sandbox Code Playgroud)
它工作正常.但是,我希望能够设置true或false重新设置我的变量,但Swift强迫我实现一个setter,它不会产生任何警告,但在运行时如果我要改变我的变量的值,应用程序崩溃了没有明显的理由或理智的解释指向setter line with EXC_BAD_ACCESS (code=2...)和控制台中的消息warning: could not load any Objective-C class information. This will significantly reduce the quality of type information available.
我为什么这样做?
该计划应在12个月和24个月后计算2个账户的利息.这很好用.我的问题是利率的getter/setter不起作用,所以当利率在另一个类私有变量中保存为0.1时,我无法从主类中打印出来.
public class testAccountIntrest{
//main method
public static void main(String[] args) {
//creating objects
Account account1 = new Account(500);
Account account2 = new Account(100);
//printing data
System.out.println("");
System.out.println("The intrest paid on account 1 after 12 months is " + account1.computeIntrest(12));
System.out.println("");
System.out.println("The intrest paid on account 1 after 24 months is " + account1.computeIntrest(24));
System.out.println("");
System.out.println("");
System.out.println("The intrest paid on account 2 after 12 months is " + account2.computeIntrest(12));
System.out.println("");
System.out.println("The intrest paid on account 2 after 24 months is …Run Code Online (Sandbox Code Playgroud) 我正在尝试在学习Go时尝试理解MVC架构,并且我试图从控制器中更改模型中的值.
代码现在创建一个只保存字符串的模型,视图显示终端上的字符串,但控制器无法更改它(它获得用户输入没有任何问题).
现在我在终端上的文字是这样的:
Hello World!
asdf //my input
Hello World!
Run Code Online (Sandbox Code Playgroud)
我想得到的输出将是这样的:
Hello World!
asdf //my input
asdf
Run Code Online (Sandbox Code Playgroud)
这是我的文件:
model.go
package models
type IndexModel struct {
Text string
}
func (m *IndexModel) InitModel() string {
return m.Text
}
func (m *IndexModel) SetText(newText string) {
m.Text = newText
}
Run Code Online (Sandbox Code Playgroud)
view.go
package views
import "github.com/jufracaqui/mvc_template/app/models"
type IndexView struct {
Model models.IndexModel
}
func (v IndexView) Output() string {
return v.Model.Text
}
Run Code Online (Sandbox Code Playgroud)
controller.go
package controllers
import "github.com/jufracaqui/mvc_template/app/models"
type IndexController struct {
Model models.IndexModel
} …Run Code Online (Sandbox Code Playgroud)