我已经阅读了这篇文章http://www.javapractices.com/topic/TopicAction.do?Id=31,它说使用instanceof预先检查我的对象是不好的做法.
但是在这种情况下我不知道怎么做:我有一个返回项目列表的API.此项列表返回用户和管理员
如果我投射一个像(用户)项目的项目,一旦它到达管理员,我将有一个ClassCastException
for(Item item : items){
if (item instanceof User){
((User)item).getName());
((User)item).getEmail());
}
else if (item instanceof Admin){
((Admin)item).getName());
((Admin)item).getEmailList().getPrimary());
}
}
Run Code Online (Sandbox Code Playgroud)
由于它是一个API,我不能修改Item,Item没有关于childs的方法,而且email检索方法不同.我还有其他选择吗?
如果我想创建一个以结构名称作为参数的函数,那么方法签名是什么样的?
typedef struct Class{
} Class;
main()
{
Class *a = malloc(Class);
return instanceOf(a, Class);
}
Run Code Online (Sandbox Code Playgroud)
宣言instanceOf会是什么样子?
我的班级类型有问题.我有一个超级"可食用"和一个界面"多彩".并非所有可食用的物品都是五彩缤纷的,所以只有在某些可食用物品上才能实现.我正在尝试使用一个可食物项的ArrayList,循环它,并返回一个只包含彩色项目的新ArrayList.我现在收到的错误是
"ArrayList类型中的方法add(Colorful)不适用于参数(Edible)"
我怎样才能解决这个限制?
private ArrayList<Edible> elist;
private ArrayList<Colorful> clist;
public List<Colorful> getColorfulItems(){
for(Edible x : elist)
if(x instanceof Colorful){
clist.add(x);
}
return clist;
}
Run Code Online (Sandbox Code Playgroud) 我有这种方法,我希望能够确定两个单元格是否相等,其中"相等"意味着它们具有相同的位置.我已经编写了这个代码,我使用它们instanceof并进行强制转换以确保我的对象属于该类型Position,然后将其转换为类型Position,但由于某些原因它似乎不起作用.
这是我的代码:
public class Postion {
private int column;
private int row;
public Position (final int column, final int row) {
this.column = column;
this.row = row;
}
public int getColumn() {
return column;
}
public int getRow() {
return row;
}
public boolean equals(final Object other) {
if (other instanceof Position) {
Position position = (Position) other;
return ((column == other.getColumn()) && (row == other.getRow()));
} else {
return false;
}
}
} …Run Code Online (Sandbox Code Playgroud) 我今天开始学习仿制药,但这对我来说有些奇怪:
我有一个通用的方法:
public<T> HashMap<String, T> getAllEntitySameType(T type) {
System.out.println(type.getClass());
HashMap<String, T> result = null;
if(type instanceof Project)
{
System.out.println(type.toString());
System.out.println("Yes, instance of Project;");
}
if(type instanceof String)
{
System.out.println(type.toString());
System.out.println("Yes, instance of String;");
}
this.getProjects();
return result;
}
Run Code Online (Sandbox Code Playgroud)
我可以轻松确定T类的类
Project<Double> project = new Project<Double>();
company2.getAllEntitySameType(project);
company2.getAllEntitySameType("TestString");
Run Code Online (Sandbox Code Playgroud)
输出将是:
class Project
Yes, instance of Project;
class java.lang.String
TestString
Yes, instance of String;
Run Code Online (Sandbox Code Playgroud)
我认为在泛型中我们不能使用实例.据我所知,有些东西并不完整.谢谢...
我试图理解为什么我得到这些结果(这不是我预期的原因)"
public interface ICommandAble
{ }
public interface ILogAble extends ICommandAble
{ }
public interface IMonitorAble extends ICommandAble
{ }
public abstract class ClassAbs
{ }
public class A extends ClassAbs implements IMonitorAble, ILogAble
{ }
Run Code Online (Sandbox Code Playgroud)
测试方法:
public void test()
{
A a=new A();
List<ICommandAble>commandList=new ArrayList<ICommandAble>()
if (a instanceof ILogAble)
{
ILogAble logAbleItem=(ILogAble)a;
commandList.add(logAbleItem);
}
if (a instanceof IMonitorAble) {
IMonitorAble monitorAbleItem=(IMonitorAble)a;
commandList.add(monitorAbleItem);
}
for(ICommandAble item: commandList)
{
if(item instanceof IMonitorAble)
{
log.debug("is monitorable");
}
if(item instanceof ILogAble)
{
log.debug("is …Run Code Online (Sandbox Code Playgroud) 继续我之前的问题,我正在尝试编写一个类似这样的方法
public <T extends LivingThing> T getData(Class<T> clazz, Object otherParam) {
if(clazz instanceof Cat) {
//do something and return a new Cat
}
}
Run Code Online (Sandbox Code Playgroud)
我在if条件上遇到编译器错误"不兼容的条件操作数类型".我究竟做错了什么?如何在我的方法中检查类类型?
UPDATE
好的,我做了一个代码更改以使用isAssignableFrom方法.这是一个新问题.
public <T extends LivingThing> List<T> getData(Class<T> classType) {
LivingThingEnum livingThing = LivingThingEnum
.getLivingThing(classType);
if (livingThings.keySet().contains(livingThing))
return livingThings.get(livingThing);
return null;
}
private Map<LivingThingEnum,List<? extends LivingThing>> livingThings;
Run Code Online (Sandbox Code Playgroud)
这给了我一个类型不匹配! - "无法从列表转换为列表".是不是应该扩展LivingThing,那么在哪种情况下为什么编译器会抛出错误?
我很困惑一个对象是一个instanceof两个不同的类.例如:
EnglishTest x = new EnglishQuiz();
if (x instanceof EnglishTest){
System.out.print("P");
}
if (x instanceof EnglishQuiz){
System.out.print("P");
}
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,我都打印出"P".我的问题是为什么?
我知道这x是类型,EnglishTest但因为我在做new EnglishQuiz,不应该意味着我正在创建一个实例,EnglishQuiz而不是EnglishTest那样?这里发生了什么?
请注意:来电者只会抛出parentexception !!
说aexception,并bexception继承自parentexception.
在方法af,它throws aexception,bexception和parentexception.
void af() throws aexception, bexception, parentexception {}
Run Code Online (Sandbox Code Playgroud)
该方法caller调用af和throw parentexception只.
void caller() throws parentexception
Run Code Online (Sandbox Code Playgroud)
在这里,我们丢失了parentexception子类的信息.
该方法rootCaller调用该方法caller,rootcaller并且只能catch parentexception抛出caller并使用以下异常进程catch块:
void rootCaller() {
try {
caller();
} catch(parentexception e) {
if(e instanceof aexception) {
......
} else if(e instanceof bexception) {
......
} else if(e instanceof parentexception) {
...... …Run Code Online (Sandbox Code Playgroud) 我试过了:
package ro.ex;
import java.io.IOException;
import java.lang.reflect.Type;
class Ex {
public boolean isIns(Object o, Class t) {
o instanceof t;
}
public static void main(String[] args) throws IOException {}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码将引发未知类"t"
我的问题是:如何传递上述代码.
更新:
以下代码无法通过intellij构思语法检查器
public boolean isIns(Object o, Class<?> t) {
return o instanceof t;
}
Run Code Online (Sandbox Code Playgroud)
所以正确的代码是:
public boolean isIns(Object o, Class<?> t) {
return t.isAssignableFrom(o.getClass());
}
Run Code Online (Sandbox Code Playgroud)
更简单的方法是:
package ro.ex;
import java.io.IOException;
import java.lang.reflect.Type;
class Ex {
public boolean isIns(Object o, Class t) {
return t.isInstance(o);
}
public static …Run Code Online (Sandbox Code Playgroud) instanceof ×10
java ×9
oop ×3
polymorphism ×2
arraylist ×1
c ×1
casting ×1
class ×1
exception ×1
generics ×1
inherited ×1
interface ×1
struct ×1
superclass ×1
types ×1