假设InvalidResourceException是ResourceException的子类.定义两种方法:
void handleException(ResourceException e) {
System.out.println("ResourceException " + e.toString());
}
void handleException(InvalidResourceException e) {
System.out.println("InvalidResourceException " + e.toString());
}
Run Code Online (Sandbox Code Playgroud)
现在以下代码:
try {
throw new InvalidResourceException("invalid resource");
} catch (ResourceException e) {
handleException(e);
}
Run Code Online (Sandbox Code Playgroud)
打印这个:
ResourceException: com.myPackage.InvalidResourceException: invalid resource
Run Code Online (Sandbox Code Playgroud)
但是以下代码:
try {
throw new InvalidResourceException("invalid resource");
} catch (InvalidResourceException e) {
handleException(e);
} catch (ResourceException e) {
handleException(e);
}
Run Code Online (Sandbox Code Playgroud)
打印这个:
InvalidResourceException: com.myPackage.InvalidResourceException: invalid resource
Run Code Online (Sandbox Code Playgroud)
这是Sun的JDK 1.5.0_15.
这与Java标准一致吗?
这段代码应该怎么做?
Exception e = new InvalidResourceException("invalid resource");
handleException(e);
Run Code Online (Sandbox Code Playgroud)
这段代码应该怎么做?
Exception e = new InvalidResourceException("invalid …Run Code Online (Sandbox Code Playgroud) 由于在其他线程中完成了基准测试(参见/sf/answers/27833221/),因此可以看出Java 6中的instanceof实际上非常快.这是如何实现的?
我知道对于单继承,最快的想法是使用一些嵌套间隔编码,其中每个类维持[低,高]间隔,而instanceof只是间隔包含测试,即2个整数比较.但它是如何制作接口的(因为区间包含仅适用于单继承)?如何处理类加载?加载新的子类意味着必须调整很多间隔.
假设我有以下代码:
public boolean doesElfLikeIt ( Monster mon )
{
if ( mon instanceof Orc ) { return false; }
if ( mon instanceof Elf ) { return true; }
}
Run Code Online (Sandbox Code Playgroud)
这是一个很好的编程方法还是我应该选择这样的东西:
public boolean doesElfLikeIt ( Monster mon )
{
if ( mon.getType() == Orc.type ) { return false; }
if ( mon.getType() == Elf.type ) { return true; }
}
Run Code Online (Sandbox Code Playgroud)
我问这个的原因是因为我听到很多关于instanceof比较有多邪恶,但我发现它很有用.
我有一个代码块,我反序列化传入的数据,然后我必须将其转换为一些已知的类对象,所以为此我做了这样的事情:
if (object instanceof MyClass) {
Myclass data = (MyClass)object;
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但现在我有一种情况,可能有不同类型的calsses.那么有没有办法根据以下内容进行比较"String":
if (object instanceof "String") {
String data = (String)object;
}
Run Code Online (Sandbox Code Playgroud)
问题是在这种情况下,用户将指定类对象名称,那么我该怎么做呢?
我应该强制用户启动一个虚拟对象,然后传递给我的方法或有没有办法用String初始化null对象,任何想法?
我正在尝试使用这里找到的Laravel缓存包:https://github.com/cviebrock/eloquent-sluggable
当我保存模型(例如产品)时,可缓存的功能不会使我的模型变得迟钝.这是今天全新安装的Laravel.
我可以看到事件监听器触发(SluggableServiceProvider.php中的第43行),但似乎if($ model instanceof SluggableInterface)语句永远不会返回true并且永远不会使我的模型变得迟钝.这可能是名称间距问题吗?还有其他想法吗?
这是我的产品型号:
<?php
use Cviebrock\EloquentSluggable\SluggableInterface;
use Cviebrock\EloquentSluggable\SluggableTrait;
class Product extends Eloquent {
use SluggableTrait;
protected $sluggable = array(
'build_from' => 'title',
'save_to' => 'slug',
);
}
Run Code Online (Sandbox Code Playgroud)
当然,我可以通过执行以下操作来手动运行sluggify方法:
$product->sluggify();
Run Code Online (Sandbox Code Playgroud)
但我想坚持最佳实践,并尝试在触发模型保存事件时使缓慢的代码自动工作.
我有一个抽象类AssociativeFunction,它扩展了Expr.有不同的函数是AssociativeFunction的子类,还有其他表达式是Expr的子类,但不是AssociativeFunction.
在AssociativeFunction类中的方法中,我需要检查Expr对象是否是当前类或其子类之一的实例,
即Product(扩展AssociativeFunction)并继承此方法; 因此,对于所有Product对象和所有CrossProduct(extends Product)对象,检查应返回true,但对于所有Sum(扩展AssociativeFunction)和Number(扩展Expr)对象,则返回false.
为什么以下不起作用?如何使用instanceof运算符使其工作?如果我不能,为什么不呢?
if (newArgs.get(i) instanceof getClass()) {
Run Code Online (Sandbox Code Playgroud)
Eclipse产生错误:
令牌"instanceof",== expected)上的语法错误
这段代码似乎有用,这是正确的吗?为什么它与(1)不同?
if (getClass().isInstance(newArgs.get(i))) {
Run Code Online (Sandbox Code Playgroud)根据我的论文,我在LogCat中发出警告,我认为这可能会影响我的代码.
我收到一个包含一些数据的JSON,我必须以编程方式使用checkbox和radiobutton(在RadioGroup中)进行布局.后来我可以投票这个调查,这是我的问题因为我在我的布局中getChildAt()
if(child instanceof CheckBox)
Run Code Online (Sandbox Code Playgroud)
不起作用,我不明白为什么.
这是我的代码:
ll = (LinearLayout) findViewById(R.id.llSurvey); //OnCreate
/*Creating CheckBox*/
String votes = jObj.getString("votes");
String label = jObj.getString("label");
String usid = jObj.getString("USID");
if(uType.equals("multi")){
CheckBox cb = new CheckBox(SingleSurvey.this);
cb.setText(label + " (" + votes + ")");
cb.setId(s+1000);
ll.addView(cb);
Log.i("MULTI_TYPE", "usid: " + usid + " id: " + cb.getId());
}
s++;
/*Voting*/
for(int i = 0; i < ll.getChildCount(); i++){
View child = ll.getChildAt(i);
int p = child.getId();
Log.i("CHILD ID", "id: " + p);
if(child instanceof …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种方法将参数c传递给instanceof,因为我需要循环遍历代码中显示的数组,但编译器返回错误"unknown class c"如何解决这个问题?
Class0 a = new Class0();
boolean bool;
Class[] array = new Class[]{Class0.class, Class1.class};
for(Class c : array){
if(a instanceof c)
bool = true;
else
bool = false;
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用a.getClass().getSimpleName()并检查它是否等于字符串数组中的元素,但我想更好地理解如何使用关键字instanceof及其工作原理.
我们都同意使用instanceOf通常不是最好的解决方案.网上有很多例子.
但是让我们考虑一下以下示例,我们需要从一个片段调用一个方法到它的活动:
public class BaseActvity extends FragmentActivity implements ISomething {
@Override
public void doSomething();
}
Run Code Online (Sandbox Code Playgroud)
然后假设我们在应用程序中有一个需要调用的片段doSomething():
public MyFragment extends Fragment() {
public void onCreate() {
public void onResume() {
Activity activity = getActivity();
if (activity != null && activity instanceOf ISomething) {
ISomething something = (ISomething) activity;
something.doSomething();
}
}
}
public interface ISomething {
void doSomething();
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我们无法保证getActivity()将返回ISomething的对象,因此我们检查类型.一个改进是添加一个接口,但我们仍然要检查getActvity()返回类型以保护我们的代码.
由于Android Framework和getActivity()调用的性质,我找不到更好的解决方案.也许有人可以帮助一些输入..
注意:我添加了一个界面来跟随访问者模式.请注意,我仍然必须使用instanceOf来确保父活动正在实现它.
谢谢 .
Gaspar de Elais
我糊涂了.
假设我们有以下课程:
class Shape { /* ... */ }
class Square extends Shape { /* ... */ }
Run Code Online (Sandbox Code Playgroud)
什么是由此产生的布尔值,为什么会这样呢?
Shape shape = ...;
boolean b1 = shape instanceof Square;
Square square = ...;
boolean b2 = ((Shape) square) instanceof Square;
boolean b3 = shape instanceof Object;
Run Code Online (Sandbox Code Playgroud)
据我所知,子类是父类的实例,但不是相反?