简体   繁体   English

UIView / UIImageView继承

[英]UIView/UIImageView inheritance

In my program, I have a class called Object, which inherits from UIView. 在我的程序中,我有一个名为Object的类,它继承自UIView。 I also have other objects called: circle, square, triangle which inherit from Object. 我还有其他对象叫做:圆形,方形,三角形,它继承自Object。

My problem is that I would like to make a possible object called Image, which also inherits from Object. 我的问题是我想创建一个名为Image的可能对象,它也继承自Object。 I would like all objects on the screen to inherit from Object, and then pass an NSArray pointer to my physics functions that contains all objects on the screen. 我希望屏幕上的所有对象都继承自Object,然后将NSArray指针传递给包含屏幕上所有对象的物理函数。

What is the best way to have my Image object acquire all the properties of my Object class? 让我的Image对象获取Object类的所有属性的最佳方法是什么?

Should I just create new properties for my Image object, or is there a way to inherit Object and still be able to display an image, since Object inherits from UIView and not UIImageView. 我应该只为我的Image对象创建新属性,还是有一种方法可以继承Object并仍然能够显示图像,因为Object继承自UIView而不是UIImageView。

You've realized that a class cannot extend two parent classes. 您已经意识到一个类不能扩展两个父类。

Could your "Object" be a Protocol instead of a Class? 您的“对象”可以是协议而不是类吗? That way circle, square and image could all implement that protocol while extending either UIView or UIImageView as needed? 这样,圆形,方形和图像都可以实现该协议,同时根据需要扩展UIView或UIImageView?

最简单的方法是让Image继承自Object,然后让Image拥有UIImageView类型的子视图。

That's a very…unusual way to do it. 这是一种非常不寻常的方式。 Have you had any Java experience? 你有Java经验吗?

What are you trying to do with the Shapes? 你想用Shapes做什么? Are you trying to draw them in those respective classes? 你想在各自的班级中画画吗? Here, you're blurring the lines of MVC. 在这里,你模糊了MVC的界限。 From what it seems like, your Circle, Square, etc. classes are both a model and a view, which shouldn't happen. 从它看起来,你的Circle,Square等类都是模型和视图,这不应该发生。 If you're trying to draw the shapes, you wouldn't create classes for them—you'd use the Quartz drawing methods to draw them in your view controller's view. 如果您正在尝试绘制形状,则不会为它们创建类 - 您将使用Quartz绘图方法在视图控制器的视图中绘制它们。 If you're trying to store info about those shapes, you wouldn't want them to inherit from UIView. 如果您尝试存储有关这些形状的信息,则不希望它们从UIView继承。 NSObject (or a custom Shape class) would be a better option. NSObject(或自定义Shape类)将是更好的选择。 (It's also a bad idea to call the class "Object"—it could get confused with NSObject). (调用类“Object”也是一个坏主意 - 它可能会与NSObject混淆)。

You're second paragraph is where my first question came from. 你的第二段是我的第一个问题来自哪里。 Your NSArray does not have to contain variables of the same type—this is very different from Java's arrays (and other languages such as C# and C). 您NSArray中没有包含的变量类型相同,这是从Java中的数组(及其他语言如C#和C)有很大不同。 Therefore, unless there were some methods or ivars you wanted to inherit, there was no reason to declare the Object class. 因此,除非您想继承某些方法或ivars,否则没有理由声明Object类。

As for displaying images, what are you intending to do with Image? 至于显示图像,你打算用Image做什么? If you just want to display images (a view), then make it a subclass of UIImageView (which is a subclass of UIView). 如果您只想显示图像(视图),那么将其作为UIImageView的子类(它是UIView的子类)。 If you want do store information about images, make it a subclass of Shape (or in this case, your Object). 如果您想存储有关图像的信息,请将其设为Shape的子类(或者在本例中为您的Object)。 However, you really can't do what you want to (inherit from both Object and UIImageView) because you shouldn't combine functionality of views and models. 但是,您实际上无法执行您想要的操作(从Object和UIImageView继承),因为您不应该组合视图和模型的功能。

Really, your best option here is to revisit your classes, make them adhere to MVC, and rethink your inheritance chain. 真的,你最好的选择是重新访问你的课程,让他们坚持MVC,并重新思考你的继承链。 Hope this helps! 希望这可以帮助!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM