简体   繁体   English

如何在父类构造函数所需的子类中初始化参数对象?

[英]How to initialize parameter Objects in child class needed for super class constructor?

So, this might sound a bit weird but let me explain. 因此,这听起来可能有些怪异,但让我解释一下。 I have a super class that requires multiple parameters. 我有一个需要多个参数的超类。 One such parameter is a BufferedImage object. 这样的参数之一是BufferedImage对象。 Now obviously, to initialize this BufferedImage in the child class to use as a parameter, I need to use try and catch blocks. 现在显然,要在子类中初始化此BufferedImage用作参数,我需要使用try和catch块。 The only way I can do that is in a method of the child class called in the constructor. 我唯一可以做到的方法是在构造函数中调用子类的方法。 The problem is, the super() constructor must be the first thing in the child class constructor. 问题是, super()构造函数必须是子类构造函数中的第一件事。 So I can't call the method to initialize my BufferedImage before calling super() . 因此,在调用super()之前,我无法调用该方法来初始化BufferedImage How can I initialize my BufferedImage object correctly before using it as a parameter when calling super() in my child class constructor? 在子类构造函数中调用super()时,如何在将其用作参数之前正确初始化BufferedImage对象?

Example: Super/Parent Class 示例:超级/父类

public class CombatEntity {
    BufferedImage sprite; 
    public CombatEntity(String name, BufferedImage sprite) {
        //do something
    }
}

Example: Child Class 示例:儿童班

public class Batman {
     BufferedImage sprite;
     Batman() {
         super("Bruce Wayne", sprite); //sprite hasn't been properly initalized

     }

     void getSprite() { //I need to call this method before super in order to initalize my image
          try {
              File f = new File("Batman.png");
              sprite = ImageIO.read(f);
          }

          catch(Exception e) {
              //whatever
          }

     }

}

Do something like this: 做这样的事情:

At the parent class create normal constructor, which takes name and sprite parameters. 在父类上创建常规构造函数,该构造函数使用name和sprite参数。 Generate getters and setters method following JavaBeans specification. 遵循JavaBeans规范生成getter和setters方法。

CombatEntity: CombatEntity:

   public class CombatEntity {

    protected String name;

    protected BufferedImage sprite; 

    public CombatEntity(String name, BufferedImage sprite) {
        this.name = name;
        this.sprite = sprite;
    }

    /*
     * Getters and Setters 
     */

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BufferedImage getSprite() {
        return sprite;
    }

    public void setSprite(BufferedImage sprite) {
        this.sprite = sprite;
    }   
}

In Batman (child) class create two constructors - one with no parameters, which you can use to create Batman object before you initialize sprite image, and second similiar to parent constructor. 在Batman(子类)类中,创建两个构造函数-一个不带参数的构造函数,您可以在初始化子画面图像之前使用它们创建Batman对象,第二个构造函数类似于父构造函数。 When you invoke constructor with no parameters, it invoke parent constructor and set it's parameters to default. 当您不带参数调用构造函数时,它将调用父构造函数并将其参数设置为默认值。 Then you can execute generateSpriteImage(String spriteImagePath) to create sprite image from injected path. 然后,您可以执行generateSpriteImage(String spriteImagePath)从注入的路径创建精灵图像。

Batman: 蝙蝠侠:

public class Batman extends CombatEntity{

    //Default constructor with no parameters 
    public Batman(){
        super("", null);
    }

    public Batman(String name, BufferedImage sprite){
        super(name, sprite);
    }

    public void generateSpriteImage(String spriteImagePath) {
        try {
            File file = new File(spriteImagePath);
            this.sprite = ImageIO.read(file);
        }

        catch(Exception e) {
            //whatever
        }
   }
}

Hope this will help you. 希望这会帮助你。

The only way to fix this is to demand an Image parameter in your Batman constructor. 解决此问题的唯一方法是在Batman构造函数中要求Image参数。 This issue is not uncommon, and is one of the reasons to comply with the Javabeans pattern, where every class has a null constructor as well as getters and setters. 这个问题并不罕见,并且是遵守Javabeans模式的原因之一,在Javabeans模式中,每个类都具有null构造函数以及getter和setter方法。

Your CombatEntity could define a abstract method getSprite() and call this one in the constructor. 您的CombatEntity可以定义一个abstract方法getSprite()并在构造函数中调用此方法。 The child class (Batman) will have to implement this method. 子类(蝙蝠侠)将必须实现此方法。

The advantage is that you don't need to call an extra method after constructing the object (as suggested in the other answer). 这样做的好处是,在构造对象之后,您无需调用其他方法(如其他答案中所建议)。

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

相关问题 Java:当超级构造函数需要参数时如何初始化子级 - Java: how to initialize child when super constructor requires parameter 如何从子类构造函数调用超类的私有构造函数? - How to call private constructor of super class from child class constructor? Java 如何使用超级 class 构造函数设置子 class 实例变量 - Java how to set child class instance variable with super class constructor 如何使用父 class 构造函数(super())为子类创建多个对象? - How do i create multiple objects for child classes using parent class constructor(super())? 如果我需要在孩子中初始化它,如何为超级 class 提供数组? - How to provide array to a super class if I need initialize it in a child? 如何将子类传递到需要超级类作为参数的方法中 - How to Pass a Child Class into a method requiring Super Class as parameter 如何确保子类将在 lombok 中调用正确的超级构造函数? - How to ensure that child class will call the correct super constructor in lombok? 为什么在子类构造函数中调用“super()”? - Why calling 'super()' in child class constructor? 在super()构造函数中获取子类名称 - Getting child class name in super() constructor Java:如何在子类构造函数中调用超类构造函数? - Java: How does a call to the super class constructor inside of a child class constructor work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM