码迷,mamicode.com
首页 > 其他好文 > 详细

面向对象-封装

时间:2016-06-16 23:23:41      阅读:432      评论:0      收藏:0      [点我收藏+]

标签:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 
  6 namespace 封装
  7 {
在其他类里引用
8 class Student 9 { 10 //成员方法 11 public void Write() 12 { 13 Console.WriteLine("我是Stu的Write方法!"); 14 } 15 16 /// <summary> 17 /// 求和方法 18 /// </summary> 19 /// <param name="a">第一个数</param> 20 /// <param name="b">第二个数</param> 21 /// <returns></returns> 22 public int Sum(int a,int b) 23 { 24 return a + b; 25 } 26 27 28 29 30 31 32 private string _Code; 33 34 public string Code 35 { 36 get 37 { 38 return _Code; 39 } 40 41 set 42 { 43 _Code = value; 44 } 45 } 46 47 private string _Name; 48 49 /// <summary> 50 /// 学生姓名 51 /// </summary> 52 public string Name 53 { 54 get { return _Name; } 55 set { _Name = value; } 56 } 57 58 59 60 61 62 private decimal _Score; 63 64 public decimal Score 65 { 66 get 67 { 68 return _Score; 69 } 70 set 71 { 72 if (value < 0 || value > 100) 73 { 74 _Score = 0; 75 } 76 else 77 { 78 _Score = value; 79 } 80 } 81 } 82 83 84 private DateTime _Birthday; 85 86 public DateTime Birthday 87 { 88 get 89 { 90 return _Birthday; 91 } 92 set 93 { 94 _Birthday = value; 95 } 96 } 97 98 public string BirthdayStr 99 { 100 get 101 { 102 return _Birthday.ToString("yyyy年MM月dd日"); 103 } 104 } 105 106 public string CodeAndScoreSum 107 { 108 get 109 { 110 return _Code + _Score; 111 } 112 } 113 114 115 } 116 }
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace 封装
 7 {
 8     class Teacher
 9     {
第二个类
10 //构造函数 11 public Teacher() 12 { 13 _Name = "切西瓜"; 14 } 15 16 private string _Name; 17 18 public string Name 19 { 20 get { return _Name; } 21 set { _Name = value; } 22 } 23 24 25 } 26 }
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace 封装
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             Student s = new Student(); //实例化
13 
14             s.Write();
15             int i = s.Sum(5, 5);
16 
17             Console.WriteLine(i);
18 
19             Teacher t = new Teacher();
20             Console.WriteLine(t.Name);
21 
22 
23 
24 
25             #region
26             //s.Code = "101";
27             //s.Score = 101;
28             //s.Birthday = DateTime.Now;
29             ////s.BirthdayStr = "";
30 
31             //Console.WriteLine(s.Code);
32             //Console.WriteLine(s.Score);
33             //Console.WriteLine(s.Birthday);
34             //Console.WriteLine(s.BirthdayStr);
35             //Console.WriteLine(s.CodeAndScoreSum);
36             #endregion
37 
38 
39             Console.ReadLine();
40         }
41     }
42 }

技术分享

面向对象-封装

标签:

原文地址:http://www.cnblogs.com/tonyhere/p/5592283.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!