泛型的覆盖方法(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)
toList<T>
原文:https://stackoverflow.com/questions/34315620
最满意答案
您应该使用您创建的持续时间方法调用相同的动画,以便向右设置动画并在完成时调用。 像这样的东西:
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看到一个带有返回值的函数/方法调用作为闭包的最后一行时,它假定您使用隐式返回值的闭包简写语法。 (这种类型可以让你 ...
-
停止animateWithDuration(Stop animateWithDuration)[2022-05-03]
您正在取消动画: [_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 ...