Praxis: Objects
Das folgende Programm enthält zwei Klassen, nämlich Person
und Address
.
Lies den Code und fülle die Lücken entsprechend aus. In dieser Aufgabe musst du herausfinden, wie du auf das Country
-Feld des Adressobjekts im address
-Feld des p1
-Objekts zugreifen kannst.
index.cs
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051using System; class Person { public string name; public int age; // We can use other classes as datatypes for a field in a class public Address address; // We can use the class itself as a datatype for a field as well public Person father; public Person mother; } class Address { public string Country; public string City; } public class ConsoleApp { public static void Main(string[] args) { Person p1 = new Person(); p1.name = "Mihaly"; p1.age = 21; Person p2 = new Person(); p2.name = "Ann"; p2.age = 52; Person p3 = new Person(); p3.name = "Nagy"; p3.age = 51; p1.mother = p2; p1.father = p3; Address address = new Address(); address.Country = "Hungary"; address.City = "Budapest"; // Assigning the address object to the address field in p1 p1.address = ___; // Note the expression 'p1.address.Country' // p1.address accesses the stored Address object // p1.address.Country accesses the Address object's Country field. Console.WriteLine($"{___} is the son of {p2.name} and {p3.name}. He is from {___}."); } }
Danke für Ihr Feedback!
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Can you explain how dot notation works for nested objects?
What would happen if the address field was null?
Can you show how to access the City field in the same way?
Awesome!
Completion rate improved to 2.04
Praxis: Objects
Swipe um das Menü anzuzeigen
Das folgende Programm enthält zwei Klassen, nämlich Person
und Address
.
Lies den Code und fülle die Lücken entsprechend aus. In dieser Aufgabe musst du herausfinden, wie du auf das Country
-Feld des Adressobjekts im address
-Feld des p1
-Objekts zugreifen kannst.
index.cs
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051using System; class Person { public string name; public int age; // We can use other classes as datatypes for a field in a class public Address address; // We can use the class itself as a datatype for a field as well public Person father; public Person mother; } class Address { public string Country; public string City; } public class ConsoleApp { public static void Main(string[] args) { Person p1 = new Person(); p1.name = "Mihaly"; p1.age = 21; Person p2 = new Person(); p2.name = "Ann"; p2.age = 52; Person p3 = new Person(); p3.name = "Nagy"; p3.age = 51; p1.mother = p2; p1.father = p3; Address address = new Address(); address.Country = "Hungary"; address.City = "Budapest"; // Assigning the address object to the address field in p1 p1.address = ___; // Note the expression 'p1.address.Country' // p1.address accesses the stored Address object // p1.address.Country accesses the Address object's Country field. Console.WriteLine($"{___} is the son of {p2.name} and {p3.name}. He is from {___}."); } }
Danke für Ihr Feedback!