我该怎么说呢:
while(input is not an int){
do this
}
Run Code Online (Sandbox Code Playgroud)
我试过这段代码,但我知道这是错的:
int identificationnumber;
Scanner sc3 = new Scanner(System.in);
identificationnumber = sc3.nextInt();
while( identificationnumber != int){ // this line is wrong
Scanner sc4 = new Scanner(System.in);
identificationnumber = sc4.nextInt();
}
Run Code Online (Sandbox Code Playgroud)
请任何建议.谢谢.
我有一个方法,其返回类型是对象。我如何为此创建一个测试用例?我如何提及结果应该是一个对象?
例如:
public Expression getFilter(String expo)
{
// do something
return object;
}
Run Code Online (Sandbox Code Playgroud) 试图回答这张票:instanceof和Class.isAssignableFrom(...)有什么区别?
我做了一个性能测试:
class A{}
class B extends A{}
A b = new B();
void execute(){
boolean test = A.class.isAssignableFrom(b.getClass());
// boolean test = A.class.isInstance(b);
// boolean test = b instanceof A;
}
@Test
public void testPerf() {
// Warmup the code
for (int i = 0; i < 100; ++i)
execute();
// Time it
int count = 100000;
final long start = System.nanoTime();
for(int i=0; i<count; i++){
execute();
}
final long elapsed = System.nanoTime() - start;
System.out.println(count+" iterations took …Run Code Online (Sandbox Code Playgroud) 现在我正在编写一个ORM框架,非常关心性能.
在本框架中,我必须使用instanceof和Class.isAssignableFrom检查类型兼容性.
所以我对instanceof和的表现有点怀疑Class.isAssignableFrom
究竟有多缓慢?
我不知道如何对 newEntry 执行类型检查,我想确保它是 MyTable 类型(而不创建 MyTable 对象)。
public static boolean add(String table, Object newEntry)
{
boolean result;
if (table.equals("MyTable") && newEntry.getClass() == MyTable.getClass())
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:
newEntry.getClass() == MyTable.getClass().
Run Code Online (Sandbox Code Playgroud)
注意:MyTable 是类名,而不是对象。
当尝试比较作为参数传递的两个对象时:首先作为对象,第二个作为对象数组(相同类型).得到错误Incompatible conditional operand types Room and allRooms[].我试图循环迭代,while直到thisRoomType不等于某个对象rooms.怎么解决?
public abstract class Room { ...}
public class LightRoom extends Room {...}
public class DarkRoom extends Room {...}
Run Code Online (Sandbox Code Playgroud)
呼叫者:
release(thisRoomType, rooms)
Run Code Online (Sandbox Code Playgroud)
带参数
private Room thisRoomType = this; // is defined in DarkRoom and LightRoom
private Room[] rooms; // is defined in DarkRoom and LightRoom
Run Code Online (Sandbox Code Playgroud)
在方法中:
public synchronized void release( Room thisRoom, Room[] allRooms) {
try {
int j = 0;
while(thisRoom instanceof allRooms[j]){
jj++;
} …Run Code Online (Sandbox Code Playgroud) 不使用instanceof 相当于什么?使用 true、false 或 else 语句时也许更简单?
public static void p (Object...ar)
{
for (int i=0; i < ar.length; i++)
{
if (ar[i] instanceof int[])
{
Run Code Online (Sandbox Code Playgroud) 想象一下情况:
@javax.persistence.Inheritance(strategy=javax.persistence.InheritanceType.JOINED)
@javax.persistence.DiscriminatorColumn
@javax.persistence.Entity
@javax.persistence.Table(name="PARENT")
public abstract class Parent{
...
}
@javax.persistence.Entity
@javax.persistence.Table(name="A")
public class A extends Parent{
...
}
@javax.persistence.Entity
@javax.persistence.Table(name="B")
public class B extends Parent{
...
}
Parent p = new A();
Run Code Online (Sandbox Code Playgroud)
现在我们称之为:
p instance of A
Run Code Online (Sandbox Code Playgroud)
总是返回false !!
在OpenJPA上运行正常!
我应该提交错误吗?Hibernate 4.3.10
在 C# 中,如果我有一个类型为 的变量object,如何检查是否可以将其转换为T值类型?该as运算符不起作用,因为 T 是值类型,并且o.GetType().Equals(typeof(T))仅检查 object 是否是类型 T 的装箱变量,但不检查它是否可以转换为 T。
java ×8
instanceof ×2
performance ×2
c# ×1
hibernate ×1
if-statement ×1
int ×1
jpa ×1
junit ×1
loops ×1
object ×1
oop ×1
testcase ×1
types ×1
while-loop ×1