Utfordring: Objekter
Følgende program inneholder to klasser, nemlig Person
og Address
.
Les gjennom koden og fyll inn de tomme feltene på riktig måte. I denne oppgaven skal du finne ut hvordan du får tilgang til Country
-feltet i adresseobjektet som ligger i address
-feltet til p1
-objektet.
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 {___}."); } }
Hvis et felt i et objekt lagrer et annet objekt, kan vi bruke punktum (.
)-notasjon for å få tilgang til dypere nivåer i objektet. For eksempel objectName.someField.anotherField
.
index.cs
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950using 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"; p1.address = 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($"{p1.name} is the son of {p2.name} and {p3.name}. He is from {p1.address.Country}."); } }
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Awesome!
Completion rate improved to 2.04
Utfordring: Objekter
Sveip for å vise menyen
Følgende program inneholder to klasser, nemlig Person
og Address
.
Les gjennom koden og fyll inn de tomme feltene på riktig måte. I denne oppgaven skal du finne ut hvordan du får tilgang til Country
-feltet i adresseobjektet som ligger i address
-feltet til p1
-objektet.
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 {___}."); } }
Hvis et felt i et objekt lagrer et annet objekt, kan vi bruke punktum (.
)-notasjon for å få tilgang til dypere nivåer i objektet. For eksempel objectName.someField.anotherField
.
index.cs
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950using 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"; p1.address = 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($"{p1.name} is the son of {p2.name} and {p3.name}. He is from {p1.address.Country}."); } }
Takk for tilbakemeldingene dine!