Java通用组件方法
我正在尝试创建一个类似于UnityEngine的java组件系统,并希望构建一个通用的GetComponent()方法。我在尝试返回找到的类型时卡住了Java通用组件方法,java,unity3d,Java,Unity3d,我正在尝试创建一个类似于UnityEngine的java组件系统,并希望构建一个通用的GetComponent()方法。我在尝试返回找到的类型时卡住了 public <T> T GetComponent (){ for (Component c: this.components){ //I know this doesn't work, I want to know how to check if the type //matches and ret
public <T> T GetComponent (){
for (Component c: this.components){
//I know this doesn't work, I want to know how to check if the type
//matches and return it (ie. Collider col = GetComponent<Collider>();)
if(c == T)){
System.out.println("Component of type: " + c.name + " found!");
return T;
}
}
System.out.println("No component found");
return null;
}
public T GetComponent(){
对于(组件c:此.components){
//我知道这不起作用,我想知道如何检查类型
//匹配并返回它(即Collider col=GetComponent();)
如果(c==T)){
System.out.println(“类型的组件:“+c.name+”found!”);
返回T;
}
}
System.out.println(“未找到任何组件”);
返回null;
}
编辑
感谢所有回答的人。这是一次关于Java和C有多大不同的学习经历。您不能
返回T
。返回采用表达式,而不是类型<代码>返回T类似于返回字符串
,这是无效的
您试图返回一个组件,因此可能:
public <T> T GetComponent (Class<T> compnentType)
for (Component c: this.components){
if(c.getClass() == compnentType)){ //you need to check your logic here
System.out.println("Component of type: " + c.name + " found!");
return (T) c;
}
}
return null; //again, logic to be checked.
}
public T GetComponent(类componenttype)
对于(组件c:此.components){
如果(c.getClass()==componentType)){//您需要在此处检查您的逻辑
System.out.println(“类型的组件:“+c.name+”found!”);
返回(T)c;
}
}
返回null//同样,需要检查逻辑。
}
这将检查组件类。如果不接收组件类,则无法检查类型,因为无法在检查的
实例中使用类型参数T
。无法返回T
。返回采用表达式,而不是类型<代码>返回T
类似于返回字符串
,这是无效的
您试图返回一个组件,因此可能:
public <T> T GetComponent (Class<T> compnentType)
for (Component c: this.components){
if(c.getClass() == compnentType)){ //you need to check your logic here
System.out.println("Component of type: " + c.name + " found!");
return (T) c;
}
}
return null; //again, logic to be checked.
}
public T GetComponent(类componenttype)
对于(组件c:此.components){
如果(c.getClass()==componentType)){//您需要在此处检查您的逻辑
System.out.println(“类型的组件:“+c.name+”found!”);
返回(T)c;
}
}
返回null//同样,需要检查逻辑。
}
这将检查组件类。如果不接收组件类,您就无法检查类型,因为您不能在检查的
实例中使用类型参数T
。当我试图从您的文本和代码中推断出您试图实现的目标时,我看不到使用泛型的必要性
在我看来,你似乎正在努力实现这一目标:
public Component getComponent (){
for (Component c: this.components){
//I know this doesn't work, I want to know how to check if the type
//matches and return it (ie. Collider col = GetComponent<Collider>();)
if(c instanceof Collider) {
System.out.println("Component of type: " + c.name + " found!");
return c;
}
}
System.out.println("No component found");
return null;
}
编辑
下面是@EpicNicks的评论,这是对我的建议的改编:
public <T extends Component> T getComponent (Class<T> componentClass){
for (Component c: this.components){
//I know this doesn't work, I want to know how to check if the type
//matches and return it (ie. Collider col = GetComponent<Collider>();)
if(c.getClass().isAssignableFrom(componentClass)) {
System.out.println("Component of type: " + c.getClass().getSimpleName() + " found!");
return (T) c;
}
}
System.out.println("No component found");
return null;
}
公共T getComponent(类componentClass){
对于(组件c:this.components){
//我知道这不起作用,我想知道如何检查类型
//匹配并返回它(即Collider col=GetComponent();)
if(c.getClass().isAssignableFrom(componentClass)){
System.out.println(“类型的组件:“+c.getClass().getSimpleName()+”found!”);
返回(T)c;
}
}
System.out.println(“未找到任何组件”);
返回null;
}
当我试图从您的文本和代码中推断出您想要实现的目标时,我认为没有必要使用泛型
在我看来,你似乎正在努力实现这一目标:
public Component getComponent (){
for (Component c: this.components){
//I know this doesn't work, I want to know how to check if the type
//matches and return it (ie. Collider col = GetComponent<Collider>();)
if(c instanceof Collider) {
System.out.println("Component of type: " + c.name + " found!");
return c;
}
}
System.out.println("No component found");
return null;
}
编辑
下面是@EpicNicks的评论,这是对我的建议的改编:
public <T extends Component> T getComponent (Class<T> componentClass){
for (Component c: this.components){
//I know this doesn't work, I want to know how to check if the type
//matches and return it (ie. Collider col = GetComponent<Collider>();)
if(c.getClass().isAssignableFrom(componentClass)) {
System.out.println("Component of type: " + c.getClass().getSimpleName() + " found!");
return (T) c;
}
}
System.out.println("No component found");
return null;
}
公共T getComponent(类componentClass){
对于(组件c:this.components){
//我知道这不起作用,我想知道如何检查类型
//匹配并返回它(即Collider col=GetComponent();)
if(c.getClass().isAssignableFrom(componentClass)){
System.out.println(“类型的组件:“+c.getClass().getSimpleName()+”found!”);
返回(T)c;
}
}
System.out.println(“未找到任何组件”);
返回null;
}
我现在无法检查这一点,但也许这可以在不传入参数的情况下找到正确的类型:
public <T> T GetComponent () {
for (Component c: this.components){
try {
return (T) c;
} catch (ClassCastException cce) {}
}
return null;
}
public T GetComponent(){
对于(组件c:此.components){
试一试{
返回(T)c;
}catch(ClassCastException cce){}
}
返回null;
}
}我现在无法检查这一点,但也许这可以在不传入参数的情况下找到正确的类型:
public <T> T GetComponent () {
for (Component c: this.components){
try {
return (T) c;
} catch (ClassCastException cce) {}
}
return null;
}
public T GetComponent(){
对于(组件c:此.components){
试一试{
返回(T)c;
}catch(ClassCastException cce){}
}
返回null;
}
}在Java中,一个好的做法是以小写字符开始方法名,比如
getComponent
。在这里读更多:我知道,我只是更喜欢C#conventions@EpicNicks,在罗马…在Java中,最好用小写字符开始方法名,如getComponent
。在这里读更多:我知道,我只是更喜欢C#conventions@EpicNicks,在罗马…有没有办法让这个方法只传递泛型参数就可以工作?就像Unity的GetComponent()?@EpicNicks一样,您可以使用T
来强制转换返回值,但是您将无法使用任何东西来过滤组件(因此,如果您的变得不可能),您知道Unity可以如何做吗?这只是C#特性的问题吗?@EpicNicks不知道,不熟悉那个框架。@EpicNicks,C#有.NET反射,我不知道Java是否有类似的功能。有没有办法让这个方法只传递泛型参数就可以工作?就像Unity的GetComponent()?@EpicNicks一样,您可以使用T
来强制转换返回值,但是您将无法使用任何东西来过滤组件(因此,如果您的变得不可能),您知道Unity可以如何做吗?这仅仅是C#功能的问题吗?@EpicNicks不知道,不熟悉该框架。@EpicNicks,C#有.NET反射,我不知道Java是否有类似的功能。我想使用泛型的原因是我将使用多个组件类型。游戏对象将最多存储每个组件中的一个