当前位置:网站首页>[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 .
边栏推荐
- Arfoundation starts from scratch 5-ar image tracking
- 2022 Teddy cup data mining challenge C project and post game summary
- Mathematical modeling clustering
- Squareline partners with visual GUI development of oneos graphical components
- 四元数与其在Unity中的简单应用
- File upload and expansion
- 数据表示与计算(进制)
- Network knowledge summary
- 2022年R2移动式压力容器充装考题模拟考试平台操作
- Could not receive a message from the daemon
猜你喜欢

LeetCode刷题(6)
[C language] DataGridView binding data
数据表示与计算(进制)

2022年P气瓶充装考试模拟100题模拟考试平台操作

6.3 references

Information system project manager must recite the quality grade of the core examination site (53)

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

Excellent package volume optimization tutorial

C language function output I love you

Leetcode deduction topic summary (topic No.: 53, 3, 141, interview question 022, the entry node of the link in the sword finger offer chain, 20, 19, Niuke NC1, 103, 1143, Niuke 127)
随机推荐
ICMP message analysis
AI application lesson 1: C language Alipay face brushing login
Is the sub database and sub table really suitable for your system? Talk about how to select sub databases, sub tables and newsql
Could not receive a message from the daemon
MySQL 错误总结
Cmake setting vs Startup running environment path
Amazfit dial Kit
Reptile practice (10): send daily news
Sword finger offer 50. the first character that appears only once
What are the backup and recovery methods of gbase 8s database
01 knapsack about from two-dimensional optimization to one-dimensional optimization
C language macro define command exercise
ERROR 1045 (28000): Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)
The gold content of PMP certificate has been increased again and included in the scope of Beijing work residence permit
Sublime text create page
文件上传及拓展
Excellent Allegro skill recommendation
Leetcode question brushing (6)
2022电工(初级)考题模拟考试平台操作
Several ways of debugging support under oneos