c# - setting value's of a superclass (inheritence) -
i'm having trouble setting variables of superclass
i have following classes:
- computer(superclass)
- laptop(subclass)
- desktop(subclass)
in supperclass computer
have variable string name;
public class computer { protected string name; }
when call method changename(string yourname)
laptop class, should set variable name
in superclass computer
, this:
public class laptop : computer { public void changename(string yourname) { name = yourname; } }
when try name ,with properties, superclass computer
, returns null
. debugged see happend, , subclass laptop
changed name in superclass, when method changename
ended compiling, restored null
.
what may have caused this?
i think setting variable
laptop.changename(name);
and trying name
computer.name
.
you misunderstanding inheritance.
obviously null
. because computer
, laptop
2 different objects. laptop inherited doesn't mean can access computer
object using laptop
object.
after setting values using laptop.changename(name);
. print laptop.name
, name
you've set.
Comments
Post a Comment