class

Can __radd__ work with the operands in any order?

一笑奈何 提交于 2019-12-24 13:15:53
问题 I want my Fraction class to work as a float when it's being added to floats or integers so I can naturally perform operations with it, but it's only working when the Fraction is the rightmost operand. Is there a way to make it work with the operands in any order or should I override another method that I haven't learned of? Code (I guess variable names are pretty self-explanatory): def __radd__(self,target): if type(target) == int or type(target) == float: return target + self.num/self.den 1

Calling an SQL Connection method in C#

僤鯓⒐⒋嵵緔 提交于 2019-12-24 13:14:23
问题 I am calling this method to my Login Form. I don't know what is wrong with this. I've created a class named MyConnection and this class contains my SQL ConnectionString. What i want is I can call this function ex. Class1 method named Myfunction would be called to my Login Form so calling a connection string would be faster. public static class MyConnection { public static SqlConnection getConnection() { string conn = "Data Source=EDGAR-PC\\SQLEXPRESS;Initial Catalog=Project1;Integrated

How to create array of pointers in C++

爷,独闯天下 提交于 2019-12-24 12:45:11
问题 In class Node : class Node { public: int data; int numchild; Node** nodelist; Node(int data, int s); }; I want an array of pointers ( nodelist ) to other nodes, which have edges from this node. Is the following way of constructing such an array (and the above way of declaring it) a correct and the best (or easiest) way possible? If not, why and whats the best way? Node::Node(int d, int s) { data = d; numchild = s; nodelist = new Node*[s]; } 回答1: Generally you shouldn't reinvent the wheel. Raw

Cannot set variable in class equal to a function

送分小仙女□ 提交于 2019-12-24 12:25:39
问题 I started today with learning object oriented PHP programming and I am struggling with the following problem: I can set a variable equal to for example 10: class exampleClass { private $number = 10; } But I cannot set it equal to a function which returns 10: class exampleClass { private $number = exampleFunction(); } 回答1: You can't set class properties directly as expressions: Invalid: class Test { protected $blah = 1 + 1; } Instead set it in the class construct: class Test { protected $blah;

Object as data attribute of class in Class diagram UML

只谈情不闲聊 提交于 2019-12-24 12:17:42
问题 Is the following format wrong if I add a pointer to an object of a class, as data attribute of class in Class diagram in UML? 回答1: could not find anything about using objects in class diagram, is underlining the object correct within the class attributes? I think you may be mis-understanding classes, objects and attributes. Apologies if it's me doing the mis-understanding. So. Here's the short answer: it's absolutely fine and normal for the type of an attribute to be a Class. In other words,

AS3 Modify stage Objects from a class

拟墨画扇 提交于 2019-12-24 12:14:14
问题 In an external class i'm trying to modify a property of a stage object, Apple. I want to set it visible so i put in my code: Apple.visible =true; but it says that Apple is not defined, probably because it doesn't refeers to the stage one... how can I "import" it in my class? 回答1: Try something like: DisplayObjectContainer(stage.getChildAt(0)).getChildByName("Apple").visible = true; where stage.getChildAt(0) is "Main Timeline" movie, that contains all inner objects. 来源: https://stackoverflow

Understanding JavaScript classes - resource class

眉间皱痕 提交于 2019-12-24 12:13:54
问题 Working on a simple video game in JavaScript / HTML5, and I had a thought to collect all of my resources within one class...currently they're spread all over the place. So, for an example, currently I have something along the lines of function c_enemy_sprites() { this.image = new Image(); this.image.src = "res/enemies.png"; .. .. } function c_tilemap() { this.image = new Image(); this.image.src = "res/tilemap.png"; .. .. } I'd like to commonize this into a single class, as so function c

Mock native method with a Robolectric Custom shadow class

假装没事ソ 提交于 2019-12-24 12:04:04
问题 I have a class with a regular method and a native method that I'd like to mock: public class MyClass { public int regularMethod() { ... } public void native myNativeMethod(); } I am using Robolectric to test my app, and I am trying to figure out a way to mock these methods using a custom shadow class. Here is my shadow class: @Implements(MyClass.class) public class MyShadowClass { @Implementation public int regularMethod { return 0; } @Implementation public void nativeMethod { ... } } Here is

how to pass a method from different class to another class in Java?

余生颓废 提交于 2019-12-24 12:03:52
问题 There are 2 files named: AnnuityDueGUI.java AnnuityDueResultGUI.java Under AnnuityDueGUI.java, there is this method as below: ============= public double calculateFADGUI(){ //FVA = A{[(1+i)^n – 1] / i} (1+i) String amountStr = amount.getText() ; //convert string to double dAmount = Double.parseDouble(amountStr) ; String iStr = iText.getText() ; dInterest = Double.parseDouble(iStr) ; String periodStr = period.getText() ; dPeriod = Double.parseDouble(periodStr) ; iPeriod = (int)dPeriod ; due =

Class initialization without __init__ method

回眸只為那壹抹淺笑 提交于 2019-12-24 11:37:36
问题 While going through scapy source code (https://github.com/jwiegley/scapy), I came across the fact none of the Ether , IP , TCP , UDP or any other protocol classes contain any __init__ method, nor they have any class methods with @classmothod annotation. All of this classes inherit the Packet class, which by the way contains __init__ method. Code structure is like this : class Ether(Packet): # class methods class IP(Packet, IPTools): # class methods # Other protocol classes So, I am wondering