P4 类、对象、类成员简介
本節(jié)內(nèi)容
類(class)是顯示世界事物的模型。
現(xiàn)實中的一架飛機========================>>>抽象為程序世界中的類
類與對象的關(guān)系
- 有些類不能被實例化,如數(shù)學(xué),我們不能說一個數(shù)學(xué)
- 現(xiàn)實世界中常稱對象,程序世界中常稱實例,二者并無太大區(qū)別,常常混用,不用太糾結(jié)。
△14:15開始編寫程序>>>接下來我們自己動手來編寫程序,創(chuàng)建一個實例。
?
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.引用變量與實例的關(guān)系
Form myForm(引用變量) = new Form()(實例);
引用變量相當(dāng)于一個小孩,而實例是一個氣球。形象比喻成:一個小孩牽著一個氣球。
*如果氣球沒有牽著就會飛掉,實例會被垃圾回收給釋放掉。
下面我們來看另外一個例子:
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();}} }這種情況相當(dāng)于是兩個孩子同時牽著一個氣球
常見的有下面三種情況
1.一個小孩牽著一個氣球。
2.一個小孩沒有牽氣球。
3.多個小孩同時牽著一個氣球。
?
類的三個成員
1.屬性(property)
*存儲數(shù)據(jù),組合起來表示類或?qū)ο螽?dāng)前的狀態(tài)。
2.方法(method)
*由C語言的(function)進(jìn)化而來,表示類能做什么。
*工作中90%的時間是在與方法打交道,因為他是類真正做事,構(gòu)成邏輯的成員。
3.事件(event)!!!善用
*類或?qū)ο笸ㄖ渌惢驅(qū)ο蟮臋C制,為C#特有。
*善用事件機制非常重要。
**F1鍵可以打開MSDN文檔
?
某些特殊類或?qū)ο笤诔蓡T方面?zhèn)戎攸c不用
1.模型類或?qū)ο笾卦趯傩?#xff1a;Entity,Framework。
2.工具類重點在方法:math,console。
3.通知類或?qū)ο笾卦谑录?#xff1a;Time。
△42分鐘編寫數(shù)據(jù)庫
△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>?
靜態(tài)成員與實例成員
*靜態(tài)(static)成員在語義上表示它是類的成員>>>與生俱來的,不需要實例化
*實例(非靜態(tài))陳冠在語義上表示它是“對象成員”。
*綁定(binding)指的是編譯器如何把一個成員與類或?qū)ο箨P(guān)聯(lián)起來。
**不可小覷的 . (小數(shù)點)操作符
△59:02開始寫例子
靜態(tài)方法示例:
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!");}} }本節(jié)課結(jié)束。
?
轉(zhuǎn)載于:https://www.cnblogs.com/YiShen/p/9828348.html
總結(jié)
以上是生活随笔為你收集整理的P4 类、对象、类成员简介的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: bzoj3771:Triple
- 下一篇: 关于QueryRunner数据查询以及常