整个问题在标题中.例如:
enum enumTest {
TYPE1(4.5, "string1"), TYPE2(2.79, "string2");
double num;
String st;
enumTest(double num, String st) {
this.num = num;
this.st = st;
}
}
Run Code Online (Sandbox Code Playgroud)
构造函数可以使用默认值或private修饰符,但如果给定public或protected修饰符,则会给出编译器错误.
我不明白为什么一个字节可以采用的最低值-128.我可以看到最高值是127,因为它是01111111二进制的,但是如何-128只用8位表示,其中一个用于符号?正128已经是8位,即10000000,然后你需要第9位来表示负号.
请有人帮我解释一下这个问题.
这可能是一个非常荒谬的问题,但事实是我的Code :: blocks在编译时不会显示错误 - 它只会在违规行旁边显示一个红色条,如屏幕截图所示.此外,当我的代码运行并具有输出时,CB打开一个新窗口,而不是像其他IDE那样在编辑器窗口底部的窗格中显示输出.如何启用其中之一/两者,因为它们可能是相同的功能?谢谢!
我在Ruby中导入文件时遇到了麻烦.我在Windows XP中制作一个Ruby应用程序.该应用程序的所有类文件都在"C:/Documents/Prgm/Surveyor_Ruby/lib".但是当我require在另一个文件中的文件时,ruby和irb都无法找到所需的文件.
当前目录的内容:
C:\Documents\Prgm\Surveyor_Ruby\lib>dir
Volume in drive C has no label.
Volume Serial Number is AAAA-BBBB
Directory of C:\Documents\Prgm\Surveyor_Ruby\lib
10/09/2010 06:32 PM <DIR> .
10/09/2010 06:32 PM <DIR> ..
10/08/2010 03:22 PM 5,462 main (commented).rb
10/08/2010 03:41 PM 92 question.rb
10/08/2010 09:06 PM 2,809 survey.rb
10/09/2010 06:25 PM 661 surveyor.rb
10/08/2010 01:39 PM 1,546 test.rb
5 File(s) 10,570 bytes
2 Dir(s) 40,255,045,632 bytes free
Run Code Online (Sandbox Code Playgroud)
确认irb在正确的目录中:
C:\Documents\Prgm\Surveyor_Ruby\lib>irb
irb(main):001:0> Dir.pwd
=> "C:/Documents/Prgm/Surveyor_Ruby/lib"
Run Code Online (Sandbox Code Playgroud)
...但是irb无法加载survey.rb:
irb(main):002:0> require 'survey'
LoadError: no …Run Code Online (Sandbox Code Playgroud) 我们公司保留一个MiscUtilities类,它只包含公共静态方法,这些方法通常执行不相关的任务,例如将日期从String转换为Calendar以及将ArrayLists写入文件.我们在其他类中引用它并发现它非常方便.但是,我见过那种公用事业类嘲笑TheDailyWTF.我只是想知道这类课程是否有任何实际的缺点,以及替代方案是什么.
编辑:好的,好的,我误读了.我不是将int与Integer进行比较.正好指出.
我的SCJP书说:
当==用于将基元与包装器进行比较时,包装器将被解包,并且比较将是原始的.
所以你认为这段代码会打印出来true:
Integer i1 = 1; //if this were int it'd be correct and behave as the book says.
Integer i2 = new Integer(1);
System.out.println(i1 == i2);
Run Code Online (Sandbox Code Playgroud)
但它打印出来false.
另外,根据我的书,这应该打印true:
Integer i1 = 1000; //it does print `true` with i1 = 1000, but not i1 = 1, and one of the answers explained why.
Integer i2 = 1000;
System.out.println(i1 != i2);
Run Code Online (Sandbox Code Playgroud)
不.是的false.
是什么赋予了?
我正在准备SCJP,多线程一直是我最讨厌的领域,主要是因为我不知道如何查看多线程代码并完成它.到目前为止,我的方法是用英语写下每个线程中可能发生的事情,并测试一些线程随机相互交叉的情况,这是一个非常重要且耗时且耗时的方法.所以我想看看专业人士会怎么做.你是否愿意阅读下面的代码(这是给我带来麻烦的最新问题)并记下你脑子里的内容(只有代码相关的东西,请:),当你计算出可能的输出时?问题附带的选择是最终的.我正在寻找的不是我所拥有的解决方案,而是如何在考试中有效地达到解决方案.
是的,我知道这个问题没有一个确切的答案等等.接受投票的答案最明确,最容易模仿,好吧:)
感谢大家!
问题:这些答案中哪一个是可能的输出?
public class Threads1 {
int x = 0;
class Runner implements Runnable {
public void run() {
int current = 0;
for (int i = 0; i < 4; i++) {
current = x;
System.out.print(current + ", ");
x = current + 2;
}
}
}
public static void main(String[] args) {
new Threads1().go();
}
public void go() {
Runnable r1 = new Runner();
new Thread(r1).start();
new Thread(r1).start();
}
}
Run Code Online (Sandbox Code Playgroud)
选择(选择所有适用的选项):
A. 0,2,4,4,6,8,10,6,
B. 0,2,4,6,8,10,2,4,
C. …
更新:我明白了.Ctrl-F仅在未选择我正在搜索的方法时有效.游标只需要是方法名称.
我刚升级到TextMate2.
当我选择一个方法并使用Ctrl+ F来定义它时,我得到:
> Failure running Jump to Method Definition
Run Code Online (Sandbox Code Playgroud)
这是跟踪:
/Users/ilikepie/Library/Application Support/TextMate/Managed/Bundles/Ruby on Rails.tmbundle/Support/lib/rails/text_mate.rb:54:in `method_missing': undefined method `current_line' for TextMate:Module (NoMethodError)
from /Users/ilikepie/Library/Application Support/TextMate/Managed/Bundles/Ruby on Rails.tmbundle/Support/bin/jump_to_method_definition.rb:13:in `initialize'
from /Users/ilikepie/Library/Application Support/TextMate/Managed/Bundles/Ruby on Rails.tmbundle/Support/bin/jump_to_method_definition.rb:147:in `new'
from /Users/ilikepie/Library/Application Support/TextMate/Managed/Bundles/Ruby on Rails.tmbundle/Support/bin/jump_to_method_definition.rb:147
Run Code Online (Sandbox Code Playgroud)
我也可以选择编辑命令".这是"跳转到方法"定义的代码:
#!/usr/bin/env bash
[[ -f "${TM_SUPPORT_PATH}/lib/bash_init.sh" ]] && . "${TM_SUPPORT_PATH}/lib/bash_init.sh"
RUBYLIB="$TM_BUNDLE_SUPPORT/lib:$RUBYLIB"
"${TM_RUBY:=ruby}" -- "${TM_BUNDLE_SUPPORT}/bin/jump_to_method_definition.rb"
Run Code Online (Sandbox Code Playgroud)
我错过了一个环境变量吗?
样本1:
class Animal {
public static void saySomething() { System.out.print(" Gurrr!");
}
}
class Cow extends Animal {
public static void saySomething() {
System.out.print(" Moo!");
}
public static void main(String [] args) {
Animal [] animals = {new Animal(), new Cow()};
for( Animal a : animals) {
a.saySomething();
}
new Cow().saySomething();
}
}
Run Code Online (Sandbox Code Playgroud)
输出是:
Gurrr! Gurrr! Moo!
Run Code Online (Sandbox Code Playgroud)
样本2:
class Animal {
public void saySomething() { System.out.print(" Gurrr!");
}
}
class Cow extends Animal {
public void saySomething() {
System.out.print(" Moo!"); …Run Code Online (Sandbox Code Playgroud) 这是SCJP的另一个问题.下面的代码打印出来Alpha:fooBeta:fooBeta:barBeta:bar,我不明白为什么第一个foo调用选择Alpha的foo而不是Beta的.如果Alpha.foo参数更改为String而不是String ...,那么输出是Beta:fooBeta:fooBeta:barBeta:bar有意义的.
我的理解是,当你说Alpha a = new Beta();,编译器检查Alpha.foo,但JVM实际上将运行Beta.foo.首先,Beta确实有一个foo方法,其签名与调用匹配.另一方面,我认为varargs方法只在没有其他可用的方法与调用匹配时运行.所以这就是我认为不应该运行Alpha.foo的两个原因.这种理解的哪一部分是错的?
谢谢!
class Alpha {
public void foo(String... args) { //if this were String args, then it makes sense
System.out.print("Alpha:foo");
}
public void bar(String a) {
System.out.print("Alpha:bar");
} }
public class Beta extends Alpha {
public void foo(String a) {
System.out.print("Beta:foo");
}
public void bar(String a) {
System.out.print("Beta:bar");
}
public static void main(String[] arg) {
Alpha a = new Beta();
Beta b = (Beta) a;
a.foo("test");//confusing line
b.foo("test"); …Run Code Online (Sandbox Code Playgroud) java ×6
ruby ×2
byte ×1
codeblocks ×1
compare ×1
constructor ×1
enums ×1
ide ×1
import ×1
inheritance ×1
irb ×1
oop ×1
polymorphism ×1
require ×1
ruby-1.9.2 ×1
static ×1
textmate ×1
wrapper ×1