//利用构造函数初始化对象
来源: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 myGradeBook1 = new GradeBook("Your are the best!Nobody can beat you.");
-
- GradeBook myGradeBook2 = new GradeBook("I'm so lonely,my heart is so empty!");
-
- Console.WriteLine("Initial course name is:'{0}'\n",myGradeBook1.CourseName);
- Console.WriteLine("Initial course name is:'{0}'\n",myGradeBook2.CourseName);
-
- myGradeBook1.DisplayMessage();
- myGradeBook2.DisplayMessage();
-
- Console.ReadKey(true);
- }
- }
GradeBook.cs
- using System;
- public class GradeBook
- {
- /*auto-implemented property CourseName implicitly created an
- instance variable for this GradeBook's course name*/
- public string CourseName {get;set;}
-
- //constructor initializes auto-implemented property
- //CourseName with string supplied as argument
- public GradeBook(string name)
- {
- CourseName = name;
- }
-
- //display a welcome message to the GradeBook user
- public void DisplayMessage()
- {
- Console.WriteLine("Welcome to the grade book for\n{0}!",CourseName);
- }
- }
Our life is fritter away with detial……Simplify,simplify!
阅读(918) | 评论(0) | 转发(0) |