Voeg een tweede constructor toe die een extra parameter accepteert en gebruik constructor chaining.
Specificaties
Breid te tweede constructor van de class Auto zo aan dat deze de eerste constructor aanroept
De tweede constructor roept de eerste constructor aan met behulp van this.
De waarde van model wordt ook meegegeven en op de waarde Unknown gezet.
Verwachte output
Een auto van het merk: Opel, model: Unknown
Nu jij
using System;Auto mijnAuto = new Auto("Opel");Console.WriteLine($"Een auto van het merk: {mijnAuto.Merk}, model: {mijnAuto.Model}");public class Auto{ public string Merk { get; set; } public string Model { get; set; } public Auto(string merk, string model) { Merk = merk; Model = model; } public Auto(string merk) : ________ { } }
Mogelijke uitwerking
using System;Auto mijnAuto = new Auto("Opel");Console.WriteLine($"Een auto van het merk: {mijnAuto.Merk}, model: {mijnAuto.Model}");public class Auto{ public string Merk { get; set; } public string Model { get; set; } public Auto(string merk, string model) { Merk = merk; Model = model; } public Auto(string merk) : this(merk, "Unknown") { } }
Opdracht 2
Gebruik constructor chaining om een klasse te maken die meerdere parameters accepteert.
Specificaties
Pas de class Glass zo aan dat de deze 3 constructors heeft
Een constructor die alleen de parameter volume accepteert.
Een constructor die de volume en color accepteert.
Een constructor die de volume, color, en contents accepteert en gebruik maakt van constructor chaining.
De default waarde voor color is "Blue" en de default waarde voor contents is "Air"
De default waardes mogen maar 1x in de code voorkomen.
Verwachte output
console:
cup1 is Blue and contains 42cc of Air
cup2 is Red and contains 10cc of Water
Nu jij
using System;Glass cup1 = new Glass(42);Glass cup2 = new Glass(10, "Red", "Water");Console.WriteLine($"cup1 is {cup1.Color} and contains {cup1.Volume}cc of {cup1.Contents}");Console.WriteLine($"cup2 is {cup2.Color} and contains {cup2.Volume}cc of {cup2.Contents}");public class Glass{ public int Volume { get; set; } public string Color{ get; set; } public string Contents { get; set; } public Glass(int volume, string color, string contents) { this.Volume = volume; this.Color = color; this.Contents = contents; } public Glass(int volume, string color) ________ { } public Glass(int volume) ________ { } }
Mogelijke uitwerking
using System;Glass cup1 = new Glass(42);Glass cup2 = new Glass(10, "Red", "Water");Console.WriteLine($"cup1 is {cup1.Color} and contains {cup1.Volume}cc of {cup1.Contents}");Console.WriteLine($"cup2 is {cup2.Color} and contains {cup2.Volume}cc of {cup2.Contents}");public class Glass{ public int Volume { get; set; } public string Color{ get; set; } public string Contents { get; set; } public Glass(int volume, string color, string contents) { this.Volume = volume; this.Color = color; this.Contents = contents; } public Glass(int volume, string color) : this(volume, color, "Air") { } public Glass(int volume) : this(volume, "Blue") { } }
Opdracht 3
Gebruik constructor chaining om een klasse te maken die meerdere parameters accepteert.
Specificaties
Pas de class Bucket zo aan dat de deze 3 constructors heeft
Een constructor die de volume en material accepteert.
Een constructor die alleen de parameter volume accepteert.
Een constructor die alleen de parameter material accepteert.
De default waarde voor volume is 30 en de default waarde voor material is "Iron"
De default waardes mogen maar 1x in de code voorkomen.
Verwachte output
console:
bucket1 is made of Iron and contains 30 liters
bucket2 is made of Wood and contains 12 liters
Nu jij
using System;Bucket bucket1 = new Bucket(30);Bucket bucket2 = new Bucket("Wood");Console.WriteLine($"bucket1 is made of {bucket1.Material} and contains {bucket1.Volume} liters");Console.WriteLine($"bucket2 is made of {bucket2.Material} and contains {bucket2.Volume} liters");public class Bucket{ public int Volume { get; set; } public string Material{ get; set; } public Bucket(int volume, string material) { this.Volume = volume; this.Material= material; } //constructor with only material ________ //constructor with only volume ________}
Mogelijke uitwerking
using System;Bucket bucket1 = new Bucket(30);Bucket bucket2 = new Bucket("Wood");Console.WriteLine($"bucket1 is made of {bucket1.Material} and contains {bucket1.Volume} liters");Console.WriteLine($"bucket2 is made of {bucket2.Material} and contains {bucket2.Volume} liters");public class Bucket{ public int Volume { get; set; } public string Material{ get; set; } public Bucket(int volume, string material) { this.Volume = volume; this.Material= material; } //constructor with only material public Bucket(string material) : this(12, material) { } //constructor with only volume public Bucket(int volume) : this(volume, "Iron") { } }