methods

can variables be set randomly when declaring them again?

五迷三道 提交于 2020-01-04 13:45:06
问题 In my method, I declare some variables, including int blockCount; . I call this method more than once. Using the Xcode debugger, I found out that after the second time the method was called, the value of blockCount was set to 364265, while it was set to 2, just a few milliseconds earlier. It's not a real problem, since I can just set it to 0 or any other number I'd like, but is it bad programming habit to have a certain variable declared over and over again? I'm quite new to programming, and

When should __method__ be called directly?

冷暖自知 提交于 2020-01-04 12:18:03
问题 When should a __dunder__ method be called directly? For example, instead of a + b one could write a.__add__(b) but is that a good idea? 回答1: In general, no, it’s not a good idea. The operators are there for a reason after all, so when using the operator is possible, use it. There are a few things where calling the special methods directly is useful. The most common thing would be when subclassing. So when you override a special method and you want to call the super’s method too, you will have

How to make integer into array of dates?

孤人 提交于 2020-01-04 09:03:39
问题 If a user creates a challenge with days_challenge: 5 & committed: ["mon", "tue", "wed", "thu", "fri"] then how can we create an array of dates from date_started: "2016-04-20" until the last day of the challenge using a model method called dates_challenged ? create_table "challenges", force: true do |t| t.string "action" t.date "date_started" t.text "committed", default: "---\n- sun\n- mon\n- tue\n- wed\n- thu\n- fri\n- sat\n" t.integer "days_challenged" end The array would look something like

Passing FileWriter as a parameter to a method

微笑、不失礼 提交于 2020-01-04 08:23:44
问题 I'm sure there is a fairly simple answer to this question, so here we go. I'm trying to use a FileWriter to write text to a file. My program reads text in from an already existing file, specified by the user and then asks whether to print the text to the console or to a new file, also to be named by the user. I believe my problem is with passing the FileWriter to the "FileOrConsole" method. Am I not passing or declaring the FileWriter in the "FileOrConsole" method correctly? The file is

How to queue and call actual methods (rather than immediately eval) in java?

﹥>﹥吖頭↗ 提交于 2020-01-04 07:55:40
问题 There are a list of tasks that are time sensitive (but "time" in this case is arbitrary to what another program tells me - it's more like "ticks" rather than time). However, I do NOT want said methods to evaluate immediately. I want one to execute after the other finished. I'm using a linked list for my queue, but I'm not really sure how/if I can access the actual methods in a class without evaluating them immediate. The code would look something like... LinkedList<Method> l = new LinkedList

How to let R know a function is not an S3 method?

╄→гoц情女王★ 提交于 2020-01-04 05:57:30
问题 If I define a function called log.foo = function(foo){ log(foo) } then R will think it is a S3 method. How can I tell R log.foo() is not the S3 method for the class foo. (In fact, class foo does not exist.) Or do I just have to rename my function? 来源: https://stackoverflow.com/questions/23377869/how-to-let-r-know-a-function-is-not-an-s3-method

Why can't I call a method outside of an anonymous class of the same name

喜你入骨 提交于 2020-01-04 05:38:09
问题 The code at the end produces a compile error: NotApplicable.java:7: run() in cannot be applied to (int) run(42); ^ 1 error The question is why? Why does javac think I am calling run(), and does not find run(int bar)? It correctly called foo(int bar). Why do I have to use NotApplicable.this.run(42);? Is it a bug? public class NotApplicable { public NotApplicable() { new Runnable() { public void run() { foo(42); run(42); // uncomment below to fix //NotApplicable.this.run(42); } }; } private

How to call a method from a separate class java

旧时模样 提交于 2020-01-04 04:39:07
问题 I'm trying to call a method from a separate class in java but it doesn't seem to work, or I'm doing something wrong. What I want to achieve is to call my Race method which is in the RacingEvent class to my main program (Check the comment in the main program). Here is the class: import java.util.Random; public class RacingEvent { private SimpleWindow w; private RaceTrack track; private Turtle t1 = new Turtle(w, 200, 400); private Turtle t2 = new Turtle(w, 300, 400); public RacingEvent

Why are parenthesis used in the middle of a method call in Java?

徘徊边缘 提交于 2020-01-04 04:13:09
问题 I came across some code and cannot understand a certain aspect of it although I have done some extensive searching! My question is: Why are parenthesis used in the middle of a method call? package com.zetcode; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class QuitButtonExample extends JFrame { public QuitButtonExample() { initUI(); } private

How to get a unique method identifier?

点点圈 提交于 2020-01-04 03:49:33
问题 I'm needing to get a unique method identifier to use as a key on a HashMap. I'm trying to do something using stacktrace and reflection and user the method signature. But the problem is I didn´t find a way to retrive the complete method signature (to avoid methods overload). Edited I would like that somethink like this works: public class Class1 { HashMap<String, Object> hm; public Class1() { hm = new HashMap<String, Object>(); } public Object method() { if (!containsKey()) { Object value; ...