Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Uitdaging: Objecten | Introductie tot Objectgeoriënteerd Programmeren (OOP)
C# Verder dan de Basis

bookUitdaging: Objecten

Het volgende programma bevat twee klassen, namelijk Person en Address.

Lees de code en vul de lege plekken op de juiste manier in. In deze opdracht moet je uitzoeken hoe je toegang krijgt tot het veld Country van het adresobject in het address-veld van het p1-object.

index.cs

index.cs

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
using 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 {___}."); } }

Als een veld van een object een ander object opslaat, kunnen we verder gebruikmaken van de puntnotatie (.) om dieper in dat object te gaan. Bijvoorbeeld: objectName.someField.anotherField.

index.cs

index.cs

copy
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
using 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}."); } }
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 6

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Suggested prompts:

How do I access the Country field of the address object in p1?

Can you give an example of how to use dot notation for nested objects?

Can you explain how object fields work in this context?

Awesome!

Completion rate improved to 2.04

bookUitdaging: Objecten

Veeg om het menu te tonen

Het volgende programma bevat twee klassen, namelijk Person en Address.

Lees de code en vul de lege plekken op de juiste manier in. In deze opdracht moet je uitzoeken hoe je toegang krijgt tot het veld Country van het adresobject in het address-veld van het p1-object.

index.cs

index.cs

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
using 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 {___}."); } }

Als een veld van een object een ander object opslaat, kunnen we verder gebruikmaken van de puntnotatie (.) om dieper in dat object te gaan. Bijvoorbeeld: objectName.someField.anotherField.

index.cs

index.cs

copy
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
using 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}."); } }
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 6
some-alt