The content of this sectionA class is a model that shows things in the world.
A plane in reality ========================>>> abstract as a class in the program world
class-to-object relationships
- An object is also called an instance, and is a matter of memory in which the class is instantiated
- Some classes cannot be instantiated, such as math, we can't say a math
- According to the class, we can create objects >>> this is the instantiation of
- The real world is often called objects, the program world is often called instances, the two are not very different, often mixed, not too tangled.
- Creating an object using the new operator
14:15 started writing programs >>> then we wrote the program ourselves to create an instance.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespacedemo2{classProgram {Static voidMain (string[] args) {Form MyForm=NewForm (); Myform.showdialog (); } }}
4. Relation of reference variables to instances
Form myForm (reference variable) = new form () (instance);
The reference variable is equivalent to a child, and the instance is a balloon. Image analogy: A child holding a balloon.
* If the balloon does not hold, it will fly away and the instance will be released by garbage collection.
Let's look at another example:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespacedemo2{classProgram {Static voidMain (string[] args) {Form MyForm1; Form MyForm2; MyForm1=NewForm (); MyForm2=MyForm1; Myform2.showdialog (); } }}
This is the equivalent of two children holding a balloon at the same time.
There are three common cases.
1. A child holding a balloon.
2. A child did not hold a balloon.
3. Many children hold a balloon at the same time.
Three members of a class
1. Attribute (property)
* Store data, grouped together to represent the current state of a class or object.
2. Methods (method)
* Evolved from the C language (function) to indicate what the class can do.
* 90% of the time at work is in dealing with methods because he is a member of a class that really does things and makes up logic.
3. event!!! Use
* The mechanism for classes or objects to notify other classes or objects is unique to C #.
* The use of the incident mechanism is very important.
**F1 key to open the MSDN documentation
Some special classes or objects do not focus on members
1. The model class or object is heavy on the property: Entity,framework.
2. Tool class focus on method: Math,console.
3. Notifies the class or object that the event is heavy: time.
42 minutes to write a database
50:36 writing WPF for Time
Now let's do it ourselves and write a time example.
The code is as follows:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;usingSystem.Windows.Input;usingSystem.Windows.Media;usingSystem.Windows.Media.Imaging;usingSystem.Windows.Navigation;usingSystem.Windows.Shapes;usingSystem.Windows.Threading;namespacewpftime{/// <summary> ///the interactive logic of MainWindow.xaml/// </summary> Public Partial classMainwindow:window { PublicMainWindow () {InitializeComponent (); DispatcherTimer Time=NewDispatcherTimer (); Time. Interval= Timespan.fromseconds (1); Time. Tick+=Time_tick; Time. Start (); } Private voidTime_tick (Objectsender, EventArgs e) { This. Timetextbox.text =DateTime.Now.ToString (); //throw new NotImplementedException (); } }}
The interface code is as follows:
<Windowx: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= "The "Width= "$"> <Grid> <TextBoxHorizontalAlignment= "Left"Height= "139"Margin= "10,10,0,0"textwrapping= "Wrap"Name= "Timetextbox"VerticalAlignment= "Top"Width= "774"FontSize= "$"/> </Grid></Window>
Static members and instance members
* Static member semantically indicates that it is a member of the class >>> is innate and does not need to be instantiated
* Instance (non-static) Chen semantically indicates that it is an "object member."
* Binding refers to how the compiler associates a member with a class or object.
* * not to be underestimated. (decimal) operator
59:02 began to write an example
static method Examples:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespacedemo2{classProgram {Static voidMain (string[] args) {Console.WriteLine ("Hello world!"); } }}
This lesson concludes.
Introduction to P4 classes, objects, class members