#include <iostream>
#include <cstring>
#include <QString>
using namespace std;
class A {
public:
static const int i = 9;
static const int PI = 1.3;
static const char ch = 's';
static const string str = "hello world"; // <--- error
static const QString str2 = "hello world"; // <--- error
};
int main(int argc, char **argv) {
cout << "Hello world" << endl;
return 0 ;
}
Run Code Online (Sandbox Code Playgroud)
由于代码提供了所有内容,我如何初始化字符串.
在类C#3.5中,我有一些带有一些变量的静态方法.即使我没有初始化类,静态方法也会先被初始化.
那么静态方法中的变量是否也在开始时初始化而不是垃圾收集?
我想知道 - 即使我没有调用方法或者我只调用一次方法并且方法退出,是否会为这样的方法变量分配内存?或者每次调用方法并退出时 - 方法中的变量都是垃圾收集的?
我想在我的Util类中创建一个静态方法,它将以日期格式返回当前时间.所以我尝试了下面的代码,但它总是同时返回.
private static Date date = new Date();
private static SimpleDateFormat timeFormatter= new SimpleDateFormat("hh:mm:ss a");
public static String getCurrentDate() {
return formatter.format(date.getTime());
}
Run Code Online (Sandbox Code Playgroud)
如何在不创建Util类实例的情况下以特定格式获取更新时间.可能吗.
我正在pycassa中做一个数据库插入脚本.我想设置一个公共静态类,它定义了一些变量,这些变量稍后会被其他函数大量使用.继承人我拥有的......
class ks_refs():
pool = ConnectionPool('TweetsKS')
user_name_cf = self.cf_connect('UserName')
user_tweet_cf = self.cf_connect('UserTweet')
def cf_connect(column_family):
cf = pycassa.ColumnFamily(self.pool, column_family)
return cf
Run Code Online (Sandbox Code Playgroud)
我还没有尝试过这个,因为我相信它不会工作.你可以看到我首先想要这个静态变量'pool',然后使用需要'pool'工作的cf_connect方法设置user_name_cf和user_tweet_cf(以及更晚一些).
我知道我可以把这个方法放在课外,或者我可以让这个非静态并创建它的实例,但我想尝试这个,因为这是我真正想要的(在我使用全局变量之前,但我认为保持这一切的静态类是最好的主意)
我是一名本科大学生,试图理解Java中的继承.docs.oracle站点表示类的所有成员都是继承的,但构造函数除外.这是有道理的.问题是我做了一个实验而且没有用.这里是:
public class One{
public void outPrint(){
System.out.println("Hello World!");
}//end outPrint
}//end One.java
public class Two extends One{
//empty
}//end Two.java
public class Three extends Two{
public static void main(String[]args){
outPrint();
}//end main
}//end Three.java
Run Code Online (Sandbox Code Playgroud)
当我运行Three时,我得到:非静态方法outPrint()不能从静态上下文中引用.这当然是因为编译器将outPrint()视为实例成员.如果我将关键字"static"添加到outPrint()方法标题中,整个过程就可以了.
这就是我的困惑所在.它似乎不仅仅是不可继承的构造函数,而且还有它的所有实例成员.有人能解释一下这对我好一点吗?有没有涉及使用"静态"的解决方法?我尝试了一些"超级"的实验无济于事.提前致谢!
在java,java.lang.Systemclass中,它有一个in静态变量.
声明为:public static final InputStream in
this
表示in是一个InputStream varibale.
但是我看到一些例子,
System.in.read()用于读取输入.
怎么能这样做,read()InputStream中的方法不是静态方法,怎么能直接调用呢?据我了解,只有静态方法可以直接由类调用而无需创建实例.
read()声明: public abstract int read() throws IOException
谢谢Jon Skeet的回答,我还有一点不明白.
如果我调用System.in.read()哪个意味着我调用InputStream类方法read()?
java.lang.System.in -----> java.io.InputStream ----> read()
Run Code Online (Sandbox Code Playgroud)
java.lang.System.in(这是一个静态变量),in是一个java.io.InputStream变量PrintStream.read()它应该像这样调用:
PrintStream rd = new PrintStream(); int c = rd.read();
因为read()应该由实例调用.read()声明:public abstract int read()抛出IOException
PS:我试试这段代码不起作用:
InputStream rd = new InputStream();
int c = rd.read();
System.out.println(c);
Run Code Online (Sandbox Code Playgroud)
但不知道为什么.
参考:http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html
我想创建一个静态类,其作用类似于枚举,但具有字符串值.以下哪种方法最安全地提取已创建类的完整实例?
public class Name
{
public static final Name MY_NAME = new Name("Chris", "Doe");
public String firstName;
public String lastName;
public Name(firstname, lastname)
{
this.firstName = firstname;
this.lastName = lastname;
}
}
Run Code Online (Sandbox Code Playgroud)
要么
public class Name
{
public String firstName;
public String lastName;
public Name(firstname, lastname)
{
this.firstName = firstname;
this.lastName = lastname;
}
public static Name myName()
{
return new Name("Chris", "Doe");
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用php 5.2.6处理遗留代码库的项目.该项目的一部分涉及类似于使用一个PHP类与另一个PHP类的A/B测试.这两个类具有许多相同的函数名,具有非常相似的签名,但方法中的方法不同.我想知道是否可以使用基于动态/变量的类名称在类中调用静态方法.
例如,id喜欢将类名设置为:
$class = isset($some_condition) && $some_condition ? 'NewClassName' : 'LegacyClassName';
Run Code Online (Sandbox Code Playgroud)
...然后调用函数:
$class::myStaticFunction();
Run Code Online (Sandbox Code Playgroud)
我知道这可以用call_user_func()完成,但我很难找到替代方法(如果有的话).我只是宁愿不用正则表达式用call_user_func()语句替换对遗留类的所有调用.
例如,
$stuff = call_user_func($class . '::myStaticFunction()');
Run Code Online (Sandbox Code Playgroud)
......工作得很好.
有没有人知道是否有更简单的表达方式:$ dynamicClassName :: staticFunction()PHP 5.2?也许我错过了我的语法等等.
我迷路了,我甚至不知道要问什么问题.我有这个程序,应该能够构造一个参数半径和高度的圆柱体.然后它应该能够调用各种方法来获取和设置半径以及输出表面积和体积.我无法通过去因为我不能在我的主要内容中放置任何内容而没有关于静态中使用的静态非静态方法的错误.我甚至都不知道这意味着什么.我实际上将其他人的代码复制到我的编译器中,它给了我同样的错误.我搞砸了一些设置吗?我知道这对Stack Overflow来说可能太过初级,但我现在绝望了.
public class Miller_A03Q1 {
public static void main(String[] args) {
Cylinder cylinder1 = new Cylinder(1,17);
Cylinder cylinder2 = new Cylinder(3,8);
Cylinder cylinder3 = new Cylinder(2,12);
Cylinder cylinder4 = new Cylinder (1,14);
}
public class Cylinder{
private double radius = 0.0;
private double height= 0.0;
private double area = 0.0;
private double volume=0.0;
private String shape = "cylinder";
public Cylinder(double r,double h){
this.radius = r;
System.out.print(r);
this.height = h;
System.out.print(h);
}
public double getVolume(){
double volume = 3.14 * …Run Code Online (Sandbox Code Playgroud) 我最近偶然发现了一段我无法用java理解的代码.我已经创建了同一种结构的小版本:
public class StaticMethodClass {
static void IDK(Integer i, String somethingElse) {
System.out.println("It has been done");
}
public static void main(String... whatever) {
Action.ITEM.doStuff();
}
}
interface Func {
void exec(Integer i, String somethingElse);
}
enum Action {
ITEM(StaticMethodClass::IDK);
private Func f;
Action(Func f){
this.f = f;
}
void doStuff() {
this.f.exec(1, "huh");
}
}
Run Code Online (Sandbox Code Playgroud)
我不能像Action.ITEM一样构建它,因为它应该得到一个实现Func接口的类.相反,它传递了一个方法,它以某种方式隐式转换.
我的问题是这是如何工作的,以及这里适用的规则.