I'm doing a modularity exercise in which I want to put in practice the encapsulation of data and methods.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Modularidad {
class Program {
static void Main(string[] args) {
Modulo modulo = new Modulo();
double resp = modulo.suma(3.5, 2.7);
Console.WriteLine("La suma es: " + resp);
Console.WriteLine("");
Modulo1 modulo1 = new Modulo1();
double pro = modulo1.multiplicacion(5.2, 8.3);
Console.WriteLine("La multiplicacion es: " + pro);
Console.WriteLine("");
Modulo2 Modulo2 = new Modulo2();
double resta = Modulo2.restar(4.3, 5.2);
Console.WriteLine("La resta es: " + resta);
Console.WriteLine("");
Modulo3 Modulo3 = new Modulo3();
double division = Modulo3.dividir(25, 5);
Console.WriteLine("La division es: " + division);
Console.WriteLine("");
persona persona = new persona();
string nombre = persona.nombre("Sasori");
Console.WriteLine("El nombre del usuario es: " + nombre);
Console.ReadKey();
}
}
class Modulo {
public double suma(double n1, double n2) {
double resultado = 0;
resultado = n1 + n2;
return resultado;
}
}
class Modulo1 {
public double multiplicacion (double n1, double n2) {
double producto = 0;
producto = n1 * n2;
return producto;
}
}
class Modulo2 {
public double restar(double n1, double n2) {
double resultado = 0;
resultado = n1 - n2;
return resultado;
}
}
class Modulo3 {
public double dividir(double n1, double n2) {
double division = 0;
division = n1 / n2;
return division;
}
}
class persona {
public string nombre(string entrada) {
string nombre;
nombre = entrada;
return entrada;
}
}
}
My question is: How do I implement the encapsulation of the fields (objects) to my example?
I know I can use the three levels of access (public, private y protected)
in the case of the "public" view if a method has this type of view, you can send it to the indicated method from any other class.
What I have not understood yet is
How to access an object with a "private" or "protected" view type?
since the object can not be accessed directly since (in the case of "private") the object can only interact within the class itself.
Investigating a bit uses properties for the fields in which "get" and "set" are used to assign and obtain values, but I would like to be able to apply all this to my example since I have not fully understood the functionality and Encapsulation application