Jump to content
  • Advertisement
Sign in to follow this  
koka282

Can you give me an example shows the meaning of the concept?

This topic is 1341 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

You use the phrase is-a when you talk about objects and classes being related to each other by a class relationship. You use has-a when you talk about objects and classes that are related only because they reference each other.

 

 

Can you give me an example shows the meaning ??

 

i can't understand what does he mean with ! related to each other by a class relationship & because they reference each other.

 

 

 

def __init__(self, name):

      self.name = name

why we must type this line ?? (self.name = name)

 

Thanks all smile.png

Edited by koka282

Share this post


Link to post
Share on other sites
Advertisement

I can't think of any practical example for "is-a" in programming right now, but conceptually, take this:

 

A teacher is a human. This can be modelled by having a class inheritance:

class Human

class Teach(Human)

A teacher also might have a schoolbook. This is a "has-a" relation, as the teacher doesn't derive of schoolbook, but instead uses it:

class Human

class Schoolbook

class Teacher(Human)

    def __init__(self)
        self.book = Schoolbook()

This is achieved via aggregation, as I've shown here. Thats whats meant by "reference each other". The Teacher references a Schoolbook, in that it posses & possibly uses it.

 


def __init__(self, name):

self.name = name

why we must type this line ?? (self.name = name)

 

__init__ is a function, executed with a bunch of parameters. at that point "name" is merely a local variable, but you want to use it as a class variable. So in order to do that, you have to assign the local "name" variable to the instance of the class ("self"), by calling "self.name" (this instances "name" variable) "= name" (is assigned the local "name").

Share this post


Link to post
Share on other sites

Ty

but please answer this question

class Teacher(Human)

    def __init__(self,name)
        self.name=name

now we are talking about the object that object have name etc

so you are talking about the human not the teacher ,, between the bracket i type the object right ??

so now saying human has-a name not teacher right ?

Share this post


Link to post
Share on other sites

 must be defined in a scope containing the derived class definition

 

 

 

but see :

class product(object):

why we type object !! 

where i type object smile.png !

 

Sorry for hard understanding smile.png

 

 

.................

 

but i think  in

class Teacher(Human)

    def __init__(self,name)
        self.name=name

it was inheritance

 

but in ..

class product(object):

i here type the object Because (it is not an inheritance from another class)

 

Thanks

Edited by koka282

Share this post


Link to post
Share on other sites

sorry for hard understanding 


hil...is that you?

i shouldn't ask sorry smile.png

Edited by koka282

Share this post


Link to post
Share on other sites

but in ..
class product(object):
i here type the object Because (it is not an inheritance from another class)


`product` inherits `object`, which is a built-in class in Python.

Share this post


Link to post
Share on other sites

Sorry for hard understanding smile.png

 

 

stop apologizing, just get your shit together, geez...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!