Properties 7: Properties Programming C# © 2003 DevelopMentor, Inc.

Slides:



Advertisements
Similar presentations
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Advertisements

Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
Understanding class definitions Looking inside classes.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
More about Class 靜宜大學資工系 蔡奇偉副教授 ©2011. 大綱 Instance Class Members Class members can be associated with an instance of the class or with the class as a.
Programming Logic and Design Using Methods. 2 Objectives Review how to use a simple method with local variables and constants Create a method that requires.
Chapter 10 Introduction to Classes
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Understanding class definitions Exploring source code 6.0.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Chapter 10 Thinking in Objects
Classes (Part 1) Lecture 3
Programming Logic and Design Seventh Edition
Chapter 10 Thinking in Objects
Review What is an object? What is a class?
Class Structure 15-Jun-18.
Chapter 10 Thinking in Objects
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Constructors and Other Tools Dr.
Encapsulation and Java Interface
Delegates and Events 14: Delegates and Events
Classes.
More about OOP and ADTs Classes
Lecture 14 Writing Classes part 2 Richard Gesick.
C++ for Engineers and Scientists Second Edition
Understanding class definitions
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
Lecture 13 Writing Classes Richard Gesick.
Class Structure 16-Nov-18.
Lecture 11 C Parameters Richard Gesick.
Chapter 9 Thinking in Objects
Defining Your Own Classes Part 1
Defining Your Own Classes
Phil Tayco Slide version 1.0 Created Oct 9, 2017
More about OOP and ADTs Classes
Finalization 17: Finalization
Chapter 9 Objects and Classes
Class Structure 7-Dec-18.
Constructors and Other Tools
Dr. Bhargavi Dept of CS CHRIST
The Object-Oriented Thought Process Chapter 04
Class Structure 2-Jan-19.
Chapter 9 Thinking in Objects
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
Class Structure 25-Feb-19.
COS 260 DAY 4 Tony Gauvin.
Object-Oriented Programming
Abstract Classes and Interfaces
CIS 199 Final Review.
Defining Classes and Methods
Barb Ericson Georgia Institute of Technology Oct 2005
Methods/Functions.
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
Binding 10: Binding Programming C# © 2003 DevelopMentor, Inc.
Classes and Methods 15-Aug-19.
Classes and Objects Systems Programming.
Introduction to Classes and Objects
Presentation transcript:

Properties 7: Properties Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Objectives Describe properties purpose definition use 7: Properties Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Public data Public data can be both good and bad 7: Properties Public data Public data can be both good and bad good because it allows clear and simple access syntax bad because it exposes implementation details of class class Person { public string name; ... } public field Person p = new Person(); p.name = "Bob"; string n = p.name; write read Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

7: Properties Private data Class typically has private fields and public access methods clients use methods to access fields class Person { private string name; public string GetName() return name; } public void SetName(string newName) name = newName; private field public read method public write method Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Advantage: access control 7: Properties Advantage: access control Private data lets class offer only desired access methods read-only write-only read and write class Person { private string name; public string GetName() return name; } supply only read method for read-only access Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Advantage: data validation 7: Properties Advantage: data validation Private data allows class to perform data validation methods can do error checking clients can not avoid validation code by accessing data directly class Person { public void SetName(string newName) if (newName == "") // error ... name = newName; } ... error checking Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Disadvantage: access method names 7: Properties Disadvantage: access method names Typical to prefix access method names with Get and Set follows common wisdom to make method names verb phrases but conflicts with concept of field as representing data property Person p = new Person(); p.SetName("Bob"); string n = p.GetName(); write read Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Disadvantage: access method usage 7: Properties Disadvantage: access method usage Syntax of access methods can be undesirable set does not use = so is not obviously a write operation get typically has awkward looking empty parentheses Person p = new Person(); p.SetName("Bob"); string n = p.GetName(); no assignment operator empty parentheses Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

7: Properties Properties Properties combine the advantages of public and private data have clean access syntax like public fields provide error checking opportunity like private fields Person has Name property Person p = new Person(); p.Name = "Ann"; string n = p.Name; write read Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Structure of property definition 7: Properties Structure of property definition Property definition has declaration of access level, type, and name code block with implementation class Person { public string Name ... } declaration of Name property implementation Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Properties storage Property does not provide any needed data storage typically define field to store data class Person { private string name; public string Name ... } field to store data Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Property accessors Property can define set and get accessors 7: Properties Property accessors Property can define set and get accessors definitions go inside implementation code block no explicit arguments or return types class Person { public string Name set ... } get write method read method Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Set implementation Set typically records new data for property 7: Properties Set implementation Set typically records new data for property new data passed in hidden argument called value class Person { private string name; public string Name set name = value; } ... use value argument Person p = new Person(); p.Name = "Ann"; calls set Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Get implementation Get typically returns current value of property 7: Properties Get implementation Get typically returns current value of property uses return to return value class Person { private string name; public string Name get return name; } ... return value of property Person p = new Person(); ... string n = p.Name; calls get Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Validation Property accessors can contain validation code class Person 7: Properties Validation Property accessors can contain validation code class Person { private string name; public string Name set if (value == "") // error ... name = value; } ... validation Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Access control Property may provide any access desired read-only 7: Properties Access control Property may provide any access desired read-only write-only read and write class Person { public int Age get ... } Age property supply get and omit set for read-only Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Data storage Property does not need to be backed by matching field 7: Properties Data storage Property does not need to be backed by matching field value may be produced in any way that is appropriate class Person { private DateTime dob; public int Age get int age = DateTime.Today.Year - dob.Year; DateTime birthday = dob.AddYears(age); if (birthday > DateTime.Today) age--; return age; } ... store date of birth calculate age birthday occurred yet this year? Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Static property Property can be static 7: Properties Static property Property can be static must be accessed through type name class Item { private static int revenue; public static int Revenue get { return revenue; } set { revenue = value; } } ... define static property Item.Revenue = 5; access using type name Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Access level Property accessors have same access level 7: Properties Access level Property accessors have same access level both public, both private, etc. class Person { public string Name set { ... } get { ... } } ... public property public read method public write method Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Summary Properties model traits of a class or object provide advantages of private field / public accessor pattern with clean user syntax Programming C# © 2003 DevelopMentor, Inc. 12/1/2003