简体   繁体   English

Java:如何创建从继承的类中获取对象的getter方法?

[英]Java: How do I create a getter method that gets an object from an inherited class?

I'm trying to form a getter method of an object Mammal within a class Catalog. 我正在尝试在类Catalog中形成对象哺乳动物的吸气剂方法。 The get method has no parameters, and has a return type Mammal (?) get方法没有参数,返回类型为Mammal(?)

public class Mammal extends Animal {
    private double intellect;
    private double hitpoints;

    public Mammal(String firstName, String lastName, String nickname) {
        super(firstName, lastName, nickname);
    }

    public void setIntellect(double intellect) {
        this.intellect = intellect;
    }

    public void setHitpoints(double hitpoints) {
        this.hitpoints = hitpoints;
    }
    public double getIntellect() {
        return intellect;
    }
    public double getHitpoints() {
        return hitpoints;
    }

I know it's a pretty simple question and you didn't have to see that, just giving context. 我知道这是一个非常简单的问题,您不必查看该问题,只需提供上下文即可。 I coded several classes like Mammal, such as Feline, Birds, etc. and am the last part of the assignment, Catalog, a series of get methods. 我编码了一些类似哺乳动物的类,例如猫科动物,鸟类等,并且是分配的最后一部分,目录,一系列get方法。 I just need to getMammal, getBirds, getFeline, and the return type is either Mammal, or Birds[], etc. So the object itself or an array of objects. 我只需要getMammal,getBirds,getFeline,返回类型是Mammal或Birds []等。因此,对象本身或对象数组。 My attempt: 我的尝试:

public class Catalogue{
    private Mammal mam;

    public getMammal() {
       Mammal mam = new Mammal();
       return mam;
    }
}

so I'm clearly not understanding what to do at this step. 因此我显然不了解此步骤。 Any help would be appreciated! 任何帮助,将不胜感激! The compile error I get is "invalid method declaration; return type required". 我得到的编译错误是“无效的方法声明;需要返回类型”。 I'm trying it out in a few different ways, but can't seem to get past this error. 我正在尝试几种不同的方法,但似乎无法克服此错误。

I think what you need looks something like this: 我认为您需要的是这样的:

public class Catalogue 
{
    private Mammal mammal;
    private Bird[] birds;
    public Catalogue(Mammal mammal, Bird[] birds)
    {
       this.mammal = mammal;
       this.birds = birds;
    }
    public Mammal getMammal()
    {
     return mammal;
    }
    public Bird[] getBirds()
    {
      return birds;
    }
}

This creates a Catalogue with instance variables mammals and birds , and allows you to get those values. 这将创建一个包含实例变量mammalsbirdsCatalogue ,并允许您获取这些值。 And then to construct a Catalogue , you call: 然后构造一个Catalogue ,您调用:

Catalogue c = new Catalogue(mammal, birds);

Where mammal is an instance of Mammal and birds is an instance of Bird[] . 其中mammal是一个实例Mammalbirds是实例Bird[]

Replace 更换

public getMammal() {
   Mammal mam = new Mammal();
   return mam;
}

by 通过

public Mammal getMammal() {
   Mammal mam = new Mammal();
   return mam;
}

You need to be explicit and define the return type of getter methods, as well as any non-constructor methods. 您需要明确并定义getter方法以及任何非构造方法的返回类型。

You might think it is obvious since you are clearly returning a Mammal , but Java is strongly and explicitly typed. 你可能会认为这是显而易见的,因为你都清楚地返回一个Mammal ,但Java的强烈和显式类型。

There are programming languages which can infer the return type of a method simply by examining the code, but Java is not one of them. 有一些编程语言可以简单地通过检查代码来推断方法的返回类型,但是Java并不是其中一种。

Scala, for instance, is strongly typed but can usually infer what the return type of methods is. 例如,Scala是强类型的,但通常可以推断出方法的返回类型是什么。

public Mammal getMammal() {
    Mammal mam = new Mammal();
    return mam;
}

you have to write the type of object, that you will return. 您必须编写返回的对象类型。 and to write in brackets "new Mammal(firstName,lastName,nickname)" 并在方括号中写下“新哺乳动物(名字,姓氏,昵称)”

If i understand you correctly this Catalog class should be something to create instances of All the Animal type that you created.. one of the solution is simple and will be like: 如果我正确理解了此Catalog类,则应该使用它来创建您创建的All Animal类型的实例。解决方案之一很简单,如下所示:

public class Catalogue{

    public Mammal getMammal() {
       return new Mammal();
    }
}

You don't need Catalog to hold on instance.. Another generic solution will be: 您不需要Catalog来保留实例。另一种通用解决方案是:

public class Catalogue{

    public static Animal getAnimalByType(Class type) {
       return type.newInstance();
    }
}

And when you call this you will know the type.. So this will be generic. 当您调用此方法时,您将知道类型。所以这将是通用的。

Mammal m = (Mammal) Catalogue.getAnimalByType(Mammal.class);

The casting is required because in you generic type you will return Animal, so this will be generic and work for all animal types.. And when you call the method you know what kind of animal you want.. Because you passing this as a parameter in constructor 强制转换是必需的,因为在您的泛型类型中,您将返回Animal,因此这将是泛型的,并且适用于所有动物类型。并且当您调用该方法时,您知道想要哪种动物。.因为您将其作为参数传递在构造函数中

Hope that helps.. Of course this can be more sophisticated but this will do the work for now . 希望能有所帮助。当然,这可能会更加复杂,但是现在可以完成工作。

EDIT: 编辑:

In order to create new instance of Mammal you also need to add construtor: 为了创建哺乳动物的新实例,您还需要添加构造器:

public class Mammal extends Animal {
    private double intellect;
    private double hitpoints;

    public Mammal() {
        super();
    }

    public Mammal(String firstName, String lastName, String nickname) {
        super(firstName, lastName, nickname);
    }

    public void setIntellect(double intellect) {
        this.intellect = intellect;
    }

    public void setHitpoints(double hitpoints) {
        this.hitpoints = hitpoints;
    }
    public double getIntellect() {
        return intellect;
    }
    public double getHitpoints() {
        return hitpoints;
    }

暂无
暂无

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

相关问题 如何覆盖从 java class 在 kotlin 伴侣 ZA8CFDE6331BD59EB6AC96F8911C4 - How to override inherited getter from java class in a kotlin companion object 如何从scala方法访问Java方法的“获取器”? - How do I access a “getter” of a Java method from a scala method? 如何使用EasyMock模拟从抽象类继承的方法? - How do I mock a method inherited from an abstract class with EasyMock? 如何从Java的继承方法中调用特定的重写方法? - How do I call a specific overridden method from an inherited method in Java? 从 class object 使用通过 singleSnapshot 获取值的 getter function 时出现问题。 Android,Java - Problem in using getter function from a class object that gets value through singleSnapshot . Android,Java 如果我创建父类的数组,如何通过数组对象从子类访问方法? - If I create an array of the parental class, how do I access a method from the sub class through the array object? 在Java中,如何在继承的类中覆盖变量的类类型? - In Java, how do I override the class type of a variable in an inherited class? Java:如何阻止人们清除我的arraylist <string> 什么时候使用吸气剂方法? - java: how do I stop people from clearing my arraylist<string> When I use a getter method? 如何从Class对象运行抽象方法来扩展Java中的抽象类? - How do I run an abstract method from Class object extending abstract class in Java? 如何从主 class 之外的方法创建 object 并从构造函数打印结果? - How do I create the object from a method outside of the main class and print the result from the constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM