class-instance-variables

Java how to call method in another class

戏子无情 提交于 2021-02-04 21:39:15
问题 Sorry for the basic question but I'm learning, new to programming. I am trying to call a method that is in another class but I'm unable to without passing in the required values for the constructor of Cypher class. Do i really need a new instance of Cypher class each time i use a method in the Menu class and want to call a method of the Cypher class or how do I rewrite to avoid below. public class Runner { private static Cypher c; public static void main(String[] args) { Menu m = new Menu();

Should I use class variables or class-instance variables for class static variables in Ruby?

淺唱寂寞╮ 提交于 2020-02-02 11:29:34
问题 class Something @@variable = 'Class variable' def give_me @@variable end end class OtherThing @variable = 'Instance variable with an interface' class << self attr_accessor :variable end def give_me self.class.variable end end p Something.new.give_me p OtherThing.new.give_me What I want to know is, which one should I use? Which are the benefits and cons of each? Class variables are: Private unless you make an interface Shared between inheritances Shorter to write Class-instance variables are:

Should I use class variables or class-instance variables for class static variables in Ruby?

♀尐吖头ヾ 提交于 2020-02-02 11:27:05
问题 class Something @@variable = 'Class variable' def give_me @@variable end end class OtherThing @variable = 'Instance variable with an interface' class << self attr_accessor :variable end def give_me self.class.variable end end p Something.new.give_me p OtherThing.new.give_me What I want to know is, which one should I use? Which are the benefits and cons of each? Class variables are: Private unless you make an interface Shared between inheritances Shorter to write Class-instance variables are:

How can Ruby's attr_accessor produce class variables or class instance variables instead of instance variables?

天涯浪子 提交于 2019-12-28 08:11:41
问题 If I have a class with an attr_accessor , it defaults to creating an instance variable along with the corresponding getters and setters. But instead of creating an instance variable, is there a way to get it to create a class variable or a class instance variable instead? 回答1: Like this: class TYourClass class << self attr_accessor :class_instance_variable end end You can look at this as opening the metaclass of the class (of which the class itself is an instance) and adding an attribute to

ruby: class instance variables vs instance variables

我的梦境 提交于 2019-12-28 01:28:11
问题 my idea is to create a community wiki for people that come from a java background because reading a lot of explanations, I couldn't comprehend anything until I actually tried a couple of things and the pieces of the puzzle started to find their places. But I first need to make sure I'm getting it right. Coming from such background it was very confusing for me to find out that @variable may mean 2 very different things. Here is an example: class Test @ins = "gah" def self.ins puts @ins end def

Difference between class variables and class instance variables?

℡╲_俬逩灬. 提交于 2019-12-27 10:14:33
问题 Can anyone tell me about the difference between class variables and class instance variables? 回答1: A class variable ( @@ ) is shared among the class and all of its descendants. A class instance variable ( @ ) is not shared by the class's descendants. Class variable ( @@ ) Let's have a class Foo with a class variable @@i , and accessors for reading and writing @@i : class Foo @@i = 1 def self.i @@i end def self.i=(value) @@i = value end end And a derived class: class Bar < Foo end We see that

Difference between class variables and class instance variables?

十年热恋 提交于 2019-12-27 10:14:13
问题 Can anyone tell me about the difference between class variables and class instance variables? 回答1: A class variable ( @@ ) is shared among the class and all of its descendants. A class instance variable ( @ ) is not shared by the class's descendants. Class variable ( @@ ) Let's have a class Foo with a class variable @@i , and accessors for reading and writing @@i : class Foo @@i = 1 def self.i @@i end def self.i=(value) @@i = value end end And a derived class: class Bar < Foo end We see that

Is there any way to get class instance attributes without creating class instance?

眉间皱痕 提交于 2019-12-24 15:41:36
问题 Here is the link to my first question: Creating class instance from dictionary? So I am trying to create class instance from dictionary which holds keys that class has not. For example: class MyClass(object): def __init__(self, value1, value2): self.attr1 = value1 self.attr2 = value2 dict = {'attr1': 'value1', 'attr2': 'value2', 'redundant_key': 'value3'} Before creating class instance I have to delete this redundant_key from dict dict.pop('redundant_key', None) new_instance = MyClass(**dict)

Auto-incrementing IDs for Class Instances

落花浮王杯 提交于 2019-12-19 10:22:47
问题 Disclaimer : This is for a semester project that I am currently working on. My question is regarding an implementation level detail and is not part of the grading scheme. I am only writing this code as a way to test the theory that I am proposing for the paper that I will write. Also, I have considered the answers for this question with little luck, so please do not consider this as a duplicate of that question The Problem : I have a graph (G=(V,E)). At some point in my algorithm, I need to

How do I use dictionary key,value pair to set class instance attributes “pythonic”ly?

荒凉一梦 提交于 2019-12-13 13:17:38
问题 I have created some Python classes to use as multivariate data structures, which are then used for various tasks. In some instances, I like to populate the classes with various value sets. The default parameter filename "ho2.defaults" would look something like this: name = 'ho2' mass_option = 'h1o16' permutation = 'odd' parity = 'odd' j_total = 10 lr = 40 br = 60 jmax = 60 mass_lr = 14578.471659 mass_br = 1781.041591 length_lr = ( 1.0, 11.0, 2.65 ) length_br = ( 0.0, 11.0, 2.46 ) use_spline =