instance

Accessing an instance of a variable from another class in Java

随声附和 提交于 2019-11-28 14:29:30
Is it possible to access an instance of a variable in one class from another class in Java. Let's say you have the following in Class A: private BlockingQueue<byte[]> buffer = new LinkedBlockingQueue<byte[]>(); I want to make changes to the queue in this class and then be able to use to access it from another class. How would i access the instance of buffer from another class? Is it even possible? Add a getter: public class Whatever { private BlockingQueue<byte[]> buffer = new LinkedBlockingQueue<byte[]>(); public BlockingQueue<byte[]> getBuffer() { return buffer; } } Then if you have an

May I create an instance of a structure using a simple Int?

大兔子大兔子 提交于 2019-11-28 14:28:57
Is it right to make a structure's instance this way? public struct Barometer { public var pressure: Int public init(pressure: Int) { self.pressure = pressure } } var barometer: Barometer = 80 Or I need to adopt a protocol? You can make that work by adopting the IntegerLiteralConvertible protocol: extension Barometer: IntegerLiteralConvertible { public init(integerLiteral value: Int) { self.init(pressure: value) } } Now a Barometer value can be instantiated from a literal integer: let barometer: Barometer = 80 print(barometer) // Barometer(pressure: 80) But note that this works only with

Reference Static Methods/Variables in Java from an Instance

女生的网名这么多〃 提交于 2019-11-28 13:56:55
Can anyone explain to me why java allows you to access static methods and members from an instance? A bad example, if I have a class called RedShape and it has a static method called getColor() which returns "red", why does java allow you to call the static method from an instance of RedShape? To me this seems to violate some of the core concepts of OO language design. At the least, it should come with a compiler warning. Thanks in advance. Edit: In particular, I'm asking about when you have something like RedShape test = new RedShape(); test.getColor(); where getColor is a static method of

In python, how can I inherit and override a method on a class instance, assigning this new version to the same name as the old one?

吃可爱长大的小学妹 提交于 2019-11-28 12:53:15
In matplotlib , a common problem are unwanted white lines between Patch objects drawn with pcolor , pcolormesh , and contourf (see this thread for the former two and this thread for the latter). I've attempted to fix this automatically by adding new methods to my Axes class/subclass instances using MethodType . I do this instead of subclassing simply because I want to generate Axes by passing slices of GridSpec objects to the add_subplot method on a Figure instance, and I am not aware of how I could do this with some kind of subclassed matplotlib.axes.Subplot (but I welcome advice). Here is

How to get instance given a method of the instance?

让人想犯罪 __ 提交于 2019-11-28 11:55:42
class MyClass: def myMethod(self): pass myInstance = MyClass() methodReference = myInstance.myMethod Now can you get a reference to myInstance if you now only have access to methodReference ? methodReference.im_self and by a similar token, for the class: methodReference.im_class For this kind of code discovery you should install iPython and use tab, for instance, in your case myReference.+TAB would give: In [6]: methodReference. methodReference.im_class methodReference.im_func methodReference.im_self Hence, you don't need to worry about remembering things so much - you know that the method is

All instances of a class have the same dict as an attribute in Python 3.2

房东的猫 提交于 2019-11-28 11:49:30
问题 I'm writing a space trading game in Python, and I've decided that the map needs to be split into smaller chunks to reduce the number of objects that need to be considered for drawing on the screen at any given time. This is handled by having a Sector object, which is defined as such: class Sector: x=0 #Position of the sector. The galactic (absolute) position of any object is its in-sector position y=0 #plus the galactic position offset times the size of a sector. stardict=dict() Then, the

Get list of open windows form instance that are excuted from different assembly

谁说胖子不能爱 提交于 2019-11-28 11:36:53
I have a 'loader app' that loads a menu and when user clicks the menu image button a list view opens based on the text (if text = employee) (Go to class A) (Go to class B) ... ... (Show List View Window) if he clicks again on the same button it opens again, I would like to prevent this. i.e but this for a WPF application If you want a list of the open forms, that is Application.OpenForms . You could iterate over this, using GetType() and checking the .Assembly to find those from a different assembly. Beyond that, I'm not entire clear on the question... Assembly currentAssembly = Assembly

Ajax-intensive page: reuse the same XMLHttpRequest object or create new one every time?

痞子三分冷 提交于 2019-11-28 11:26:35
I'm working on some sort of online multiuser editor / coop interface, which will be doing a lot (as in, thousands) of ajax requests during one page lifetime. What would be best: ('best' in terms of stability, compatibility, avoiding trouble) Create one XMLHttpRequest object and reuse that for every HTTP request Create a new XMLHttpRequest object for every HTTP request Manage a dynamic 'pool' of XMLHttpRequest objects, creating a new one when starting a HTTP request and no existing object is available, and tagging a previously created object as 'available' when its last request was completed

Do RegExps made by expression literals share a single instance?

拜拜、爱过 提交于 2019-11-28 11:01:14
问题 The following snippet of code (from Crockford's Javascript: The Good Parts ) demonstrates that RegExp objects made by regular expression literals share a single instance: function make_a_matcher( ) { return /a/gi; } var x = make_a_matcher( ); var y = make_a_matcher( ); // Beware: x and y are the same object! x.lastIndex = 10; document.writeln(y.lastIndex); // 10 Question : Is this the same with any other literals? I tried modifying the code above to work with the string "string" , but got a

Changing fill color of MovieClip Actionscript 3

别说谁变了你拦得住时间么 提交于 2019-11-28 10:14:12
i want to ask, how to change only fill color of an instance on the stage - i can do it by using ColorTransform object, but it changes color of the whole instance, not just the fill. I want to change only the fill color, not the stroke color. can anybody help me? this is my code: function paint(){ var myColorTransform:ColorTransform = new ColorTransform(); myColorTransform.color = someColorNumber; coloredObject.transform.colorTransform = myColorTransform; } This cant be done once you've drawn a shape as action script has no way of determining what is a stroke and what is a fill. You have a