了解对象继承 [英] Understanding Object Inheritance

查看:116
本文介绍了了解对象继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在上一个问题 (在此链接) ,有一些尚未解决的关于对象继承的问题仍然不清楚。 (我使用的术语可能不正确,请进行相应修改 - 谢谢)



超类不继承已明确分配给它的子类的属性。代码示例:

  UIView * view = nil; 
UILabel * label = [[UILabel alloc] init];
label.text = @some text ...;
view = label;

//视图未继承任何UILabel属性
view.text = @new text ...; // error
view.textColor = [UIColor redColor]; //错误

只是想要绕过这个。



谢谢。

解决方案

类继承就像特殊化。



想想类Person。



一个人知道如何呼吸,行走,说话, (所以我们假设所有人都有 MakeMeASandwich 方法。)



一个人可能有一个专门的工作:木匠,厨师,水管工,IT人员,卡车司机等。



如果您有变量

  Person * aPerson; 

这是一个指向Person的指针。你不知道那个人做什么交易,你只知道这是一个人。您可以将水管工对象分配给该变量,或者Carpenter对象或任何其他专门的Person,因为所有这些类型也属于一般类Person。



当你指的是aPerson,编译器不知道它在说什么类型的人。



编译器可能知道它可以告诉Person做一个三明治,所有的人都知道如何做到这一点。 (所有的专业Person类如Carpenter和Plumber继承自父类Person的makeMeASandwich方法。)



除非你告诉编译器:



这个人是一个木匠,那么编译器会抱怨,如果你要求人为你构建一个套牌,因为你的普通人没有buildADeck方法。



当你说

  


$ b

你告诉编译器这个人是一个木匠。相信我,我知道,请他/她建一个甲板。编译器假设你知道你在说什么,那个人真的是一个木匠。



如果在运行时,aPerson变量指向的Person对象不是Carpenter类型,程序会崩溃,因为当你告诉糕点厨师建一个甲板,他/她会很困惑,不知道该怎么办。



如果您说:

  Person * aPerson = [[Carpenter alloc] init]; 

你告诉编译器创建一个Carpenter,并将其存储在通用的Person变量。编译器会立即忘记它创建的人是一个木匠。它只是知道它是一个人。



所以,编译器会抱怨,如果你试图说

  [aPerson buildADeck]; 

您必须说

  [(Carpenter *)aPerson buildADeck] 

如何做一个三明治,你可以说

  [aPerson makeMeASandwich] 
pre>

并使其工作。即使人是水喉匠,他/她也知道如何做一个三明治,并且(希望)使你成为一个。



你也可以说



水管工* aPlumber = [[Plumber alloc] init];
[aPlumber makeMeASandwich];



因为管道工也是人,并且知道如何做三明治。 (IN OOP术语,管道工从他们的父Person类继承makeMeASandwich方法。)


In a previous question (linked here), there were some unresolved issues about object inheritance which remain unclear. (my use of terminologies may be incorrect - please edit accordingly - thank you)

Put simply, why does the superclass not inherit the properties of a subclass that has been expressly assigned to it. A code example:

UIView *view = nil;
UILabel *label = [[UILabel alloc] init];
label.text = @"some text...";
view = label;

// view has not inherited any UILabel properties
view.text = @"new text...";              //error
view.textColor = [UIColor redColor];     //error

Just would like to get my head around this.

Thank you.

解决方案

Class inheritance is like specialization.

Think of the class Person.

A Person knows how to breath, walk, talk, feed himself, etc. (So let's say all people have a MakeMeASandwich method.)

A person might have a specialized job: carpenter, cook, plumber, IT person, truck driver, etc.

If you have a variable

Person *aPerson;

It is a pointer to a Person. You don't know what trade that Person does, you just know it's a Person. You can assign a Plumber object to that variable, or a Carpenter object, or any other specialized Person, because all of those types also belong to the general class Person.

When you refer to aPerson, the compiler has no idea what type of person it's talking to.

The compiler might know that it can tell the Person to make a sandwich, and assume all people know how to do that. (All the specialized Person classes like Carpenter and Plumber inherit the makeMeASandwich method from their parent Person class.)

Unless you tell the compiler:

"This Person is a Carpenter" then the compiler is going to complain if you ask the Person to build you a deck, because your average Person doesn't have a buildADeck method. Only Carpenter-type people have that method.

When you say

[(Carpenter *)aPerson buildADeck]

You are telling the compiler "This Person is a Carpenter. Trust me. I know. Ask him/her to build a deck." The compiler assumes you know what you're talking about, and that that Person really is a Carpenter. It will ask the Person to build a deck.

If, at runtime, it turns out the Person object pointed to by the aPerson variable is not of type Carpenter, the program will crash because when you tell the pastry chef to build a deck, he/she will be very confused and not know what to do. He/she might even get mad and quit.

If you say:

Person *aPerson = [[Carpenter alloc] init];

You are telling the compiler to create a Carpenter, and store it in a general-purpose "Person" variable. The compiler promptly forgets that the person it created was a Carpenter. It just knows that it's a Person.

So, the compiler will the complain if you try to say

[aPerson buildADeck];

You have to say

[(Carpenter *)aPerson buildADeck]

But, since all people know how to make a sandwich, you can say

[aPerson makeMeASandwich] 

and have it work. Even if the Person is a Plumber, he/she will know how to make a sandwich, and will (hopefully) make you one. It will probably be a better sandwich if the Person is a cook, but at least you'll get a sandwich.

you can also say

Plumber *aPlumber = [[Plumber alloc] init]; [aPlumber makeMeASandwich];

because Plumbers are people too, and know how to make sandwiches. (IN OOP terms, Plumbers inherit the makeMeASandwich method from their parent Person class.)

这篇关于了解对象继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆