Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-01 02:00:44 09月21日 Update

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!

Tags: Member volume class function variable object learning content type facet encapsulation difference from design code prototype instance width that is properties Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Technology Apple Linux Shulou Tech Info