给定以下格式(.properties或.ini):
propertyName1=propertyValue1
propertyName2=propertyValue2
...
propertyNameN=propertyValueN
Run Code Online (Sandbox Code Playgroud)
对于Java,有一个Properties类,它提供了解析/与上述格式交互的功能.
python的标准库(2.x)中有类似的东西吗?
如果没有,我还有其他选择吗?
有没有办法在自动实现的属性中设置setter/getter的断点?
int Counter { get; set; }
Run Code Online (Sandbox Code Playgroud)
除了将其更改为标准属性(我这样做,但要做到这一点,我必须更改并重新编译整个项目)
我正在尝试使用新的Apple Swift语言来解决问题.假设我曾经在Objective-C中做过类似的事情.我有readonly属性,他们不能单独更改.但是,使用特定方法,属性会以逻辑方式更改.
我采用以下示例,一个非常简单的时钟.我会在Objective-C中写这个.
@interface Clock : NSObject
@property (readonly) NSUInteger hours;
@property (readonly) NSUInteger minutes;
@property (readonly) NSUInteger seconds;
- (void)incrementSeconds;
@end
@implementation Clock
- (void)incrementSeconds {
_seconds++;
if (_seconds == 60) {
_seconds = 0;
_minutes++;
if (_minutes == 60) {
_minutes = 0;
_hours++;
}
}
}
@end
Run Code Online (Sandbox Code Playgroud)
出于特定目的,我们不能直接触摸秒,分和小时,并且只允许使用方法逐秒递增.只有这种方法可以通过使用实例变量的技巧来更改值.
由于Swift中没有这样的东西,我试图找到一个等价物.如果我这样做:
class Clock : NSObject {
var hours: UInt = 0
var minutes: UInt = 0
var seconds: UInt = 0
func incrementSeconds() {
self.seconds++
if self.seconds …Run Code Online (Sandbox Code Playgroud) 假设你有一个像这样的javascript对象:
var data = { foo: 'bar', baz: 'quux' };
Run Code Online (Sandbox Code Playgroud)
您可以通过属性名称访问属性:
var foo = data.foo;
var baz = data["baz"];
Run Code Online (Sandbox Code Playgroud)
但是,如果您不知道属性的名称,是否可以获取这些值?这些属性的无序性质是否使它们无法区分?
在我的情况下,我特别想到一个函数需要接受一系列名称 - 值对的情况,但属性的名称可能会改变.
到目前为止,我对如何执行此操作的想法是将属性的名称与数据一起传递给函数,但这感觉就像一个黑客.如果可能的话,我宁愿用内省来做这件事.
有几个词有类似的(在某种意义上)含义:
选项,设置,属性,配置,首选项
英语不是我的母语.你能用简单的英语解释一下这个区别吗?我认为以下模板可能有用:
- 在GUI中使用XXX以便让人们更改应用程序的行为(可能是首选项或设置?)
- 在GUI中使用YYY以便让人们更改对象的部分(可能是属性或选项?)
- 在你的代码中使用ZZZ ......
什么是最佳做法?
我有一个JAR文件,我的所有代码都存档以便运行.我必须访问每次运行前需要更改/编辑的属性文件.我想将属性文件保存在JAR文件所在的目录中.有没有告诉Java从该目录中获取属性文件?
注意:我不想将属性文件保留在主目录中,也不希望在命令行参数中传递属性文件的路径.
我可以通过反思设置私有财产吗?
public abstract class Entity
{
private int _id;
private DateTime? _createdOn;
public virtual T Id
{
get { return _id; }
private set { ChangePropertyAndNotify(ref _id, value, x => Id); }
}
public virtual DateTime? CreatedOn
{
get { return _createdOn; }
private set { ChangePropertyAndNotify(ref _createdOn, value, x => CreatedOn); }
}
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了以下它并不起作用,其中t代表一种类型Entity:
var t = typeof(Entity);
var mi = t.GetMethod("set_CreatedOn", BindingFlags.Instance | BindingFlags.NonPublic);
Run Code Online (Sandbox Code Playgroud)
我想我可以做到这一点,但我无法解决这个问题.
IC#我们通过反思来做到这一点.在Javascript中它很简单:
for(var propertyName in objectName)
var currentPropertyValue = objectName[propertyName];
Run Code Online (Sandbox Code Playgroud)
怎么用Python做?
在python中,我可以使用@classmethod装饰器向类添加方法.是否有类似的装饰器向类中添加属性?我可以更好地展示我在说什么.
class Example(object):
the_I = 10
def __init__( self ):
self.an_i = 20
@property
def i( self ):
return self.an_i
def inc_i( self ):
self.an_i += 1
# is this even possible?
@classproperty
def I( cls ):
return cls.the_I
@classmethod
def inc_I( cls ):
cls.the_I += 1
e = Example()
assert e.i == 20
e.inc_i()
assert e.i == 21
assert Example.I == 10
Example.inc_I()
assert Example.I == 11
Run Code Online (Sandbox Code Playgroud)
我上面使用的语法是可能的还是需要更多的东西?
我想要类属性的原因是我可以延迟加载类属性,这似乎足够合理.
这就是我在许多其他类继承的类中作为方法提出的.这个想法是它允许在相同类型的对象的属性之间进行简单比较.
现在,这确实有效 - 但为了提高我的代码质量,我想我会把它扔出去仔细检查.它怎么能更好/更有效/等等?
/// <summary>
/// Compare property values (as strings)
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public bool PropertiesEqual(object comparisonObject)
{
Type sourceType = this.GetType();
Type destinationType = comparisonObject.GetType();
if (sourceType == destinationType)
{
PropertyInfo[] sourceProperties = sourceType.GetProperties();
foreach (PropertyInfo pi in sourceProperties)
{
if ((sourceType.GetProperty(pi.Name).GetValue(this, null) == null && destinationType.GetProperty(pi.Name).GetValue(comparisonObject, null) == null))
{
// if both are null, don't try to compare (throws exception)
}
else if (!(sourceType.GetProperty(pi.Name).GetValue(this, null).ToString() == destinationType.GetProperty(pi.Name).GetValue(comparisonObject, null).ToString()))
{
// only …Run Code Online (Sandbox Code Playgroud) properties ×10
python ×3
c# ×2
object ×2
reflection ×2
.net ×1
breakpoints ×1
class-method ×1
comparison ×1
iteration ×1
java ×1
javascript ×1
objective-c ×1
options ×1
private ×1
settings ×1
swift ×1