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

Started by
9 comments, last by RLS0812 9 years, 6 months ago

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

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").

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 ?


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 ?

In the bracket, you type the current classes parent class, so thats "is-a". See https://docs.python.org/2/tutorial/classes.html chapter 9.5.

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

Phil...is that you?

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

sorry for hard understanding


hil...is that you?

i shouldn't ask sorry smile.png

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.

Sean Middleditch – Game Systems Engineer – Join my team!

Sorry for hard understanding smile.png

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

Hahaha, Beer, play nice now.

"The code you write when you learn a new language is shit.
You either already know that and you are wise, or you don’t realize it for many years and you are an idiot. Either way, your learning code is objectively shit." - L. Spiro

"This is called programming. The art of typing shit into an editor/IDE is not programming, it's basically data entry. The part that makes a programmer a programmer is their problem solving skills." - Serapth

"The 'friend' relationship in c++ is the tightest coupling you can give two objects. Friends can reach out and touch your privates." - frob

This topic is closed to new replies.

Advertisement