Chinaunix首页 | 论坛 | 博客
  • 博客访问: 22313
  • 博文数量: 10
  • 博客积分: 251
  • 博客等级: 二等列兵
  • 技术积分: 125
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-10 16:30
文章分类
文章存档

2011年(10)

我的朋友
最近访客

分类: 嵌入式

2011-06-27 09:14:16

//利用构造函数初始化对象
来源:Visual C# 2008 How to Program,Third Edition
GradeBookTest.cs
  1. using System;

  2. public class GradeBookTest
  3. {
  4.     //Main method begins program execution

  5.     public static void Main(string[] args)
  6.     {
  7.         GradeBook myGradeBook1 = new GradeBook("Your are the best!Nobody can beat you.");
  8.         
  9.         GradeBook myGradeBook2 = new GradeBook("I'm so lonely,my heart is so empty!");
  10.         
  11.         Console.WriteLine("Initial course name is:'{0}'\n",myGradeBook1.CourseName);
  12.         Console.WriteLine("Initial course name is:'{0}'\n",myGradeBook2.CourseName);
  13.     
  14.         myGradeBook1.DisplayMessage();
  15.         myGradeBook2.DisplayMessage();
  16.         
  17.         Console.ReadKey(true);
  18.     }
  19. }
GradeBook.cs
  1. using System;

  2. public class GradeBook
  3. {
  4.     /*auto-implemented property CourseName implicitly created an
  5.      instance variable for this GradeBook's course name*/
  6.     public string CourseName {get;set;}
  7.     
  8.     //constructor initializes auto-implemented property

  9.     //CourseName with string supplied as argument

  10.     public GradeBook(string name)
  11.     {
  12.         CourseName = name;
  13.     }
  14.     
  15.     //display a welcome message to the GradeBook user

  16.     public void DisplayMessage()
  17.     {
  18.         Console.WriteLine("Welcome to the grade book for\n{0}!",CourseName);
  19.     }
  20. }
Our life is fritter away with detial……Simplify,simplify!
阅读(883) | 评论(0) | 转发(0) |
0

上一篇:class And object_Two

下一篇:divide and conquer

给主人留下些什么吧!~~