class

Accessing variables in other classes (Java)

与世无争的帅哥 提交于 2020-01-04 05:09:30
问题 Why doesn't the following program return 0 , since I am accessing p from a new A() , which has not had main called on it? public class A { public static int p = 0; public static void main(String[] args) { p = Integer.parseInt(args[0]); new B().go(); } } class B { public void go() { System.out.println(new A().p); } } 回答1: That should not even compile. Probably you had p as static and than you change it. The way it is written now, doesn't compile. $ javac A.java A.java:7: non-static variable p

Accessing variables in other classes (Java)

僤鯓⒐⒋嵵緔 提交于 2020-01-04 05:09:12
问题 Why doesn't the following program return 0 , since I am accessing p from a new A() , which has not had main called on it? public class A { public static int p = 0; public static void main(String[] args) { p = Integer.parseInt(args[0]); new B().go(); } } class B { public void go() { System.out.println(new A().p); } } 回答1: That should not even compile. Probably you had p as static and than you change it. The way it is written now, doesn't compile. $ javac A.java A.java:7: non-static variable p

Declare Lambda Expression as a class constant field

喜夏-厌秋 提交于 2020-01-04 04:43:04
问题 Why isn't it possible to declare a class constant filed of type Lambda Expression . I want Something like this: class MyClass { public const Expression<Func<string,bool>> MyExpr = (string s) => s=="Hello!"; } But I get the compile error: Expression cannot contain anonymous methods or lambda expressions 回答1: This is just a limitation of C# and CLR. Only primitive numeric values, string literals and null can be used as a value of a constant field. Expression trees are represented as a normal

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

Easier way to make class to work with operators?

﹥>﹥吖頭↗ 提交于 2020-01-04 03:58:11
问题 Here, I have a class called Value which simply can get and set float . class Value { public: Value(float f) :f(f){}; float get() { return f; } void set(float f) { this->f = f; } private: float f; }; And I want my class to be able to work like the following example. Value value(3); std::cout << value * 2 - 1 << std::endl; // -> 5 std::cout << value == 5 << std::endl; // -> true value /= 2; std::cout << value << std::endl; // -> 2.5 Should I manually add all operator methods to my class? Or

Why is it not possible to access static fields of a class via Type.getClass()?

随声附和 提交于 2020-01-04 03:58:08
问题 In Haxe, it is possible to get the class of an object with the following function: Type.getClass(myObject); If the object myObject is an instance of the class myClass , which contains a static field, I should be able to access this static field: class MyClass { public static myStaticField:Int = 5; } public var myObject = new MyClass(); //expected trace: "5" trace (Type.getClass(myObject).myStaticfield); But the result is: "Class <MyClass> has no field myStaticField." Any idea why? 回答1: You

How to convert lists of class objects to a list of their attributes

假如想象 提交于 2020-01-04 03:18:34
问题 guys! So I recently started learning about python classes and objects. For instance, I have a following list of strings: alist = ["Four", "Three", "Five", "One", "Two"] Which is comparable to a class of Numbers I have: class Numbers(object): One=1 Two=2 Three=3 Four=4 Five=5 How could I convert alist into alist = [4, 3, 5, 1, 2] based on the class above? My initial thought was to create a new (empty) list and use a for loop that adds the corresponding object value (e.g. Numbers.One ) to the

Get attributes for class and instance in python

ぐ巨炮叔叔 提交于 2020-01-04 03:12:23
问题 In python work next code: class MyClass(object): field = 1 >>> MyClass.field 1 >>> MyClass().field 1 When I want return value for custom fields I use next code: class MyClass(object): def __getattr__(self, name): if name.startswith('fake'): return name raise AttributeError("%r object has no attribute %r" % (type(self).__name__, name)) >>> MyClass().fake fake But: >>> MyClass.fake Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: class MyClass has no

Netbeans deleting a built class?

≯℡__Kan透↙ 提交于 2020-01-04 02:28:09
问题 I have a NetBeans project with quite a few classes in it. I've been working on this project for the past 3 weeks, and just started having this issue today. When clicking the "Run Main Project" button in NetBeans, I see the following error: Exception in thread "main" java.lang.NoClassDefFoundError: stockscreener/Stock Caused by: java.lang.ClassNotFoundException: stockscreener.Stock at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native