当前位置:网站首页>[Tang Laoshi] C -- encapsulation: member variables and access modifiers
[Tang Laoshi] C -- encapsulation: member variables and access modifiers
2022-07-03 20:24:00 【Tang laoshiyi】
Last article review
Class is a template for an object
Used to abstract real objects

Object is an instance of a class
Used to represent a real object individual

Class is a custom variable
Instantiate class objects
Is declaring variables
Today we will study in the future
Characteristics of the object
Member variables
【 Member variables 】
Object oriented programming idea , Simply put, it is abstracted with program language ( Describe ) A real object appears . Now we know that classes are used to describe objects , Objects are instantiated from classes . Specific The characteristics and behavior of objects stay Class representation Mainly through : Member variables and member methods Embodied .
Member variables
↓↓↓↓
Declare variables in classes
↓↓↓↓
Write in a class statement block

Basic rules for member variables
1. Declared in a class statement block
2. Whether the assignment is determined according to the requirements
3. It is used to describe the characteristics of objects
4. It can be of any kind
5. There is no limit to the quantity
First of all, I want to emphasize a point , Class is also a variable type , It's just a custom variable type . It and the structure 、 Enumerations are very similar , It is a variable rule that we can name and define ourselves . So, in the above example, in Person Owned by Person Variables are fully allowed ! You need to understand its concept with object-oriented thinking , Just like you are human , You also have to have a human girlfriend ~
How to use these member variables ?
First we must learn the access modifier
↓↓↓↓
【 Access modifier 】
Access modifier
Current decision
Member variables
Member method
Access rights of
keyword
public—— public
private—— private
protected—— Protect

The access modifier is written before the member variable declaration
Determines the access rights of member variables

1. Public member variables , You can point it out
2. Private member variables , Can only be used inside a class
3. Protected member variables , When we inherit
public
Public member variables
Inside and outside the class
at will Access modification
private
Private member variables
Can only Inside the class
at will Access modification
protected
Explain when inheriting
When declaring member variables
Do not write access modifiers
The default is private
【 Use member variables 】


【 summary 】
Access modifier
Determine access to member variables
public—— public
private—— private
Member variables
Describe the characteristics
Class
The assignment is arbitrary
Any type
Any number
Focus on Tang Laoshi Get more dry content

边栏推荐
- How to modify the network IP addresses of mobile phones and computers?
- 2166. Design bit set
- Cesiumjs 2022 ^ source code interpretation [7] - Analysis of the request and loading process of 3dfiles
- Thread, thread stack, method stack, the difference of creating thread
- Line segment tree blue book explanation + classic example acwing 1275 Maximum number
- In 2021, the global general crop protection revenue was about $52750 million, and it is expected to reach $64730 million in 2028
- 6006. Take out the minimum number of magic beans
- [Yugong series] February 2022 Net architecture class 004 ABP vNext used in WPF project
- Global and Chinese market of rubidium standard 2022-2028: Research Report on technology, participants, trends, market size and share
- [raid] [simple DP] mine excavation
猜你喜欢

Line segment tree blue book explanation + classic example acwing 1275 Maximum number

Preliminary practice of niuke.com (11)

Detailed and not wordy. Share the win10 tutorial of computer reinstallation system

JMeter plug-in installation

AcWing 1460. Where am i?

The global industrial design revenue in 2021 was about $44360 million, and it is expected to reach $62720 million in 2028. From 2022 to 2028, the CAGR was 5.5%

Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of rotary tablet presses in the global market in 2022

Test panghu was teaching you how to use the technical code to flirt with girls online on Valentine's Day 520

Typora charges, WTF? Still need support

2.4 conversion of different data types
随机推荐
An old programmer gave it to college students
Sparse matrix (triple) creation, transpose, traversal, addition, subtraction, multiplication. C implementation
Implementation of stack
2166. Design bit set
Global and Chinese market of rubidium standard 2022-2028: Research Report on technology, participants, trends, market size and share
[Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University
How to handle wechat circle of friends marketing activities and share production and release skills
Qtablewidget control of QT
QT tutorial: signal and slot mechanism
3. Data binding
jvm jni 及 pvm pybind11 大批量数据传输及优化
6006. Take out the minimum number of magic beans
Global and Chinese market of cyanuric acid 2022-2028: Research Report on technology, participants, trends, market size and share
The global industrial design revenue in 2021 was about $44360 million, and it is expected to reach $62720 million in 2028. From 2022 to 2028, the CAGR was 5.5%
Typora charges, WTF? Still need support
Find a line in a file and remove it
Acquisition and transmission of parameters in automatic testing of JMeter interface
First knowledge of database
Global and Chinese market of full authority digital engine control (FADEC) 2022-2028: Research Report on technology, participants, trends, market size and share
Camera calibration (I): robot hand eye calibration