简体   繁体   English

可以采用不同类型的变量?

[英]A variable that could take different types in?

TLDR: I need a variable that can take in an object from either of 2 classes. TLDR:我需要一个可以从两个类中的任何一个接受对象的变量。

I'm working on a Java 2D game, still getting the basics to work and here I have a problem: in the class Actor, in the constructor, the actor generates its hitbox (4 XY coordinates object) and then adds that hitbox to a list of things that need to check for collisions. 我正在开发Java 2D游戏,仍然可以使用其基础知识,这是一个问题:在Actor类中,在构造函数中,actor生成其Hitbox(4个XY坐标对象),然后将该Hitbox添加到需要检查碰撞的事物列表。

But now that I got all this working, I made a new class, Platform, so that my character can walk on something else than the defiled corpses of it's enemies. 但是现在我已经完成了所有这些工作,我创建了一个新的平台Platform,以便我的角色可以走在敌人被污秽的尸体之外的其他物体上。 But in the Rect constructor, I have a variable ( Parent ) that sets itself to the object that called the constructor (with itself in parameter) so I would get hitbox.parent() = player for example. 但是在Rect构造函数中,我有一个变量( Parent )将自身设置为调用该构造函数的对象(其自身位于参数中),因此我将获得hitbox.parent()= player。

But since the Platform objects are from another class (that I don't really want to inherit from the Actor class) how can I make it so that Rect and give itself a parent of different type ? 但是由于平台对象来自另一个类(我真的不想从Actor类继承),我如何才能使它成为Rect并为其自身提供不同类型的父对象?

Edit 编辑

The class as it is now 现在的班级

package misc;

import item.Platform;
import npc.Actor;

public class Rect {
int x,y,wdt,hgt;

public Rect(Actor a){
    x = a.x;
    y = a.y;
    wdt = a.wdt;
    hgt = a.hgt;        
}
public Rect(Platform p){
    parent = p;
    x =p.x;
    y =p.y;
    wdt =p.wdt;
    hgt =p.hgt;
}
}

And here is the place where I have trouble calling it 这是我遇到麻烦的地方

private static void collision(Rect r1,Rect r2){

    if (r1.y -r2.y <= r2.hgt && r1.y -r2.y >= -r2.hgt){
        r1.parent.yCol = true;  
    }else{
        r1.parent.yCol = false;
    }

    if (r1.x -r2.x <= r2.wdt && r1.x -r2.x >= -r2.wdt){
        r1.parent.xCol = true;  
    }else{
        r1.parent.xCol = false;
    }
}

You need to use Inheritance, which is one of the most important aspects of Object Oriented Programming. 您需要使用继承,这是面向对象编程的最重要方面之一。 You need to do some reading on it so you understand how it works and how to use it: https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html 您需要对其进行一些阅读,以了解它的工作方式以及使用方法: https : //docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

In addition to inheritance, you could also use an interface based approach. 除了继承,您还可以使用基于接口的方法。

public interface GameRect {
  int getX();
  int getY();
  int getHeight();
  int getWidth();
}

public class Actor implements GameRect {
  // implementation
}

public class Platform implements GameRect {
  // implementation
}

public class Rect {
    // implementation
    private GameRect parent;

    // constructor works for all classes that implement GameRect interface
    public Rect(GameRect gr) {
      parent = gr;
      x = gr.getX();
      y = gr.getY();
      // etc
    }
}

The problem with a solution like this is that you need to cast back to the original type (Actor, and Platform respectively) every time you want to call class methods on the parent objects that are not GameRect interface methods. 此类解决方案的问题在于,每次要在不是GameRect接口方法的parent对象上调用类方法时,都需要回退到原始类型(分别是Actor和Platform)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM