instance

What is a good way to memoize data across many instances of a class in Ruby?

佐手、 提交于 2019-12-07 12:21:14
问题 Consider: many instances of an object that generates data. It would be great to only generate that data once per run. class HighOfNPeriods < Indicator def generate_data @indicator_data = DataStream.new (0..@source_data.data.count - 1).each do |i| if i < @params[:n_days] ... @indicator_data.add one_data end end There are different instances of HighOfNPeriods with different params and different @source_data . Here is how the indicator is used: class Strategy attr_accessor :indicators def

Limit number of class instances with python

南楼画角 提交于 2019-12-07 10:57:57
问题 Μy Mainclass creates a simple QmainWindows like this: class mcManageUiC(QtGui.QMainWindow): def __init__(self): super(mcManageUiC, self).__init__() self.initUI() def initUI(self): self.show() And at the end of my file I launch it like this: def main(): app = QtGui.QApplication(sys.argv) renderManagerVar = mcManageUiC() sys.exit(app.exec_()) if __name__ == '__main__': main() My problem is that each time i source it, it launches a new window. I would like to know if there is a way to detect

Class Objects and Instance Variables in Objective-C

纵然是瞬间 提交于 2019-12-07 09:42:46
问题 I'm having a hard time wrapping my head around this concept. I'll take the quote exactly from the book: Class objects also inherit from the classes above them in the hierarchy. But because they don’t have instance variables (only instances do), they inherit only methods. Correct me if I'm wrong, but a class object would be this: NSString *aString = [[NSString alloc]initWithString:@"abc.."]; The class object in this case is *aString -- am I correct so far? The thing that confuses me is the

Why can't I instantiate a Groovy class from another Groovy class?

时光怂恿深爱的人放手 提交于 2019-12-07 08:20:43
问题 I have two classes. One.groovy: class One { One() {} def someMethod(String hey) { println(hey) } } And Two.groovy: class Two { def one Two() { Class groovy = ((GroovyClassLoader) this.class.classLoader).parseClass("One.groovy") one = groovy.newInstance() one.someMethod("Yo!") } } I instantiate Two with something like this: GroovyClassLoader gcl = new GroovyClassLoader(); Class cl = gcl.parseClass(new File("Two.groovy")); Object instance = cl.newInstance(); But now I get groovy.lang

How to get specific instance of class from another class in Java?

微笑、不失礼 提交于 2019-12-07 06:30:40
问题 I've created the following class with the main method, which creates new instance of Application and instances of ApplicationModel , ApplicationView and ApplicationController for this particular Application . public class Application { // Variables private ApplicationSettings settings; private ApplicationModel model; private ApplicationView view; private ApplicationController controller; // Constructor public Application() { settings = new ApplicationSettings(); model = new ApplicationModel()

Referencing a row from another table (PostgreSQL)

别等时光非礼了梦想. 提交于 2019-12-07 06:10:30
I'm new to PostgreSQL and I'm struggling to understand how to use a reference to an 'instance' (or row) from a table as a value within a row of another table. Here is my desired result: class User{ int age; Post[] posts; } class Post{ int postId; ... } // Sql script sqlMain{ User k = new User(20); k.addPost(10, ...); } As you can see, I want an (dynamic prefereably, like an ArrayList) array of posts as an attribute of a user. So far I have the following Script: CREATE TABLE Post( postId INT ) CREATE TABLE User( id INT, posts Post[] ) // Member function of User class CREATE FUNCTION addPost

Calling static function from instance

孤街醉人 提交于 2019-12-07 05:22:26
问题 I'm trying to call a static magic function ( __callStatic ) from a member of its child class. Problem being, it goes to the non-static __call instead. <?php ini_set("display_errors", true); class a { function __call($method, $params) { echo "instance"; } static function __callStatic($method, $params) { echo "static"; } } class b extends a { function foo() { echo static::bar(); // === echo self::bar(); // === echo a::bar(); // === echo b::bar(); } } $b = new b(); echo phpversion()."<br />"; $b

Right way to instantiate class in PHP

假如想象 提交于 2019-12-07 05:19:18
问题 I am trying to create a method inside class, that will instantiate class that is currently in. But I would also need that from this method to work correctly in all extended classes. As I have learned from this thread, it's not good to use self keyword for this task. So obvious choice would be using static keyword. But, I've come across different method that also works. Example: class SimpleClass { private $arg; public function __construct( $arg ){ $this->arg = $arg; } public function getArg()

Why can't I store a PHP class instance as a SESSION variable

依然范特西╮ 提交于 2019-12-07 04:47:40
问题 I have a PHP script that is called in 2 ways from a Dojo Ajax xhrGet call. The first time it is called with an "init" argument which causes the script to create an instance of the StateList class and read in a file of state names. session_start(); @include('StateList.php'); require_once('phplog.php'); //start executing here $comd=$_GET['nexturl']; if($comd=="init") { $st = new StateList("../data/statestxt.txt"); $_SESSION['statefile'] = $st; } The second and further times, another xhrGet call

Android: two instances of Text-to-Speech work very slowly

戏子无情 提交于 2019-12-07 02:11:39
问题 I need to implement feature in my Andorind app which allows to play two different synthesized languages in current Acitivity - for instance having two buttons Say English and Say French I've tried to do it in two following ways but both of them works ineffectively because there is long delay before sound plays: first approach: create single instance of TTS and change language by setLocale method depending on what language has to be played. Unfortunately switching between languages by