Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does C # get the values of different class members in a class

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how C # gets the value of different class members in a class". The explanation in this article is simple and clear and easy to learn and understand. let's go deep into the editor's train of thought. Let's study and learn "how to get the value of different class members in a class"!

Member functions and encapsulation

A member function of a class is a function that has its definition or prototype in the class definition, just like other variables. As a member of a class, it can operate on any object of the class and can access all members of that object's class.

Member variables are properties of the object (from a design perspective), and they remain private for encapsulation. These variables can only be accessed using public member functions.

Let's use the above concept to set and get the values of different class members in a class:

Example

Using System

Namespace BoxApplication

{

Class Box

{

Private double length; / / length

Private double breadth; / / width

Private double height; / / height

Public void setLength (double len)

{

Length = len

}

Public void setBreadth (double bre)

{

Breadth = bre

}

Public void setHeight (double hei)

{

Height = hei

}

Public double getVolume ()

{

Return length * breadth * height

}

}

Class Boxtester

{

Static void Main (string [] args)

{

Box Box1 = new Box (); / / declare Box1 with type Box

Box Box2 = new Box (); / / declare Box2 with type Box

Double volume; / / Volume

/ / Box1 details

Box1.setLength (6.0)

Box1.setBreadth (7. 0)

Box1.setHeight (5.0)

/ / Box2 details

Box2.setLength (12.0)

Box2.setBreadth (13.0)

Box2.setHeight (10.0)

/ / Volume of Box1

Volume = Box1.getVolume ()

Console.WriteLine ("volume of Box1: {0}", volume)

/ / Volume of Box2

Volume = Box2.getVolume ()

Console.WriteLine ("volume of Box2: {0}", volume)

Console.ReadKey ()

}

}

}

When the above code is compiled and executed, it produces the following results:

Volume of Box1: volume of 210Box2: 1560 Thank you for your reading, this is the content of "how to get the value of different class members in a class". After the study of this article, I believe you have a deeper understanding of how to get the value of different class members in a class, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report