我有一个Python类
class pytest:
i = 34
def func(self):
return "hello world"
Run Code Online (Sandbox Code Playgroud)
当我访问时pytest.i,我得到34.我也可以这样做:
a = pytest()
a.i
Run Code Online (Sandbox Code Playgroud)
这也给了34.
如果我试图访问(不存在)pytest.j,我得到
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
pytest.j
AttributeError: class pytest has no attribute 'j'
Run Code Online (Sandbox Code Playgroud)
当我尝试时a.j,错误是
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
a.j
AttributeError: pytest instance has no attribute 'j'
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:两种情况到底发生了什么,有什么区别?
我有一个与另一个问题非常相似的问题:将属性名称作为字符串.
他的解决方案结束了
// Static Property
string name = GetPropertyName(() => SomeClass.SomeProperty);
// Instance Property
string name = GetPropertyName(() => someObject.SomeProperty);
Run Code Online (Sandbox Code Playgroud)
我想要的是具有类似于静态属性的语法,但是对于实例属性.
原因是我现在有代码使用反射来获取集合中所有对象的属性值,但我必须将其作为硬编码字符串传递.
示例代码:
double Sum = AmountCollection.Sum("thatfield");
Run Code Online (Sandbox Code Playgroud)
嗯,这很好用,但如果"thatfield"被重命名,代码将不再有效.编译器无法检查它,因为它只是一个字符串.此外,Get References也不会出于同样的原因.
那么,有没有办法实现从实例属性轻松获取属性名称的目标(即;只是一个函数调用)?
谢谢.
我的问题是为什么这一行- ThreadTest tt = new ThreadTest();在下面的示例中创建一个公共实例而不是一个单独的实例。请指教,谢谢!
class ThreadTest
{
bool done;
static void Main()
{
ThreadTest tt = new ThreadTest(); // Create a common instance
new Thread (tt.Go).Start();
tt.Go();
}
// Note that Go is now an instance method
void Go()
{
if (!done) { done = true; Console.WriteLine ("Done"); }
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:该示例来自http://www.albahari.com/threading/#_Introduction,该示例演示了如何在线程之间共享数据。
EDIT2:我的问题恰恰是为什么“该实例对两个线程都相同”
假设我有一个类Foo和这个类的实例myFoo:
Foo *myFoo;
Run Code Online (Sandbox Code Playgroud)
是否有任何方法"dispalyFooObjectName"可以显示对象的名称,例如:
NSLog(@"i was called from %s", [myFoo dispalyFooObjectName]);
Run Code Online (Sandbox Code Playgroud)
结果将是:
i was called from myFoo
Run Code Online (Sandbox Code Playgroud) 我在使用ARC的Ios 5应用程序上出现以下错误:
*** -[ViewDettaglio respondsToSelector:]: message sent to deallocated instance 0x12193300
在控制台上我写命令:
info malloc-history 0x12193300
我得到以下堆栈跟踪:
Alloc: Block address: 0x12193300 length: 192
Stack - pthread: 0xa08a3540 number of frames: 31
0: 0x96bdab03 in malloc_zone_calloc
1: 0x96bdaa5a in calloc
2: 0x16f8c93 in class_createInstance
3: 0x170388b in _objc_rootAllocWithZone
4: 0x21af661 in +[NSObject allocWithZone:]
5: 0x17038b9 in _objc_rootAlloc
6: 0x2c4c8 in -[ViewElenco CaricaViewDettaglio:] at /Users/.../ViewElenco.m:186
7: 0x2e550 in -[ViewElenco mapView:annotationView:calloutAccessoryControlTapped:] at /Users/.../ViewElenco.m:337
8: 0x3fa99c
9: 0x405faa in MKLongHash
10: 0x21aeec9 in -[NSObject performSelector:withObject:withObject:] … 我可以在本地引用C#中的类,而不是类的实例吗?以下代码将无法编译,但作为示例,类似于:
void someFunc()
{
var a = System.Math;
var b = a.Abs(4);
}
Run Code Online (Sandbox Code Playgroud)
编辑:在真正的程序中,它不是System.Math类,我想构造类并返回构造的值.我原本不认为我想要使用该类的上下文是相关的,而且可能不应该.
Anastasiosyal有一个有趣的想法,使用当地的代表来做.
我正在使用以下代码收集实例:
class Hand():
instances = []
def __init__(self):
Hand.instances.append(self)
self.value = 5
def do_something(self, a):
self.value = self.value * a
class Foo():
def __init__(self):
pass
def insty(self):
self.hand1 = Hand()
self.hand2 = Hand()
foo = Foo()
foo.insty()
print Hand.instances
for hand in Hand.instances:
print "how do I print the instance name?"
Run Code Online (Sandbox Code Playgroud)
最后一行只是学习如何访问实例名称的方法,因此我可以按顺序在每个实例上调用'do_something'方法.
如何访问Hand的每个实例的实例名称?
问题是我不断将余额变为负值,它执行需要取出资金的部分,但我已经实施的"if"陈述应该可以防止这种情况发生.我是编程的新手,如果这很傻,我很抱歉.
这是我的主要内容:
public class PrinterAccount
{
private int balance;
public void topUp(int amount)
{
balance += amount;
}
public int getBalance()
{
return balance;
}
public boolean printDocument(int numPages, boolean isDoubleSided)
{
if (isDoubleSided)
{
if(numPages % 2 == 0)
{
int b = 0;
b = b - ((numPages/2)*5);
if (balance > b)
{
balance -= ((numPages/2) * 5);
return true;
}
else
{
return false;
}
}
else
{
numPages = numPages + 1;
int a = 0; …Run Code Online (Sandbox Code Playgroud) 我想要一个类变量,以便可以在所有实例中访问该值,但我还想在类中的方法中访问该变量.那可能吗?我试过这个,但它根本不起作用.
class myClass:
myvariable = 1
def add():
myvariable+= 1
def print1():
print myvariable
Run Code Online (Sandbox Code Playgroud)
我想制作两个实例,一个只做add方法,另一个只做print1方法
这些天我一直有一些空闲时间,所以我决定学习一些haskell。
为了练习,我正在翻译我在SCALA的一个班级所做的项目。但是我对这部分代码有疑问。这很容易理解。
这个想法是为一些解析器建模,该解析器将使用一些字符串,并将其解析为一个包含解析元素“ output”和“ remainder”(无法解析的字符串的一部分)的ParserOutput。
我可以不用定义一个新的类就可以做到这一点(只使用数据“ MyParser”),但是我认为定义一个类很有趣,这样我就可以在一个地方定义我希望解析器工作的所有方法。
data ParserOutput a =
Failure | Success { output :: a, remainder :: String }
deriving (Show)
data MyParser t = MyParser (String -> ParserOutput t)
class Parser p where
parse :: p -> String -> ParserOutput t
instance Parser (MyParser t) where
parse (MyParser parserDefinition) = parserDefinition
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
data ParserOutput a =
Failure | Success { output :: a, remainder :: String }
deriving (Show)
data MyParser t = MyParser (String -> …Run Code Online (Sandbox Code Playgroud)