除了 main() 之外,我在 java 程序中声明了三个静态方法
方法。由于程序运行时首先执行静态方法,因此
方法将首先执行?它会是我拥有的静态方法之一吗
声明,或者它将是主要方法?
我正在尝试为我的程序创建一个方法,当被调用时,会要求您按Enter继续.但是,BlueJ给了我一条错误信息,说"预期".以下是我的代码
注意:continue类还没有完全完成.
import java.util.Scanner;
public class BlackjackRunner
{
public static void main (String[]args)
{
System.out.print("Ready to play Blackjack(y/n)? =====> ");
Scanner input = new Scanner(System.in);
String response = input.nextLine();
System.out.println();
if(response.charAt(0) != 'y' || response.charAt(0)!= 'Y')
System.out.println("Too bad, you play");
else
System.out.println("Good.");
}
public static void*** continue()
{
System.out.println("-----PRESS ENTER TO CONTINUE-----");
Scanner wait = new Scanner(System.in);
}
}
Run Code Online (Sandbox Code Playgroud)
BlueJ在void语句之后给出了一条错误消息(其中***).我不确定为什么这是不正确的.如果有人可以解释/帮助,那将是美好的.
我刚刚开始我的C#编程中级课程,我正在学习如何创建多个类并创建在我的程序中使用的方法.
这对我来说是一个非常新的话题,所以如果它是非常明显或愚蠢的话我会道歉.我在所有方法中都得到了以下信息:
Cannot access static method in non-static context
Run Code Online (Sandbox Code Playgroud)
方法类中的代码:
public static int Add(params int[] numbers) {
var sum = 0;
foreach (var n in numbers) {
sum += n;
}
return sum;
}
public static int Subtract(params int[] numbers) {
var sum = 0;
foreach (var n in numbers) {
sum -= n;
}
return sum;
}
public static int Multiply(params int[] numbers) {
var sum = 0;
foreach (var n in numbers) {
sum *= n;
}
return …Run Code Online (Sandbox Code Playgroud) 我正在努力学习Java.
我不明白为什么这段代码不起作用.
它不会Hello World从test()函数输出" " .
我究竟做错了什么?
public class Main {
public test(args) {
System.out.println(args);
}
public static void main(String[] args) {
test('Hello World');
}
}
Run Code Online (Sandbox Code Playgroud) 我想我已经知道了这个问题的答案,因为看来,呃,有点牵强.但我正在认真寻找解决方案.
假设我有一个Activity,称之为蓝牙活动,启动蓝牙背景线程,即使蓝牙活动消失,线程也会永远运行 - 是的,它确实会永远运行.这个蓝牙背景线程是一个数据采集线程,它不断地收集由Activity绘制的数据,实时称之为Plot活动.我们可以将绘图方法称为一个名为Plot.plotData()的静态方法;
我的问题是我无法判断Plot活动何时处于活动状态,因此我无法确定何时开始调用Plot.data().你可能会认为,因为Plot.plotData()是一个静态方法,我可以随时调用它.但事实并非如此.我必须等到Plot通过Intent()和startActivity实例化.否则Plot的onCreate()方法尚未调用,Plot充满了空指针.
我该如何解决这个问题.添加一个最初为false的静态getter/setter,直到onCreate运行?
我现在正在编写很多java,所以我对java 静态方法和c ++ 静态函数感到困惑.
在java中,你可以从类中调用静态方法,我经常使用/看到它,例如:
public class A{
public void static b(){
System.out.println("hello");
}
}
Run Code Online (Sandbox Code Playgroud)
你能做到,A.b();你能用C++做到吗?如果是这样,与在java中这样做相比,它不是那么受欢迎吗?
我有非常简单的代码,我删除了奇数代码.
所以这是我的班级,他的一个方法是静态的,我想稍后使用它Main class:
public class TradeInformationReader {
private static String tradeType = "FX_SPOT";
public static double tradePrice = -1;
private double price;
public static int setTradeInformation(String path_to_file) {
return 1;
}
}
Run Code Online (Sandbox Code Playgroud)
在这里我如何尝试调用最后一个方法:
public class Main {
public static int main(String[] args) {
String path_to_file = "D:\\1.txt";
if (0 > TradeInformationReader.setTradeInformation(path_to_file)) {
return -1;
}
return 1;
}
}
Run Code Online (Sandbox Code Playgroud)
我阅读了许多类似问题的帖子,但找不到解决方案.一切看起来都很好.IDE没有显示任何错误,我只是想调用静态方法setTradeInformation,为什么它不能识别它(找不到符号方法setTradeInformation)?有任何想法吗?我将非常感谢你的帮助.
私有非静态变量/方法可以在静态函数中访问吗?如果是,那么“专用”访问修饰符的用途是什么?请检查以下代码。
// Testclassheader.h file
class TestClass{
private:
int TestVariable; //Private Variable
int TestFunction(); //Private Method
public:
static void TeststaticFn(); //Static Method
};
void TestClass::TeststaticFn()
{
TestClass TestObj;
TestObj.TestVariable = 10; // Compiles perfectly
TestObj.TestFunction(); //Compiles Perfectly
}
// Another Class
//#include "Testclassheader.h"
class AnotherClass{
public:
int DummyFunc();
};
int AnotherClass::DummyFunc()
{
TestClass AnotherObj;
AnotherObj.TestVariable = 15; //error: 'TestVariable' is a private member of 'TestClass'
AnotherObj.TestFunction(); //error: 'TestFunction' is a private member of 'TestClass'
}
Run Code Online (Sandbox Code Playgroud)
我在Visual Studio 12中尝试了上述代码。谁能解释为什么私有变量/方法可以在静态方法中访问(实际上不应该)?
我刚读过(第k次)
这是关于模拟虚拟静态成员的问题.我的问题是 - 是什么让C++标准委托(或之前的Bjarne Stroustrup)没有将此功能添加到C?他们知道会破坏什么吗?或妨碍任何事情的表现(即使不使用)?
为了更好地说明我对功能定义本身的看法,这里有一些代码:
// This does not compile!
class Base {
// A pure virtual member - perhaps need to indicate that somehow
virtual static const string ToWhom;
void sayHello() {
cout << "Hello, " << ToWhom << "!" << endl;
}
};
class World : public Base {
static virtual const string ToWhom = "the entire world"s; // C++14 literal
};
class Everybody : public Base {
static virtual const string ToWhom = "everybody around"s; …Run Code Online (Sandbox Code Playgroud) 我想知道在静态类中,所有方法和数据成员应该是静态的还是可以找到非静态成员?
下面的 static 关键字代码在调用函数之前和之后给出了两个不同的输出
#include<stdio.h>
static int count=5;
int fun()
{
count = 0;
count++;
return count;
}
int main()
{
printf("%d ", count);
printf("%d ", fun());
printf("%d ", fun());
printf("%d ", count);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:5 1 1 1
问)为什么 count 给出两个不同的值;一开始是5,调用函数后就变成1了?