首页 \ 问答 \ 泛型的覆盖方法(Overriding method with Generics)

泛型的覆盖方法(Overriding method with Generics)

我有一个名为GenericOrder的基类,可以用来创建任何类型的产品的订单,然后我有更具体的订单的子类。 我的问题是与我的ComputerOrder类和我重写的方法。 这是基类代码。

import java.util.List;
import java.util.ArrayList;

public class GenericOrder<T> {

private long orderNumber;
private List<T> products;
private T theClass;

public GenericOrder() 
{
    products = new ArrayList<T>();
    orderNumber = System.currentTimeMillis();
}

public long getOrderNumber() {
    return orderNumber;
}

public void addProduct(T newProduct) {
    products.add(newProduct);

}

public int getNumberOfProducts() {
    return products.size();
}

public List<T> getProducts()
{
    return products;
}

public void setProducts(List<T> products)
{
    this.products = products;
}

public T get()
{
    return theClass;
}

public void set(T theClass)
{
    this.theClass = theClass;
}
}

这是我的子类代码。 getProducts是我遇到麻烦的方法。

import java.util.ArrayList;
import java.util.List;

public class ComputerOrder<T> extends GenericOrder<T> {
    private List<ComputerPart> computerParts = new ArrayList<ComputerPart>();
    private String orderType = "Computer Parts";

public ComputerOrder() {
    super();

}

public void addProduct(ComputerPart newProduct) {

    computerParts.add(newProduct);

}

public String getOrderType() {
    return orderType;
}

public int getNumberOfProducts() {
    return computerParts.size();
}


public List<T> getProducts()
{

    return computerParts;
}


}

我得到的错误说不能从List(ComputerPart)转换为List<T>


I have a base class called GenericOrder that can be used to create an order with any type of products, then I have subclasses of that order that are more specific. My problem is with my ComputerOrder class and a method that I'm overriding. Here's the base class code.

import java.util.List;
import java.util.ArrayList;

public class GenericOrder<T> {

private long orderNumber;
private List<T> products;
private T theClass;

public GenericOrder() 
{
    products = new ArrayList<T>();
    orderNumber = System.currentTimeMillis();
}

public long getOrderNumber() {
    return orderNumber;
}

public void addProduct(T newProduct) {
    products.add(newProduct);

}

public int getNumberOfProducts() {
    return products.size();
}

public List<T> getProducts()
{
    return products;
}

public void setProducts(List<T> products)
{
    this.products = products;
}

public T get()
{
    return theClass;
}

public void set(T theClass)
{
    this.theClass = theClass;
}
}

And here is my subClass code. The getProducts is the method I'm having trouble with.

import java.util.ArrayList;
import java.util.List;

public class ComputerOrder<T> extends GenericOrder<T> {
    private List<ComputerPart> computerParts = new ArrayList<ComputerPart>();
    private String orderType = "Computer Parts";

public ComputerOrder() {
    super();

}

public void addProduct(ComputerPart newProduct) {

    computerParts.add(newProduct);

}

public String getOrderType() {
    return orderType;
}

public int getNumberOfProducts() {
    return computerParts.size();
}


public List<T> getProducts()
{

    return computerParts;
}


}

The Error I get says cannot convert from List(ComputerPart) to List<T>


原文:https://stackoverflow.com/questions/34315620
更新时间:2022-04-19 19:04

最满意答案

您应该使用您创建的持续时间方法调用相同的动画,以便向右设置动画并在完成时调用。 像这样的东西:

func animateRight()
{
    UIView.animateWithDuration(1.0, delay: 0.0, options:[], animations: {
        self.label.center.x = self.view.frame.width
        }, completion: { finished in
            if finished {
                self.animateLeft()
            }
    })
}

func animateLeft()
{
    UIView.animateWithDuration(2.0, delay: 0.0, options: [ .Autoreverse, .Repeat, .CurveEaseInOut, .BeginFromCurrentState], animations: {
        self.label.frame.origin.x = 0.0
        }, completion: nil)
}

You should call the same animate with duration method you create for animating to right and call in completion. Something like this:

func animateRight()
{
    UIView.animate(withDuration: 1.0, delay: 0.0, options: [], animations: {
        self.label.center.x = self.view.frame.width
    }, completion: { finished in
        if finished {
            self.animateLeft()
        }
    })
}

func animateLeft()
{
    UIView.animate(withDuration: 2.0, delay: 0.0, options: [ .autoreverse, .repeat, .curveEaseInOut, .beginFromCurrentState], animations: {
        self.label.frame.origin.x = 0.0
    }, completion: nil)
}

相关问答

更多
  • 您应该使用您创建的持续时间方法调用相同的动画,以便向右设置动画并在完成时调用。 像这样的东西: func animateRight() { UIView.animateWithDuration(1.0, delay: 0.0, options:[], animations: { self.label.center.x = self.view.frame.width }, completion: { finished in if finished ...
  • 您正在使用AutoLayout。 使用自动布局时,无法直接更改frame 。 您必须设置约束,然后设置约束中的更改动画以设置视图的动画。 另外,你不能做blah.frame.origin.y += 100 IIRC CGrect上的原点是只读的。 你可以做... Blah.frame = CGRectOffset(blah.frame, 0, 100) 但是再一次,这不适用于自动布局。 编辑 原点可以在swift中设置。 谢谢jrturton :-) You are using AutoLayout. Y ...
  • 您将在旧subview的顶部添加新subview subview 。 当您调用addSubView函数时,新的子视图将添加到旧的子视图之上。 这就是动画更改不可见的原因。 尝试添加 self.view.bringSubviewToFront(viewToRemove) 在开始动画之前。 或者您可以直接在旧使用下添加新的子视图。 self.view.insertSubview(createView(UIColor.redColor()), belowSubview: viewToRemove) You a ...
  • 用这个: [UIView animateWithDuration:3 delay:0 options:UIViewAnimationTransitionNone | UIViewAnimationOptionCurveLinear animations:^(void){ ... Use this: [UIView animateWithDuration:3 delay:0 options:UIViewAnimationTransitionNone | UIViewAnimationOptionCurve ...
  • 我认为你的意思是在你的第一个片段中使用returnToRootViewController ,因为returnToRootViewController不是returnToRootViewController上的方法。 你的问题是, popToRootViewControllerAnimated有一个返回值(视图控制器的数组从导航堆栈中删除)。 即使您试图放弃返回值,这也会造成麻烦。 当Swift看到一个带有返回值的函数/方法调用作为闭包的最后一行时,它假定您使用隐式返回值的闭包简写语法。 (这种类型可以让你 ...
  • 您正在取消动画: [_backgroundView.layer removeAllAnimations]; 但您可能会忘记导入QuartzCore.h: #import 如果没有帮助尝试这个: [CATransaction begin]; [_backgroundView.layer removeAllAnimations]; [CATransaction commit]; 如果它没有帮助尝试将此行添加到上面的代码: [CATransaction f ...
  • 您正在一个块中设置状态值,该块将异步执行。 这意味着,在执行块之后,不保证返回语句。 要知道动画何时结束,您需要以不同的方式声明方法。 + (void)showAnimationFirstContent:(UIView *)view completion:(void (^)(void))callbackBlock{ CGRect show = [SwFirstContent rectFirstContentShow]; [UIView animateWithDuration:DURATI ...
  • 由于您无法在协议声明本身内部提供任何实现,因此您应该在默认实现中引用UIView类。 我希望这三个模板案例中的一个是你需要的: import UIKit protocol SomeProtocol { static func animateWithDuration(duration: NSTimeInterval, delay: NSTimeInterval, options: UIViewAnimationOptions, animations: () -> Void, completion: ...
  • 您的原始代码虽然工作异常,但实际上定义不明确。 在将视图添加到视图层次结构之前,不应启动动画。 你正在逃避它,但不要这样做。 在你接近正确的第二种方法中,问题是你已经使background成为非惰性计算属性。 所以每次调用model.background ,都会得到一个不同的对象。 鉴于你是如何写这个,背景的lazy属性可能是你真正的意思。 正如@dasdom所说, BoardModel是一个非常糟糕的视图名称。 这应该是BoardView 。 一般来说,你的流程有点奇怪。 我可能BoardView为自己添 ...
  • 你可以很容易地链接动画; 在完成时开始另一个: 这里举例说明: //1st animation UIView.animateWithDuration(1.0, delay: 0.0, options: .CurveEaseInOut | .AllowUserInteraction, animations: { //some code ex : view.layer.alpha = 0.0 }, completion: { finished in ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)