//自实现属性
来源:Visual C# 2008 How to Program,Third Edition
GradeBookTest.cs
- using System;
- public class GradeBookTest
- {
- //Main method begins program execution
- public static void Main(string[] args)
- {
- GradeBook myGradeBook = new GradeBook();
-
- Console.WriteLine("Initial course name is:'{0}'\n",myGradeBook.CourseName);
-
- //promptfor and read course name
- Console.WriteLine("Please enter the course name:");
- myGradeBook.CourseName = Console.ReadLine();
-
- myGradeBook.DisplayMessage();
-
-
- Console.ReadKey(true);
- }
- }
GradeBook.cs
- using System;
- public class GradeBook
- {
- /*auto-implemented property CourseName implicitly creates
- an instance variable for this GradeBook's course name*/
- public string CourseName {get;set;}
-
- //display a welcome message to the GradeBook user
- public void DisplayMessage()
- {
- /*use auto-implemented property CourseName to get the
- name of the course taht this GradeBook represents*/
- Console.WriteLine("Welcome to the grade book for\n{0}!",CourseName);
- }
- }
阅读(830) | 评论(0) | 转发(0) |