P4 类、对象、类成员简介
本節內容
類(class)是顯示世界事物的模型。
現實中的一架飛機========================>>>抽象為程序世界中的類
類與對象的關系
- 有些類不能被實例化,如數學,我們不能說一個數學
- 現實世界中常稱對象,程序世界中常稱實例,二者并無太大區別,常常混用,不用太糾結。
△14:15開始編寫程序>>>接下來我們自己動手來編寫程序,創建一個實例。
?
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;namespace demo2 {class Program{static void Main(string[] args){Form myForm = new Form();myForm.ShowDialog();}} }?
4.引用變量與實例的關系
Form myForm(引用變量) = new Form()(實例);
引用變量相當于一個小孩,而實例是一個氣球。形象比喻成:一個小孩牽著一個氣球。
*如果氣球沒有牽著就會飛掉,實例會被垃圾回收給釋放掉。
下面我們來看另外一個例子:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;namespace demo2 {class Program{static void Main(string[] args){Form myForm1;Form myForm2;myForm1 = new Form();myForm2 = myForm1;myForm2.ShowDialog();}} }這種情況相當于是兩個孩子同時牽著一個氣球
常見的有下面三種情況
1.一個小孩牽著一個氣球。
2.一個小孩沒有牽氣球。
3.多個小孩同時牽著一個氣球。
?
類的三個成員
1.屬性(property)
*存儲數據,組合起來表示類或對象當前的狀態。
2.方法(method)
*由C語言的(function)進化而來,表示類能做什么。
*工作中90%的時間是在與方法打交道,因為他是類真正做事,構成邏輯的成員。
3.事件(event)!!!善用
*類或對象通知其他類或對象的機制,為C#特有。
*善用事件機制非常重要。
**F1鍵可以打開MSDN文檔
?
某些特殊類或對象在成員方面側重點不用
1.模型類或對象重在屬性:Entity,Framework。
2.工具類重點在方法:math,console。
3.通知類或對象重在事件:Time。
△42分鐘編寫數據庫
△50:36編寫WPF的time
接下來我們自己動手來編寫一個Time的例子吧。
代碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; 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 WPFTime {/// <summary>/// MainWindow.xaml 的交互邏輯/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();DispatcherTimer time = new DispatcherTimer();time.Interval = TimeSpan.FromSeconds(1);time.Tick += Time_Tick;time.Start();}private void Time_Tick(object sender, EventArgs e){this.timeTextBox.Text = DateTime.Now.ToString();//throw new NotImplementedException(); }} }界面代碼如下:
<Window x:Class="WPFTime.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:WPFTime"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><TextBox HorizontalAlignment="Left" Height="139" Margin="10,10,0,0" TextWrapping="Wrap" Name="timeTextBox" VerticalAlignment="Top" Width="774" FontSize="48"/></Grid> </Window>?
靜態成員與實例成員
*靜態(static)成員在語義上表示它是類的成員>>>與生俱來的,不需要實例化
*實例(非靜態)陳冠在語義上表示它是“對象成員”。
*綁定(binding)指的是編譯器如何把一個成員與類或對象關聯起來。
**不可小覷的 . (小數點)操作符
△59:02開始寫例子
靜態方法示例:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;namespace demo2 {class Program{static void Main(string[] args){Console.WriteLine("hello world!");}} }本節課結束。
?
轉載于:https://www.cnblogs.com/YiShen/p/9828348.html
總結
以上是生活随笔為你收集整理的P4 类、对象、类成员简介的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: bzoj3771:Triple
- 下一篇: 关于QueryRunner数据查询以及常