Constants
lượt xem 1
download
Constant's objectives is describe ways to create constants (const, readonly, enum). It introduce about motivation; Const; Const local variable; Const field; Const and static; Compile time const; Limited const references; No const parameters;...
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Constants
- Constants
- Objectives • Describe ways to create constants – const – readonly – enum 2
- Motivation • Idea of constant is useful – makes programs more readable – allows more compile time error checking 3
- Const • Keyword const used to indicate compile time constant – applicable to local variables – applicable to fields 4
- Const local variable • Local variable can be declared constant – must be initialized inline – value cannot be changed later double Area(double radius) { constant const double pi = 3.14; local variable return pi * radius * radius; } 5
- Const field • Field can be declared constant – must be initialized inline – value cannot be changed later public class Math { constant public const double PI = 3.14159265358979323846; fields public const double E = 2.7182818284590452354; ... } 6
- Const and static • Field cannot be both declared both const and static – not needed – const field automatically static – saves memory by generating single copy public class Math { implicitly public const double PI = 3.14159265358979323846; static public const double E = 2.7182818284590452354; ... } double radius = 1.5; access using type name double area = Math.PI * radius * radius; 7
- Compile time const • const provides restrictive notion of constant – value must be computable at compile time void Process(int width, int height) { error, value must be const int area = width * height; compile time constant ... } 8
- Limited const references • Value for const reference must be compile time constant – references can only be null – too limited to be truly useful • Works for strings since compile time literals exist ok const Person p = null; error const Person ann = new Person("Ann"); ok const string greeting = "hello"; 9
- No const parameters • Parameters cannot be const – not value type parameters – not reference type parameters – parameter value is not determined at compile time error, const parameters double Average(const int a, const int b) not supported { ... } error, const parameters double Process(const Person p) not supported { ... } 10
- Readonly • Keyword readonly used to indicate runtime constant – applicable only to fields 11
- Readonly field • Field can be declared readonly – can only be initialized using variable initializer or in constructor – value cannot be changed later – compiler warning if not set – value used for initialization can be determined at runtime class Person { readonly field readonly DateTime dob; public Person(DateTime dob) { this.dob = dob; } ... } 12
- Readonly and static • Fields often made both readonly and static – more flexible than const – prevents write access – allows runtime determination of value – saves memory by generating single copy struct Point { ok public static readonly Point origin = new Point(0, 0); ... } 13
- Enum • Keyword enum used to create new type – with corresponding set of symbolic constants 14
- Enum definition • Can create enumeration value type – use keyword enum – specify type name – give list of named constants define enum enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday } 15
- Enum use • Can create variables of enum type – use enum name as type name • Can use values listed in enum – values are scoped inside enum – prefix with enum name for access create variable Day d; d = Day.Monday; if (d == Day.Saturday || d == Day.Sunday) Console.WriteLine("weekend"); 16
- Enum underlying type • Enum uses an underlying type for representation – default is int – can use any integral type except char represented enum Day using int { Sunday, represented enum ErrorCode : byte using short Monday, { Tuesday, DomainError, Wednesday, RangeError Thursday, } Friday, Saturday } 17
- Enum values • Enum constants have values – default is sequential starting at 0 – can explicitly specify any value and others follow in sequence enum Day enum Day { { default Sunday, // 0 specify Sunday = 1, values Monday, // 1 value Monday, // 2 Tuesday, // 2 Tuesday, // 3 Wednesday, // 3 Wednesday, // 4 Thursday, // 4 Thursday = 10, Friday, // 5 Friday, // 11 Saturday // 6 Saturday // 12 } } 18
- Enum and casting • Must cast to convert between enum and underlying type – cast needed in either direction – helps reduce chance of assigning invalid value convert to Day Day d = (Day)3; convert to int int i = (int)d; 19
- Enum inheritance hierarchy • enum is part of type hierarchy – ultimately descends from Object like all types – automatically derived from the value type Enum – Enum class provides many convenient utility methods Object ValueType Enum user enum 20
CÓ THỂ BẠN MUỐN DOWNLOAD
-
MS Excel - Bài 6: Tính toán trong Excel
5 p | 611 | 130
-
Các đặc điểm C++ áp dụng cho class Lập trình hướng đối tượng
15 p | 370 | 77
-
18.5. Switching Locations
5 p | 86 | 6
-
LỆNH KHAI BÁO
17 p | 78 | 6
-
Điều khiển logic - Ngôn ngữ lập trình và ứng dụng - Lâm Tăng Đức - 3
16 p | 97 | 6
-
Bài giảng Cấu trúc dữ liệu: Chương 1 - Nguyễn Xuân Vinh
23 p | 84 | 5
-
Web Programming with Java - Java Basics
37 p | 88 | 5
-
C++ Lab 14 SERACHI
9 p | 54 | 3
-
C++ Lab 3 Branching
8 p | 85 | 3
-
Chapter 18 – Change Management
8 p | 54 | 3
-
TCP/IP - Security PerspectiveUpper Layers
15 p | 40 | 2
-
C++ Lab 15 Review of Arrays, Array of Objects and Vector Dr. John Abraham, Professor
7 p | 60 | 2
-
C++ Lab 16 Pointers
5 p | 56 | 2
-
Bài 2 Mục tiêu: Kết thúc bài học này, bạn có thể: Hiểu và sử dụng
15 p | 66 | 2
-
Lecture Windows programming: Chapter 2 - Châu Thị Bảo Hà
68 p | 42 | 2
-
Lecture Java programming language: Variables and constants - Ho Dac Hung
15 p | 39 | 2
-
COMPUTATIONAL COMPLEXITY
5 p | 39 | 1
Chịu trách nhiệm nội dung:
Nguyễn Công Hà - Giám đốc Công ty TNHH TÀI LIỆU TRỰC TUYẾN VI NA
LIÊN HỆ
Địa chỉ: P402, 54A Nơ Trang Long, Phường 14, Q.Bình Thạnh, TP.HCM
Hotline: 093 303 0098
Email: support@tailieu.vn