• 类(class)是现实世界事物的模型
      • 类是对现实世界事物抽象所得的
        • 事物(类)是由“物质”(实体)与“运动”(逻辑)组成
        • 建模是一个去伪存真,由表及里的过程
    • 类与对象的关系
      • 什么时候叫“对象”,什么时候叫“实例”
        • 对象=实例,类经过实例化在内存中得到的实体
        • 现实世界称为对象,程序世界称为实例
        • 有些类不能实例化
      • 使用new操作符创建类的实例

    (new Form()).ShowDialog();//``"()" of "Form()" is ctor Class create(new) a entity in the memory

    • 引用变量与实例的关系
      • 孩子与气球

    Form myForm;//``Use Form-Class declare a variable
    **myForm = new Form()**``;//``Assign Form-entity to myForm-variable
    myForm.ShowDialog();

    1. - 气球不一定有孩子牵

    (new Form()).ShowDialog();

    1. - 多个孩子用各自绳子牵一个气球

    Form myForm1, myForm2;
    myForm1 = new Form();
    myForm2 = myForm1;
    myForm1.Text = "My Form";
    myForm2.Text = "I Change It"
    myForm1.ShowDialog();
    myForm2.ShowDialog();

    1. - 多个孩子用一根绳子牵一个气球
    2. - **ref修饰符**
    • 类的三大成员
      • 属性(Property)
        • 存储数据,表示状态的成员
      • 方法(Method)
        • 类似于C语言中的Function
        • 构成逻辑的成员
        • TIP:程序=数据+算法
      • 事件(Event)
        • 类或对象通知其他类或对象的机制
        • 容易滥用,非必需品
      • 使用MSDN Document
      • 某些特殊类或对象在成员方面侧重点不同
        • 模型类或对象侧重于属性, example : Entity Framework
        • 工具类或对象侧重于方法, example : Method, Console

    double x = Convert.ToDouble(Console.ReadLine());
    double result_sqrt = Math.Sqrt((double)x);
    Console.WriteLine($"{x} sqrt is {result_sqrt}");

    1. - **通知类或对象****侧重于事件**, example : Timer
    1. <Window x:Class="Wtf_Test.MainWindow"
    2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    4. Title="MainWindow" Height="350" Width="525">
    5. <Grid>
    6. <TextBox x:Name="Time_TextBox" Height="60" HorizontalAlignment="Left" FontSize="40" Margin="43,51,0,0" VerticalAlignment="Top" Width="419" Background="#FFE5BABA" />
    7. </Grid>
    8. </Window>
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Windows;
    6. using System.Windows.Controls;
    7. using System.Windows.Data;
    8. using System.Windows.Documents;
    9. using System.Windows.Input;
    10. using System.Windows.Media;
    11. using System.Windows.Media.Imaging;
    12. using System.Windows.Navigation;
    13. using System.Windows.Shapes;
    14. using System.Windows.Threading;
    15. namespace Wtf_Test
    16. {
    17. /// <summary>
    18. /// MainWindow.xaml 的交互逻辑
    19. /// </summary>
    20. public partial class MainWindow : Window
    21. {
    22. public MainWindow()
    23. {
    24. InitializeComponent();
    25. DispatcherTimer timer = new DispatcherTimer();
    26. timer.Interval = TimeSpan.FromSeconds(1);
    27. timer.Tick += new EventHandler(timer_Tick);
    28. timer.Start();
    29. }
    30. void timer_Tick(object sender, EventArgs e)
    31. {
    32. this.Time_TextBox.Text = DateTime.UtcNow.ToString();
    33. }
    34. }
    35. }
    • 类的静态成员与实例成员
      • 静态成员
        • 类的成员
      • 实例成员
        • 对象的成员
      • 关于绑定(Binding)
        • 编译器将成员与类或对象关联起来
          • 早绑定 : 静态绑定,程序编译时就绑定 example : C++语言
          • 晚绑定 : 动态绑定,函数调用时才绑定 example : Pyton语言
        • “.”操作符
          • 成员访问操作符