【问题标题】:How do I return a unsure type?如何返回不确定的类型?
【发布时间】:2017-06-08 01:13:04
【问题描述】:

我有一个名为“Shape”的父类并在其中编写了一个方法 我希望任何从它扩展的类都可以调用更大的方法以供其他用途。 像这样的简单逻辑:

public abstract Class Shape{
    public int size;
    public Shape() {}
    public Class<? extends Shape> bigger() {
        this.size += 1;
        return this; // ← Here,How can I return it with unsure type?
    }
}

但是我怎样才能在这里返回一个不确定的类型呢? 感谢您的任何想法!

====

如果我有一个类 Square extends Shape;

我想这样使用它:

Square S = new Square().bigger();

它会返回一个 Shape 类,而不是 Square 类。

但我不想使用:(Square) new Square.bigger();

我希望它可以使用这个方法自动识别什么类a

并返回正确的类型。

【问题讨论】:

  • 你可以只返回形状。所有延伸形状的事物也是形状。这是一个返回的参考,因此不会丢失任何信息。我相信这是相关的:stackoverflow.com/questions/41525783/…
  • 如果你打算编译你的代码,你必须完全正确地拼写像class这样的关键字。您不能声明一个方法来返回一种类型,如Class,并返回另一种类型。查看官方 Java 教程了解如何完成这些操作。
  • 谢谢,我想我还有很多东西要学XD

标签: java polymorphism


【解决方案1】:

您可以覆盖返回Square(而不是Shape)的bigger() 方法。 很豪华。

public abstract class Shape {
    public int size;
    public Shape() {}
    public Shape bigger() {
        this.size += 1;
        return this; // ← Here,How can I return it with unsure type?
    }
}

public class Square extends Shape {
    @Override
    public Square bigger() { // returns Square, not Shape
        return this;
    }
}

【讨论】:

    【解决方案2】:

    您不会在这里返回Class,而只是返回Shape。类似的,

    public abstract class Shape { // <- class
        public int size;
    
        public Shape() {
        }
    
        public Shape bigger() { // <-- a Shape. By definition of a sub-class because
            this.size += 1; // <-- "this" class is abstract
            return this;
        }
    }
    

    【讨论】:

    • 谢谢,这让我对我的问题有了另一个想法。但是,我真的很想知道有什么方法可以让我返回不确定的类型吗?
    • public abstract class Shape&lt;T&gt; { 并从 bigger() 返回一个 Shape&lt;T&gt;。我想真正的问题是,“不确定的类型”是什么意思? Object?
    【解决方案3】:

    我不确定什么是“不确定”类型,但在 java 中我们有 Generic 类型,即 Java 可以返回任何类,无论它们是什么。

    例如

    public interface dbDao<T>{
    
        public T t; //this is generic class, all class can use this method  
    
    } 
    

    我希望你明白我想说什么。

    【讨论】:

    • 谢谢,这正是我需要的!
    • 很高兴为您提供帮助,先生。顺便说一句,你能投票给答案吗?谢谢。
    【解决方案4】:

    在 Java 中,当您覆盖一个方法时,您实际上可以比接口要求的更具体。例如,接口需要更大的返回一个 Shape,但从 Shape 扩展的 Square 类可以返回一个 Square,因为 Square 是一个 Shape。这意味着如果将其分配给 Square 变量,则在调用更大时无需强制转换。

    public abstract class Shape { // class not Class
        public int size;
    
        public Shape bigger() {
            this.size += 1;
            return this;
        }
    }
    
    public class Square extends Shape {
        @Override
        public Square bigger() {
            this.size += 1;
            return this;
        }
    }
    

    这是一种方法,在这种情况下有点令人沮丧,因为它重复了代码。另一种在 C# 中也可以使用的方法是使用具有泛型类型自身实现的限制的泛型。这被称为奇怪重复的模板模式https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern

    public abstract class Shape <S extends Shape>{
        public int size;
    
        public S bigger() {
            this.size += 1;
            return this;
        }
    }
    
    public class Square extends Shape<Square>{
    }
    

    【讨论】:

    • 一个什么都不做的公共构造函数不需要声明
    猜你喜欢
    • 1970-01-01
    • 2016-10-18
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    • 2019-12-22
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    相关资源
    最近更新 更多