class Program { static void Main(string[ ] args) { Person David = new Person(); Person Alice = new Person(); // Initialize David David.age = 21; David.name = "David"; David.weight = 185.4; David.height = 72.3; // Initialize Alice Alice.age = 18; Alice.name = "Alice"; Alice.weight = 125.7; Alice.height = 67.1; // print some values Console.WriteLine("David’s age = {0}; David’s weight = {1}",David.age, David.weight); Console.WriteLine("Alice’s age = {0}; Alice’s weight = {1}", Alice.age, Alice.weight); } } Add this code to Program.cs. Compile and run it. Modify this code to print all of the instance variables of David and Alice. Run and understand the code. Add comments to record your understanding. Encapsulation in C#: In the previous example, we accessed the characteristics of the Person Class directly because the attributes (e.g. name, age, etc.) are defined as ‘public’. This is not a good practice and actually should be avoided in order to have ‘encapsulation’ – one of three most important features of object oriented design. The better option and practice is to define the attributes ‘private’ and then design some public methods to access them, normally ‘getXXX’ and ‘setXXX’. Amend the codes above accordingly. (Task 1.2)
时间: 2024-04-16 13:26:08 浏览: 120
class Person
{
private int age;
private string name;
private double weight;
private double height;
public int GetAge()
{
return age;
}
public void SetAge(int newAge)
{
age = newAge;
}
public string GetName()
{
return name;
}
public void SetName(string newName)
{
name = newName;
}
public double GetWeight()
{
return weight;
}
public void SetWeight(double newWeight)
{
weight = newWeight;
}
public double GetHeight()
{
return height;
}
public void SetHeight(double newHeight)
{
height = newHeight;
}
}
class Program
{
static void Main(string[] args)
{
Person David = new Person();
Person Alice = new Person();
// Initialize David
David.SetAge(21);
David.SetName("David");
David.SetWeight(185.4);
David.SetHeight(72.3);
// Initialize Alice
Alice.SetAge(18);
Alice.SetName("Alice");
Alice.SetWeight(125.7);
Alice.SetHeight(67.1);
// Print all instance variables of David
Console.WriteLine("David: age = {0}, name = {1}, weight = {2}, height = {3}", David.GetAge(), David.GetName(), David.GetWeight(), David.GetHeight());
// Print all instance variables of Alice
Console.WriteLine("Alice: age = {0}, name = {1}, weight = {2}, height = {3}", Alice.GetAge(), Alice.GetName(), Alice.GetWeight(), Alice.GetHeight());
}
}
In the modified code, the attributes (age, name, weight, height) of the Person class are defined as private. We have added public methods (getters and setters) to access and modify these attributes. By encapsulating the attributes, we achieve data hiding and provide controlled access to them.
The getters (e.g., GetAge, GetName, etc.) are used to retrieve the values of the private attributes, and the setters (e.g., SetAge, SetName, etc.) are used to set new values for the attributes.
In the Main method, we now use the setters to initialize the attributes for David and Alice. When printing the values, we use the getters to retrieve the attribute values.
Encapsulation is a fundamental concept in object-oriented programming that promotes data integrity and helps maintain code flexibility and reusability. By encapsulating the attributes, we can control access to them and enforce data validation or business logic if needed.
阅读全文