• P4 类、对象、类成员简介


    本节内容

    类(class)是显示世界事物的模型。

    现实中的一架飞机========================>>>抽象为程序世界中的类

    类与对象的关系

    1. 对象也叫做实例,是类经过实例化得到的内存中的事宜
      • 有些类不能被实例化,如数学,我们不能说一个数学
    2. 依照类,我们可以创建对象>>>这就是实例化
      • 现实世界中常称对象,程序世界中常称实例,二者并无太大区别,常常混用,不用太纠结。
    3. 使用new操作符创建对象

    △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!");
            }
        }
    }

    本节课结束。

  • 相关阅读:
    高级指令(三)
    高级指令(二)
    高级指令(一)
    高级指令(四)
    基础指令(二)
    02使用Maven构建Java项目
    03初识Maven核心概念(上)
    01Maven概述与安装
    关于sizeof与#pragma pack 以及网络上关于字节对齐的一点感想
    学习之"setjmp和longjmp函数"
  • 原文地址:https://www.cnblogs.com/YiShen/p/9828348.html
Copyright © 2020-2023  润新知