instance

Java use of static fields

拥有回忆 提交于 2019-12-10 11:59:50
问题 I am taking java class and I had to do an assignment which reads as follows: Create a class named Purchase. Each Purchase contains an invoice number, amount of sale, and amount of sales tax. Include set methods for the invoice number and sale amount. Within the set() method for the sale amount, calculate the sales tax as 7.5% (using a static filed in the Purchase class) of the sale amount. Also include a display method that displays a purchase's details in a well formatted output display.

Name of the current object/class instance

时光总嘲笑我的痴心妄想 提交于 2019-12-10 11:44:34
问题 I need to load a YAML file (I'm experimenting with SettingsLogic) and I'd like the instance to load the YAML with the same name as it. Briefly: class MySettings < SettingsLogic source "whatever_the_instance_is_called.yml" # Do some other stuff here end basic_config = MySettings.new # loads & parses basic_config.yml advanced_cfg = MySettings.new # loads & parses advanced_cfg.yml ...and so on... The reason for this I don't yet know what configuration files I'll have to load, and typing: my

Convention for namespacing classes vs instances in javascript?

十年热恋 提交于 2019-12-10 11:39:19
问题 I am creating a instance of a class using the following code: test = new function(){ ... } However, base has no prototype because it was created from an anonymous function (I'm guessing this is the reason?). This leaves me unable to create any public functions for the instance. You could argue I could get around this by simply doing: function testClass(){ ... } test = new testClass() and then attaching public functions to testClass. But this forces me to do unnecessary namespacing. In

Matlab class with knowledge of instance name in the constructor

妖精的绣舞 提交于 2019-12-10 10:49:45
问题 I would like to have a class which, in its constructor, can have knowledge (extract as a string) its instance name. For the moment I worked the name extraction out like this: classdef mysession methods (Access = public) function this=mysession (varargin) this.cargs=varargin; this.built=false; end function id=build(this) id=this.mynameis; this.id = id; %% instructions needing id built=true; end function name = mynameis (this) name=evalin ('caller', 'inputname'); end end properties (Access

I need a good analogy to make sense of Class methods vs. instance methods

时光毁灭记忆、已成空白 提交于 2019-12-10 09:55:52
问题 Im getting fairly confused as the book im reading is delving into the NSNumber class and talks about all the different methods you can call on it. I have a couple questions: 1.) Do you not have to call a typical alloc or init on foundation classes? 2.)in what cases would you use, say, numberWithChar: as opposed to initWithChar (i think this is the part that is messing me up the most, not really sure im groking this concept on the level i need to be, if you folks could break it down for me I

Inheritance with a variable in java

∥☆過路亽.° 提交于 2019-12-10 09:42:21
问题 Can anyone clarify me. Here instance method is overridden but variable is not. output is: B 10 class A{ int i=10; public void name(){ System.out.println("A"); } } class B extends A{ int i=20; public void name(){ System.out.println("B"); } } public class HelloWorld { public static void main(String[] args){ A a = new B(); a.name(); System.out.println(a.i); } } 回答1: You cannot override attribute, you can only override method: public class A{ private int i=10; public void name(){ System.out

How to show multiplicities in UML object diagrams

旧街凉风 提交于 2019-12-10 04:00:17
问题 I wonder if there is way to create an array of objects in UML? the following is my try using Modelio as a modeling environment but as you can see there is different objects(i.e. instance[0],instance 1],instance[2],instance[3]) 回答1: As you know there are two ways to show UML attributes in class diagrams: attribute text and association notation. Consider an example of a company and employees. Two valid class diagrams are: Now let's try to draw the corresponding object diagrams: If the details

Constant instance variables?

馋奶兔 提交于 2019-12-10 03:26:33
问题 I use @property to ensure that changes to an objects instance variables are wrapped by methods where I need to. What about when an instance has an variable that logically should not be changed? Eg, if I'm making a class for a Process, each Process instance should have a PID attribute that will frequently be accessed but should not be changed. What's the most Pythonic way to handle someone attempting to modify that instance variable? Simply trust the user not to try and change something they

Why type(classInstance) is returning 'instance'?

…衆ロ難τιáo~ 提交于 2019-12-10 02:21:24
问题 I have a method that accepts a parameter that can be of several types, and has to do one thing or other depending on the type, but if I check the type of said parameter, I don't get the 'real' type, I always get <type 'instance'> , and that is messing up with my comparisons. I have something like: from classes import Class1 from classes import Class2 # Both classes are declared in the same file. # I don't know if that can be a problem # # ... # def foo(parameter) if (type(parameter) == type

python之类属性和实例对象属性

一曲冷凌霜 提交于 2019-12-09 20:25:58
python中的实例对象属性(Instance Attributes)就是跟这个对象绑定的简单的数据而已。实例对象属性一般在__init__函数中初始化,但是在其它方法中也是可以随意添加新的属性。 class Foo(object): def __init__(self, name): super(Foo, self).__init__() self.name = name // 实例对象属性 def fun(self): self.sugar = "whatever" // 给实例添加属性,但并不提倡这么做,所有的实例属性最好在__init__中给出 需要注意的是,self指代是实例化后对象。 类属性可以理解为跟类绑定的数据,跟实例无关,但是我们可以通过实例化对象引用类属性。python核心编程中有一句描述我认为描述不当。( When an instance is created, Python provides a default, read-only instance attribute which is an alias to the class attribute.)实际上,当实例化对象的时候,新的实例得到是类属行的一个引用,这个引用不能说是可读的,可读不可读都是针对所引用的这个属性是否可变,数字,字符串是不可边的数据类型,而list或者自定义对象等都是可变类型