简体   繁体   English

如何在不知道我返回的对象类型的情况下实现一个在Java中返回的方法?

[英]How do I implement a method that returns in Java without knowing what object type I'm returning?

I'm returning to Java after a year spent working with Python, so I'm trying to remember some of the syntax/design techniques involved. 我在使用Python一年后回到Java,所以我试图记住一些涉及的语法/设计技术。 In doing so I'm going back and implementing every project from my previous class, only in Java...yet I'm already stuck on implementing a simple Stack. 在这样做的过程中,我将返回并实现我之前课程中的每个项目,仅限于Java ...但我已经停留在实现一个简单的Stack上了。 This is because I'm not sure how to write method signatures when I don't know what sort of object I'm returning, ie in a pop() method. 这是因为当我不知道我正在返回什么类型的对象时,我不确定如何编写方法签名,即在pop()方法中。 The same problem applies with the insert() method, since I don't know what sort of object's being passed in as an argument. insert()方法也存在同样的问题,因为我不知道作为参数传递了什么类型的对象。 In Python there was no need to explicitly state object types in method signatures so this leaves me confused. 在Python中,没有必要在方法签名中显式声明对象类型,因此这让我感到困惑。

Do I have to write separate methods for each possible type of argument/return value, or is there some way around this problem? 我是否必须为每种可能的参数/返回值类型编写单独的方法,或者是否有某种解决方法?

You can write it like this: 你可以像这样写:

public interface Stack
{
    void push(Object o);
    Object pop();
}

Or you can use generics: 或者您可以使用泛型:

public interface Stack<T>
{
    void push(T o);
    T pop();
}

If your purpose is not to write your own, you can use the one built into Java: 如果你的目的不是自己编写,你可以使用Java内置的:

http://www.docjar.com/docs/api/java/util/Stack.html http://www.docjar.com/docs/api/java/util/Stack.html

public Object pop(); .

Object is the base class that every class inherits from. Object是每个类继承的基类。

That said, while this kind of signature has its merits, in many cases a better approach would be something more type-safe, ie popInt(), popString(), etc - after all, the calling code will need to cast the Object to something, and worse still, it needs to make the decision on what to cast it to based on something that is typically better done using polymorphism. 也就是说,虽然这种签名有其优点,但在许多情况下更好的方法是类型安全的东西,即popInt(),popString()等 - 毕竟,调用代码需要将Object转换为事情,更糟糕的是,它需要根据通常使用多态性做得更好的事情做出决定。

I don't know your setup, so Object pop() may just be your ticket. 我不知道你的设置,所以Object pop()可能只是你的票。

EDIT: Or use generics, like duffymo expertly demonstrated. 编辑:或使用泛型,如duffymo专业演示。

Java supports generics and allows you to parameterize the type you want to store. Java支持泛型,并允许您参数化要存储的类型。 So in your case it would be something like. 所以在你的情况下它会是这样的。 Read more on this here 这里阅读更多内容

public class Stack<T>
{

List<T> items=new ArrayList<T>();

public T push(T item) {...}

public T pop() {...}

}

暂无
暂无

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

相关问题 Java:如何在不知道对象类型的情况下在arraylist上调用方法 - java: how to call a method on arraylist without knowing the type of object 在Java中,如何实现返回Class的方法 <List<String> &gt; - In Java, how do I implement a method that returns Class<List<String>> Java:如何实现一个需要2个数组并返回2个数组的方法? - Java: How do I implement a method that takes 2 arrays and returns 2 arrays? 我如何仅在知道该方法应该做什么的情况下找到一种在Java中使用的方法? - How do I find a certain method to use in java knowing only what the method should do? 如何在不知道字段是什么的情况下创建ElasticSearch查询? - How do I create an ElasticSearch query without knowing what the field is? Java-方法如何在编译时不知道结果类型的情况下返回变量类型的对象? - Java - How can a method return a variable type object without knowing the resulting type at compile time? 如何实现在Java二进制搜索树中返回按顺序排序的数组的方法? - How do I implement a method that returns an inorder sorted array in a java binary search tree? 如何在不知道Java中确切类型的情况下克隆对象? - How to clone an Object without knowing the exact type in Java? 应用方法而不知道Java中的对象类型 - Apply methods without knowing object type in java 知道变量的内容之后,如何确定要放置在&lt;&gt;中的内容以消除“原始类型”警告? - Knowing what the contents of variables are going to be, how do I determine what to place inside the <> to eliminate “raw type” warnings?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM