- 类(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-variablemyForm.ShowDialog();
- 气球不一定有孩子牵
(new Form()).ShowDialog();
- 多个孩子用各自绳子牵一个气球
Form myForm1, myForm2;myForm1 = new Form();myForm2 = myForm1;myForm1.Text = "My Form";myForm2.Text = "I Change It"myForm1.ShowDialog();myForm2.ShowDialog();
- 多个孩子用一根绳子牵一个气球- **ref修饰符**
- 类的三大成员
- 属性(Property)
- 存储数据,表示状态的成员
- 方法(Method)
- 类似于C语言中的Function
- 构成逻辑的成员
- TIP:程序=数据+算法
- 事件(Event)
- 类或对象通知其他类或对象的机制
- 容易滥用,非必需品
- 使用MSDN Document
- 某些特殊类或对象在成员方面侧重点不同
- 模型类或对象侧重于属性, example : Entity Framework
- 工具类或对象侧重于方法, example : Method, Console
- 属性(Property)
double x = Convert.ToDouble(Console.ReadLine());double result_sqrt = Math.Sqrt((double)x);Console.WriteLine($"{x} sqrt is {result_sqrt}");
- **通知类或对象****侧重于事件**, example : Timer
<Window x:Class="Wtf_Test.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="MainWindow" Height="350" Width="525"><Grid><TextBox x:Name="Time_TextBox" Height="60" HorizontalAlignment="Left" FontSize="40" Margin="43,51,0,0" VerticalAlignment="Top" Width="419" Background="#FFE5BABA" /></Grid></Window>
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;using System.Windows.Threading;namespace Wtf_Test{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();DispatcherTimer timer = new DispatcherTimer();timer.Interval = TimeSpan.FromSeconds(1);timer.Tick += new EventHandler(timer_Tick);timer.Start();}void timer_Tick(object sender, EventArgs e){this.Time_TextBox.Text = DateTime.UtcNow.ToString();}}}
- 类的静态成员与实例成员
- 静态成员
- 类的成员
- 实例成员
- 对象的成员
- 关于绑定(Binding)
- 编译器将成员与类或对象关联起来
- 早绑定 : 静态绑定,程序编译时就绑定 example : C++语言
- 晚绑定 : 动态绑定,函数调用时才绑定 example : Pyton语言
- “.”操作符
- 成员访问操作符
- 编译器将成员与类或对象关联起来
- 静态成员
