如果我有一个WPF列表框,我将其itemssource绑定到一个对象列表.如果对象成员是公共的但没有{get; 组; 绑定将失败.为什么?
我知道这对许多人来说可能是一个愚蠢的问题,但我通常喜欢坚持正确/更好的实施.在Java中,当编写getter/setter时,最好是直接引用实例变量this还是直接访问它?
谢谢
我现在有两个班:RemindersDAO.java和ViewLocalReminders.java.
我正在尝试访问ViewLocalReminders.java中的变量,我试图从RemindersDAO.java中调用它.我是通过使用getter/setter方法组合来做到这一点的.但是,由于某种原因,我的变量值在getter方法中始终设置为0.这是代码:
ViewLocalReminders.java
public class ViewLocalReminders extends SherlockListActivity {
private long reminderID;
public void onCreate(Bundle SavedInstance) {
reminderID = 16;
setReminderID(reminderID);
}
public void setReminderID(long id) {
id = reminderID;
System.out.println("Reminder ID Value in Setter: " + reminderID);
}
public long getReminderID() {
return reminderID;
System.out.println("Reminder ID Value in Getter: " + reminderID);
}
}
Run Code Online (Sandbox Code Playgroud)
RemindersDAO.java
public class RemindersDAO extends SQLiteOpenHelper {
@SuppressWarnings("deprecation")
public Cursor getRowByID(Activity activity) {
String[] from = { _ID, NAME };
ViewLocalReminders viewLocalReminders = new ViewLocalReminders(); …Run Code Online (Sandbox Code Playgroud) 我是JavaScript的新手.在使用给定的javascript时,我期望输出值为"123"; "测试1"; "456"和"test2",因为有两个实例.
但返回的值是"456";"test2"; "456"和"test2".
如果我更改setter以处理"this"(目前在代码中已经出现),问题就解决了.我无法理解为什么protype对象表现得很差.
我不想用这个声明变量,因为我希望变量是私有的,只能通过getter/setters方法访问.
<html>
<head>
<script type="text/javascript">
function Test(){
var name;
var id;
Test.prototype.setId = function(newId){
id = newId;
}
//this.id = function(newId){
Test.prototype.getId = function(newId){
return id;
}
Test.prototype.setName = function(newName){
name = newName;
}
//this.getName = function(newGuid){
Test.prototype.getName = function(newGuid){
return name;
}
}
function callme(){
var instance1 = new Test();
instance1.setId("123");
instance1.setName("test1");
var instance2 = new Test();
instance2.setId("456");
instance2.setName("test2");
alert(instance1.getId());
alert(instance1.getName());
alert(instance2.getId());
alert(instance2.getName());
}
</script>
</head>
<body>
<button onclick='callme()'> Test </button>
</body>
</html> …Run Code Online (Sandbox Code Playgroud) 这可能是一个超级问题,道歉.我正在尝试在PHP中实现一个效果我熟悉PHP,它正在为私有属性构建getter和setter函数.
在PHP中,我通常会这样做:
<?php
class newClass() {
private $var = null;
function newClass($initVal) {
//init
}
public function get($var) {
if ( isset($this->$var) ) {
return $this->$var;
}
}
}
?>
Run Code Online (Sandbox Code Playgroud)
但是,在Python中尝试相同的想法:
class newClass():
def __init__(self, initVal):
self.__nameOfVar1 = initVal
self.__nameOfVar2 = initVal
def get(self, var):
#assuming, when called, var = nameOfVar1
if self.__var:
return self.__var
Run Code Online (Sandbox Code Playgroud)
引发错误 AttributeError:'newClass' object has no attribute '__var'
而且非常正确,事实并非如此.如何构建getter和setter函数来访问私有属性?
更新:
我已经离开了原来的问题,因为对于Python的新成员来说,了解他们可能会意外提出的其他语言的惯例是有用的.但实际上,我最初的问题有点不同,在尝试使问题更加普遍时,我模糊了我的实际需要.所以这是一个修正案:
事实上,我没有使用私有属性; 事实上,我正在尝试通过组合环境向下传递属性.即:
Class A,作为属性,具有Class B不从中继承的对象Class A.几乎从来没有Class B需要任何信息的情况Class …
有没有人知道是否有计划为类变量添加隐式getter和setter?
我正在考虑当前的Scala代码,它已经允许这样做了.类似下面的内容,如果你没有定义一个getter/setter它会使用该值,但是如果你为它使用的值定义一个getter/setter而不是一个直接变量调用.
class A{
int value = 3;
}
class B{
int value = 3;
public int value(){
return value;
}
}
// in some method
A a = new A();
System.out.println(a.value);
B b = new B();
System.out.println(b.value); // <-- no () for accessing value even though it uses the getter
Run Code Online (Sandbox Code Playgroud) 内联,对象文字'get function()'样式和Object.defineProperty之间的功能似乎有重叠.
MDN docs for get https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/get没有提到内联'get'函数已被弃用.
var john = {
firstName: 'John',
lastName: 'Smith',
age: 21,
gender: 'Male'
// () ? String
// Returns the full name of object.
get name() {
return this.firstName + ' ' + this.lastName
},
// (new_name:String) ? undefined
// Sets the name components of the object,
// from a full name.
set name(new_name) {
var names = new_name.trim().split(/\s+/)
this.firstName = names['0'] || ''
this.lastName = names['1'] || ''
},
}
Run Code Online (Sandbox Code Playgroud)
Mozilla的Jeff Walden在2010年(似乎是)的文章中说: …
javascript properties getter-setter ecmascript-5 defineproperty
我正在为bool属性使用自定义设置器,因为当此值更改时,我需要做一些额外的工作。这是我的实现:
MyView.h
@interface MyView : UIView
@property (nonatomic) BOOL isSelected;
@end
Run Code Online (Sandbox Code Playgroud)
MyView.m
@implementation MyView
@synthesize isSelected;
-(void)setIsSelected:(BOOL)_isSelected
{
self.isSelected = _isSelected;
//Custom code that changes UI based on bool state
}
@end
Run Code Online (Sandbox Code Playgroud)
但是,二传手没有被叫到!有人可以告诉我为什么吗?
我最近看到了一个很棒的c风格宏播放,它会自动为该类生成设置器/获取器。这就是我在说的。
#define BOOL_VARIABLE(name)\
void set##name(bool iValue)\
{\
// set the boolean
}\
const bool get##name() const\
{\
// get the boolean
}
BOOL_VARIABLE(AVariableName); // and calling them inside the class
Run Code Online (Sandbox Code Playgroud)
我知道所有避免使用宏的注释类型,但实际上我觉得它很酷,因为较长的getter / setter行确实困扰着我。您能想到这种方法有什么问题吗?
我的问题是关于性能与设计.我在PHP中对Getter和Setter有很多了解.而背后的想法是非常好的和有用的(Debugging,Sanitize).
所以我开始做基准测试:
class Foo {
public $bar;
public function __construct($bar) {
$this->bar = $bar;
}
public function getBar() {
return $this->bar;
}
public function setBar($bar) {
$this->bar = $bar;
}
}
$foo = new Foo(42);
Debug::ProcessingTimeSinceLastCall();
//Without Setter and Getter
for ($i = 0; $i < 1000000; $i++) {
if ($foo->bar === 42) {
$foo->bar = 43;
} else {
$foo->bar = 42;
}
}
Debug::ProcessingTimeSinceLastCall('No Setter and Getter');
//With Getter and Setter
for ($i = 0; $i < 1000000; …Run Code Online (Sandbox Code Playgroud) getter-setter ×10
java ×3
javascript ×2
php ×2
android ×1
benchmarking ×1
c++ ×1
data-binding ×1
ecmascript-5 ×1
getter ×1
ios ×1
ios5 ×1
iphone ×1
macros ×1
objective-c ×1
oop ×1
performance ×1
properties ×1
prototype ×1
python ×1
scala ×1
wpf ×1