我一直试图了解几个小时的事情,我想得到你的观点.
我在我的一个类属性上有setter/getter(我注意到我必须在setter名称前添加"set",否则编译器会说没有setter):
@property (nonatomic, retain, readwrite, setter=setTopString:, getter=TopString) NSString* m_topString;
Run Code Online (Sandbox Code Playgroud)
当我像这样调用setter时,编译器很高兴:
[secureKeyboardController setTopString:@"This action requires that your enter your authentication code."];
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用"点"约定时,我被编译器拒绝了:
secureKeyboardController.topString = @"This action requires that your enter your authentication code.";
Run Code Online (Sandbox Code Playgroud)
真正奇怪的是点命名约定适用于此属性:
@property (nonatomic, readwrite, getter=PINMaxLength, setter=setPINMaxLength:) NSInteger m_PINMaxLength;
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我可以做:
[secureKeyboardController setPINMaxLength:10];enter code here
Run Code Online (Sandbox Code Playgroud)
要么
secureKeyboardController.PINMaxLength = 10;
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,编译器都很高兴.
我真的想睡得比我现在感觉的那么愚蠢.因此,我们将非常感谢任何解释.
此致,Apple92
注意:我没有使用ARC
我有一个具有以下属性的UILabel:@property (nonatomic, retain) UILabel *someLabel;
我正在尝试设置自定义setter.以下代码是否会导致泄漏,因为@property
实际上也是在调用retain
?
- (void)setSomeLabel:(UILabel *)someLabel
{
if (someLabel != self.someLabel) {
[self.someLabel release];
self.someLabel = [someLabel retain];
}
// some custom code here
}
Run Code Online (Sandbox Code Playgroud) 除非存在具有相同名称的局部变量,否则可以在没有显式接收器的情况下使用Getter方法:
class A; attr_reader :foo end
A.new.instance_eval do
@foo = :foo
p foo
end
# => :foo
Run Code Online (Sandbox Code Playgroud)
当存在具有相同名称的局部变量时,这将不成立,因为当存在歧义时,作为局部变量的解释具有优先级而不是方法调用.
class A; attr_reader :foo end
A.new.instance_eval do
foo = :bar
@foo = :foo
p foo
end
# => :bar
Run Code Online (Sandbox Code Playgroud)
但是,即使没有在相关表达式之前分配具有相同名称的局部变量,也不能在没有显式接收器的情况下使用setter方法:
class A; attr_writer :foo end
A.new.instance_eval do
foo = :foo # <= No local variable named `foo` has been assigned before this point
p @foo
end
# => nil
Run Code Online (Sandbox Code Playgroud)
这个setter方法的"反私人"属性如何合理?
我需要在我的表单的FormStyle属性更改之前进行一些处理,但TForm.SetFormStyle(属性setter)是私有的,是否有某种方法来覆盖属性但仍然可以访问父类属性?
TMyForm = class(TForm)
private
procedure MySetFormStyle(Style: TFormStyle);
public
property FormStyle: TFormStyle read Parent.FormStyle write MySetFormStyle;
end;
TMyForm.MySetFormStyle(Style: TFormStyle);
begin
if Parent.FormStyle <> Style then
DoSomething;
Parent.FormStyle := Style;
end;
Run Code Online (Sandbox Code Playgroud)
我正在使用delphi 2010
var = putVar;以下代码运行 ; & this.var = putVar;
我理解:"this"用于识别 - "将此值仅用于'我的'对象".当两者都有效时,为什么人们通常在制定者中使用"这个"?
码:
public class PlayingWithObjects
{
public static void main(String[] args)
{
SomeClass classObj = new SomeClass(10);
System.out.println("classObj.getVar: " + classObj.getVar() );
classObj.setVar(20);
System.out.println("classObj.getVar: " + classObj.getVar() );
classObj = new SomeClass(30);
System.out.println("classObj.getVar: " + classObj.getVar() );
}
}
class SomeClass
{
private int var;
public SomeClass(int putVar)
{
var = putVar;
}
public int getVar()
{
return var;
}
public void setVar(int putVar)
{
// var = putVar; …
Run Code Online (Sandbox Code Playgroud) 我在C#中有一个具有当前属性的对象.
public DateTime startDate
{
get
{
string[] ymd = Environment.GetCommandLineArgs()[2].Split('.');
return new DateTime(Int32.Parse(ymd[2]), Int32.Parse(ymd[1]), Int32.Parse(ymd[0]));
}
set { startDate = value; }
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用定义为此的函数时:
public String Calculate(){
if (startDate > endDate)
return "not calculable since the end date can not be before than the start date.";
while (startDate <= endDate)
{
if (startDate.DayOfWeek.ToString()[0] != 'S')
count++;
startDate = startDate.AddDays(1);
}
return "not implemented yet";
Run Code Online (Sandbox Code Playgroud)
Stack Overflow发生:)你可以帮我解决这个问题吗?
我是Spring Framework的新手。实际上我正在做一个实验spring
。
看这个HelloWorld.java
:
public class HelloWorld {
private String messageee;
public void setMessage(String messageee){
this.messageee=messageee;
}
public void show(){
System.out.println("message: "+messageee);
}
}
Run Code Online (Sandbox Code Playgroud)
你看这个节目,我已经被外界声明为一个变量private
命名为messageee
和下一个被参数化可变setter
命名为messageee
。您会看到两者具有相同的名称。
好的..现在看这个bean文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloWorld" class="com.springframework.HelloWorld">
<property name="message" value="Hello.. This is Spring Framework example."></property>
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
在这里,您会看到内部bean
标签。我已将属性名称声明为message
。我不明白,当我输入名称messageee
时会出现如下错误:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloWorld' defined in class path …
Run Code Online (Sandbox Code Playgroud) 我只是在玩C#而且我在嘲笑自己哪种方法适合Getter和Setter.我用谷歌找到了这样的东西:
class MyClass
{
Button btnMyButton;
// some code...
public Button getBtnMyButton
{
get
{
return btnMyButton;
}
}
}
Run Code Online (Sandbox Code Playgroud)
有一种'正确'的方式吗?或者这也没关系:
class MyClass
{
Button btnMyButton;
// some code...
public Button getBtnMyButton()
{
return this.btnMyButton;
}
}
Run Code Online (Sandbox Code Playgroud)
有什么不同?
最近,我在计算机科学课程中完成了一项任务,创建了一个由所述类构建的两个对象的类.
教授通过电子邮件批评的内容如下:"构造函数,getter和setter中的sysouts应该是主要方法."
他不会说英语,所以没有多大帮助.有人知道他在谈论我的代码时到底在说什么吗?这是我提交的代码:
public class Book {
int currentPage;
int nextPage;
int lastPage;
public Book(int pageNumber) {
currentPage = pageNumber;
System.out.println("You've opened the book to page " + currentPage + ".");
}
public void turnForward(int numTurned) {
nextPage = numTurned + currentPage;
System.out.println("You've turned forward to page " + nextPage + ".");
}
public void turnBack(int numTurned) {
lastPage = nextPage - numTurned;
if (lastPage <= 1) {
lastPage = 1;
System.out.println("You've turned back to the first page.");
}else { …
Run Code Online (Sandbox Code Playgroud) 我对计算机编码世界有点新意,所以我不确定如何阅读GDP,但是当我试图在我的程序中运行我的setter时,我得到了Seg Fault:11.任何人都可以解释为什么这些安装人员会引起问题(如果可能的话,还有lamens术语和技术术语)?setter的编码如下:
class MapEntry{
private:
int mIDVal;
char* mKeyName;
public:
MapEntry(char *key, int val) {
mKeyName = key;
mIDVal = val;
}
int getVal(){
return mIDVal;
}
char* getKey(){
return mKeyName;
}
void setVal(int val){
this->mIDVal = val;
}
void setKey(char* key){
this->mKeyName = key;
}
};
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建一个简单的数据库,其中包含一个char字符串作为键,一个学生ID作为val进行排序.这是代码的其余部分.
Map::Map() {
database = new MapEntry*[DATABASE_SIZE];
for (int i = 0; i < DATABASE_SIZE; i++){
database[i]->setVal(0);
database[i]->setKey("");
}
}
/* Adds (inserts) val with the associated key.
* Returns if successful or …
Run Code Online (Sandbox Code Playgroud) setter ×10
getter ×3
java ×3
c# ×2
objective-c ×2
properties ×2
this ×2
accessor ×1
c++ ×1
class ×1
delphi ×1
inheritance ×1
ios ×1
iphone ×1
object ×1
ruby ×1
spring ×1
spring-mvc ×1