zoukankan      html  css  js  c++  java
  • 面向对象之封装

    封装:打包

    可以把程序按某种规则分成很多“块“,块与块之间可能会有联系,每个块都有一个可变部分和一个稳定的部分。我们需要把可变的部分和稳定的部分分离出来,将稳定的部分 暴露给其他块,而将可变的部分隐藏起来,以便于随时可以让它修改。这项工作就是封装.

    面向对象的封装就是把事物的状态和行为封装在类中,使用类的人不需要知道类内部是怎么实现的,只要调用其中的属性和方法实现功能就行。

    类和对象本身就是封装的体现。

    1.属性封装了字段、
    2.方法的多个参数封装成了一个对象、
    3.将一堆代码封装到了一个方法中、
    4.将一些功能封装到了几个类中、
    5.将一些具有相同功能的代码封装到了一个程序集中(dll、exe),并且对外提供统一的访问接口。(属性名、方法名等。)

     1 namespace _01封装
     2 {
     3     class Program
     4     {
     5         static void Main(string[] args)
     6         {
     7             
     8             Person p = new Person();
     9             p.Name = "王宇";
    10             p.Age = 22;
    11             p.Gender = "";
    12             Show(p);
    13             Console.ReadKey();
    14         }
    15         /// <summary>
    16         /// 方法的多个参数封装为一个对象
    17         /// </summary>
    18         /// <param name="p"></param>
    19         public static void Show(Person p)
    20         {
    21             Console.WriteLine("name:{0},age:{1},gender:{2}", p.Name,p.Age,p.Gender);
    22         }
    23     }
    24     public class Person
    25     {
    26         //属性封装字段
    27         private string name;
    28 
    29         public string Name
    30         {
    31             get { return name; }
    32             set { name = value; }
    33         }
    34         private int age;
    35 
    36         public int Age
    37         {
    38             get { return age; }
    39             set { age = value; }
    40         }
    41         private string gender;
    42 
    43         public string Gender
    44         {
    45             get { return gender; }
    46             set { gender = value; }
    47         }
    48     }
    49 }
    封装举例
     1 namespace _03将多个参数封装到一个对象
     2 {
     3     public partial class Form1 : Form
     4     {
     5         public Form1()
     6         {
     7             InitializeComponent();
     8         }
     9 
    10         private void Form1_Load(object sender, EventArgs e)
    11         {
    12 
    13             Prov p1 = new Prov();
    14             p1.Name = "山东";
    15             p1.Id = 1;
    16             Prov p2 = new Prov();
    17             p2.Name = "广州";
    18             p2.Id = 2;
    19             Prov p3 = new Prov();
    20             p3.Name = "河南";
    21             p3.Id = 3;
    22             cob.Items.Add(p1);
    23             cob.Items.Add(p2);
    24             cob.Items.Add(p3);
    25             cob.DropDownStyle = ComboBoxStyle.DropDownList;
    26             cob.SelectedIndex = 0;
    27         }
    28 
    29         private void btnResult_Click(object sender, EventArgs e)
    30         {
    31             Prov p = (Prov)cob.SelectedItem;
    32             MessageBox.Show(p.Name + "---" + p.Id);
    33         }
    34     }
    35     
    36 }
    37 class Prov
    38     {
    39         private string name;
    40 
    41         public string Name
    42         {
    43             get { return name; }
    44             set { name = value; }
    45         }
    46         private int id;
    47 
    48         public int Id
    49         {
    50             get { return id; }
    51             set { id = value; }
    52         }
    53         public override string ToString()
    54         {
    55             return Name;
    56         }
    57     }
    将多个参数封装为一个对象

  • 相关阅读:
    11111 Generalized Matrioshkas
    Uva 442 Matrix Chain Multiplication
    Uva 10815 Andy's First Dictionary
    Uva 537 Artificial Intelligence?
    Uva 340 MasterMind Hints
    SCAU 9508 诸葛给我牌(水泥题)
    Uva 10420 List of Conquests(排序水题)
    Uva 409 Excuses, Excuses!
    10/26
    11/2
  • 原文地址:https://www.cnblogs.com/kongbei2013/p/3273391.html
Copyright © 2011-2022 走看看