Junior — Middle
What are the main ways of interacting with objects in programming?
sobes.tech AI
Answer from AI
The main ways of interacting with objects in programming include:
- Calling methods — objects provide methods that can be invoked to perform actions or retrieve data.
- Accessing properties — reading or modifying the values of an object's properties.
- Message passing — a concept from object-oriented programming where an object receives a message (method call) and responds to it.
- Using interfaces — interacting through predefined contracts, allowing work with objects abstractly.
Example in C#:
class Person {
public string Name { get; set; }
public void SayHello() {
Console.WriteLine($"Hello, my name is {Name}");
}
}
var person = new Person { Name = "Alex" };
person.SayHello(); // method call
string name = person.Name; // access to property
The main ways of interacting with objects in programming include:
- Calling methods — objects provide methods that can be invoked to perform actions or retrieve data.
- Accessing properties — reading or modifying the values of an object's properties.
- Message passing — a concept from object-oriented programming where an object receives a message (method call) and responds to it.
- Using interfaces — interacting through predefined contracts, allowing work with objects abstractly.
Example in C#:
class Person {
public string Name { get; set; }
public void SayHello() {
Console.WriteLine($"Hello, my name is {Name}");
}
}
var person = new Person { Name = "Alex" };
person.SayHello(); // method call
string name = person.Name; // access to property