C beginner's course series 9: classes and objects
Gong dork 2020-11-26 13:25:34

This article is about C# Simple tutorial for beginners , This is the first 9 piece . Thank you for watching. , Remember to follow me , There are more tutorial articles to follow , thank you .

The environment of this paper is Visual Studio 2019.

One 、 Turn the real world into a class

Class is a code description of the real world , In other words, encapsulation . This is my own exclusive explanation , Copy what you don't have online .

Through encapsulation , We can use the method 、 Properties, etc , To describe a real world .

Like the family in life , We can express it as having a method “ Family member list ”, There are properties “ Family income ”, wait .

Class in C# Use in class To define , Its properties and methods, etc , We call them members of this class :
  • These properties are used to describe the data that the class will hold .
  • These methods tell you what you can do with the data .

Two 、 What is an object

An object is simply an instantiation of a class , stay C# in , By keyword new You can create an object .

When you create an object , Will use constructors , There is usually a default parameterless constructor . A constructor is a special member of a class .

C# Beginner's course series 9: Classes and objects



3、 ... and 、 stay VS How to create a class

open Visual Studio After creating a new project , You can right-click -> add to -> Create a class by creating a new item .

C# Beginner's course series 9: Classes and objects



?

C# Beginner's course series 9: Classes and objects



?

Pictured above , You can create a new class .

Four 、 How to write your own code logic in a class

After you have a class , With “ family ” For example , Take a look at how to express the real world , Write it to... In the code .

Our family may need a list of members , It could be a way , Of course, you can use attributes :

C# Beginner's course series 9: Classes and objects



?

And then our family , There may also be the attribute of family income :

C# Beginner's course series 9: Classes and objects



?

And then our family , Maybe it has its own name , It could be a static property :

C# Beginner's course series 9: Classes and objects



?

Last , We need to instantiate it :

C# Beginner's course series 9: Classes and objects



?

Have a good meal .
版权声明
本文为[Gong dork]所创,转载请带上原文链接,感谢
https://fheadline.com/2020/11/20201126132513391m.html
相似文章

2021-08-09