当前位置:网站首页>[unity entry program] C # and unity - understand classes and objects
[unity entry program] C # and unity - understand classes and objects
2022-07-29 09:01:00 【flashinggg】
Catalog
1 The concept of object-oriented programming
2 C# Three characteristics of object-oriented
5.2 Visualization of class objects in the attribute window
Because of study C# Is for Unity To prepare for , Therefore, only simple learning records are made for classes and objects .
1 The concept of object-oriented programming
As long as you touch C# I must have heard this sentence “C# Is an object-oriented programming language ”, So what is object-oriented programming ?
object-oriented programming ——OOP(Object-Oriented Programming), It refers to describing everything as an object in programming , Because of this , Object oriented programming is more readable , It is also easier to understand , That's why I say with C++ comparison C# It will be easier to learn .
2 C# Three characteristics of object-oriented
2.1 encapsulation
encapsulation , That is, one or more projects are enclosed in a package , Hide the details of the internal state and function of the object , Provide some public elements for access . Let's take the mobile phone as an example : Some operations can be carried out through some keys of the mobile phone , But we don't know how the internal hardware of the mobile phone realizes these operations . Encapsulation allows users not to care about how objects are implemented , Just know the usage of the object .
abstract And encapsulation are often mentioned together , After realizing encapsulation , Some abstract public elements provided —— class 、 Methods to realize the visualization of information , However, the access rights of users to these information are not all public .
Access modifier
C# Package utilization Access modifier To set user access rights ,C# There are the following access modifiers ( If there is no limit , The default is private)
- public—— public , Members of a class can be accessed from outside the class
- private—— Can only be accessed within a class
- protected—— Only this kind of object and its children can access
- internal—— Objects in the same assembly can access
- prtected internal—— This category 、 Derived classes or assemblies containing them are accessible
2.2 Inherit
C# The inheritance of is mainly reflected in the inheritance between classes , Through inheritance, we can define another class according to one class , Improve code reuse . For example, in the following code
public class MyPlayer : MonoBehaviour
{
...
}Created a new class MyPlayer, It inherited Unity Script API Basic classes in MonoBehaviour, That means inheriting the data members and member functions of this basic class , There is no need to write repeatedly .
In this relationship ,MyPlayer be called Derived class ,MonoBehaviour be called Base class .
2.3 polymorphic
The same behavior has different representation forms , Or implement inherited properties or methods in different ways , This is called polymorphism . stay C# Polymorphism is embodied by class inheritance or interface .
3 object
Briefly understand what is an object : Objects include properties and methods
- attribute —— Inherent properties of objects
- Method —— Behavior methods of objects
Suppose we have a mobile phone object , Then the style of mobile phone 、 The model is its attribute , And make phone calls with mobile phones 、 Texting and other operations are its methods .
4 class
C# Everything in is related to classes and objects and their properties and methods , When you define a class , It defines a blueprint for data types . for instance : A student is a “ class ”, And a student named Xiao Ming , He became a “ object ” 了 , Class is more like a concept , The object is the class Instantiation Become an individual with specific information .
Static member of class
Class members are divided into instance members and static members .
Static members —— have access to static Keyword modifies class members to static .
When a member is declared static , No matter how many class members are created , They will only have a copy of the static member . Static variables are used to define constants in classes , Without creating an instance, you can directly call the class to get , It can be defined externally or internally .
5 class &Unity Components
When using static methods or properties of classes , There is no need to instantiate the class , In addition, all C# Classes in must be instantiated into objects before they can be used .
5.1 Class in Unity Use
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class MyPlayer : MonoBehaviour
{
public string myName;
public Cat cat;
void Start()
{
cat.Print();
}
}
[Serializable]
public class Cat
{
// Data member
public string name;
public int age;
/*// Constructors
public Cat(string name, int age)
{
this.name = name;
this.age = age;
}*/
// Member method
public void Print()
{
Debug.Log($" The kitten's name is {name}, It's this year {age} Year old ");
}
}
Input name and age after ,ctrl+p Results of operation

5.2 Visualization of class objects in the attribute window
stay Unity in , We all know that if you want to visualize a defined variable , add public that will do , for example

But to visualize the class objects defined in the script , Add only public It's not enough. , You also need to add

[Seralizable] It's in the namespace System Under the


such , The object created by this class plus public You can visualize it in the attribute window .
边栏推荐
- Restful style details
- ERROR 1045 (28000): Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)
- How to quickly experience oneos
- Gutcloud technology restcloud completed the pre-A round of financing of tens of millions of yuan
- ML.NET相关资源整理
- Network knowledge summary
- Design of distributed (cluster) file system
- What is the key to fast multi tag user profile analysis?
- 数据表示与计算(进制)
- QT learning: use non TS files such as json/xml to realize multilingual internationalization
猜你喜欢

Leetcode:132. split palindrome string II

2022电工(初级)考题模拟考试平台操作

2022.7.9 quick view of papers

2022 R2 mobile pressure vessel filling test question simulation test platform operation

Summary of some experiences in the process of R & D platform splitting

Arfoundation starts from scratch 5-ar image tracking

The gold content of PMP certificate has been increased again and included in the scope of Beijing work residence permit

Data is the main body of future world development, and data security should be raised to the national strategic level

Emmet syntax

Tesseract text recognition -- simple
随机推荐
信息系统项目管理师必背核心考点(五十三)质量等级
Intel will gradually end the optane storage business and will not develop new products in the future
Redis series 3: highly available master-slave architecture
What are the backup and recovery methods of gbase 8s database
C language watch second kill assist repeatedly
Jetpack Glance? The spring of widgets is coming
数学建模——聚类
Design of distributed (cluster) file system
GBase 8s数据库有哪些备份恢复方式
2022 electrician (elementary) test question simulation test platform operation
Restful style details
Leetcode question brushing (6)
Excellent package volume optimization tutorial
The gold content of PMP certificate has been increased again and included in the scope of Beijing work residence permit
7.2-function-overloading
[[first blog]p controller implementation instructions in UDA course]
OSG advanced sequence
1.2.24 fastjson deserialization templatesimpl uses chain analysis (very detailed)
Tesseract图文识别--简单
Travel notes in 2022 (ongoing)