Download presentation
Presentation is loading. Please wait.
Published byAshley Morris Modified over 6 years ago
1
15: Object Object Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
2
Objectives Introduce the Object class concept references methods
overriding methods Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
3
Unified inheritance hierarchy
15: Object Unified inheritance hierarchy Type system is unified all types derive from System.Object root of type hierarchy Object Person Shape Student Employee Circle Rectangle Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
4
Object class alias C# provides a convenient alias for Object
lower case object object a; System.Object b; equivalent Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
5
Motivation Unified inheritance hierarchy is useful
15: Object Motivation Unified inheritance hierarchy is useful Object reference is generic handle Object methods universally supported Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
6
Object reference Object reference can refer to anything
legal because all types are derived from Object object a = new Person (); object b = new Student (); object c = new Stock (); object d = new Rational(); use object references Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
7
Access through Object reference
Access through Object reference limited can only access Object members object o = new Person(); o.Name = "Bob"; error, not Object member class Person { public string Name; public int Age; ... } Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
8
Cast Can cast to recover more specific reference
15: Object Cast Can cast to recover more specific reference allows access to entire object void Rename(object o) { Person p = (Person)o; p.Name = "Bob"; ... } cast access Person member Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
9
Object methods Object class provides several methods
provide basic services to all types class Object { public Type GetType () public virtual string ToString () public virtual int GetHashCode () public virtual bool Equals (object o) public static bool Equals (object a, object b) ... public static bool ReferenceEquals(object a, object b) ... protected void Finalize () protected object MemberwiseClone() } Object methods Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
10
GetType GetType gives access to type metadata returns Type object
not virtual, cannot be overridden Person p = new Person("Bob", 32); Type t = p.GetType(); get Type object for Person Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
11
Type object Type object contains complete information about the type
runtime type information facility called reflection public class Type { public string FullName { get; } public bool IsAbstract { get; } public bool IsClass { get; } public bool IsPrimitive { get; } public bool IsInterface { get; } public Type BaseType { get; } public ConstructorInfo[] GetConstructors() ... public FieldInfo [] GetFields () ... public MethodInfo [] GetMethods () ... public Type [] GetInterfaces () ... ... } properties methods Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
12
Unique Type object One Type object per type shared by all instances
Person a = new Person("Ann", 27); Person b = new Person("Bob", 32); Type ta = a.GetType(); Type tb = b.GetType(); references are to same Type object Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
13
15: Object Object ToString Object version of ToString returns fully qualified type name Person p = new Person("Bob", 32); string s = p.ToString(); returns "Person" Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
14
Simple type string conversion
15: Object Simple type string conversion Simple types have overridden ToString method returns string representation of data implicitly called when simple type used in string context int age = 32; string s = age.ToString(); string t = "Bob " + age; call explicitly called implicitly when used in string context Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
15
Override ToString Common to override ToString virtual method
15: Object Override ToString Common to override ToString virtual method return more specific string containing object data class Person { public string Name; public int Age; public override string ToString() return Name + " " + Age; } ... override ToString Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
16
Automatic use of ToString
15: Object Automatic use of ToString ToString sometimes called automatically when object used in string context inside WriteLine(object) method Person p = new Person("Bob", 32); string s = "Hello " + p; Console.WriteLine(p); calls ToString calls ToString Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
17
Dynamic binding ToString is dynamically bound
15: Object Dynamic binding ToString is dynamically bound derived class version called through Object reference object o = new Person("Bob", 32); string s = o.ToString(); calls Person.ToString Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
18
Hashing Hash table is efficient way to store key/value pairs
15: Object Hashing Hash table is efficient way to store key/value pairs Hash code computed on key to determine placement in table Person bob = new Person("Bob", 32); House home = new House(...); Hashtable addresses = new Hashtable(); addresses.Add(bob, home); hash code computed to position the new entry in the table Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
19
Object GetHashCode Object supplies GetHashCode method
called by library hash table when entry added few guarantees given about exact behavior of Object version public class Object { public virtual int GetHashCode() ... } object GetHashCode Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
20
Override GetHashCode Can override virtual method GetHashCode
15: Object Override GetHashCode Can override virtual method GetHashCode typical motivation is to increase performance of hash tables goal is to get a random distribution of hash codes class Person { public override int GetHashCode() return Name.GetHashCode() * Age; } ... } override GetHashCode hash on Person data Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
21
Summary Object class is root of type system
all types are derived from Object Object reference is generic handle all types automatically convert to Object reference cast required when recovering original type Object methods provide basic operations supported by all types Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.