|
| 1 | +// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +/// This example shows the use of super-initializer parameters. |
| 6 | +/// |
| 7 | +/// Super-initializer parameters allow to forward constructor parameters |
| 8 | +/// to the superclass, avoiding having to write the parameter multiple times |
| 9 | +/// in the superclass constructor invocation. |
| 10 | +
|
| 11 | +/// This example contains multiple classes representing different types of |
| 12 | +/// synthesizers, a musical instrument similar to a piano |
| 13 | +/// which can be used to create new sounds. |
| 14 | +/// |
| 15 | +/// All subclasses inherit from the [Synth] class and take advantage of the |
| 16 | +/// different super-initializer capabilities. |
| 17 | +class Synth { |
| 18 | + /// Model name |
| 19 | + final String model; |
| 20 | + |
| 21 | + /// Amount of notes that can be played at the same time |
| 22 | + final int polyphony; |
| 23 | + |
| 24 | + /// Amount of sound generators, default to 1 |
| 25 | + final int oscillators; |
| 26 | + |
| 27 | + /// In the class constructor [model] is a positional parameter, |
| 28 | + /// while [polyphony] and [oscillators] are named parameters. |
| 29 | + Synth( |
| 30 | + this.model, { |
| 31 | + required this.polyphony, |
| 32 | + this.oscillators = 1, |
| 33 | + }); |
| 34 | + |
| 35 | + @override |
| 36 | + String toString() { |
| 37 | + return 'Synth $model. Polyphony: $polyphony, oscillators: $oscillators'; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/// This class represents an old vintage synthesizer. |
| 42 | +/// |
| 43 | +/// [VintageSynth] can only have 1 oscillator. |
| 44 | +/// [polyphony] is optional and is 1 by default. |
| 45 | +class VintageSynth extends Synth { |
| 46 | + /// [model] is forwarded to the super constructor. |
| 47 | + /// Named parameter [polyphony] is forwarded, with the default value of 1. |
| 48 | + VintageSynth(super.model, {super.polyphony = 1}); |
| 49 | +} |
| 50 | + |
| 51 | +/// This class represents a modern digital synthesizer. |
| 52 | +/// |
| 53 | +/// [DigitalSynth] can only have 1 oscillator. |
| 54 | +/// Named parameter [polyphony] is required. |
| 55 | +class DigitalSynth extends Synth { |
| 56 | + /// [model] is forwarded to the super constructor. |
| 57 | + /// Named parameter [polyphony] is forwarded and it is required. |
| 58 | + DigitalSynth(super.model, {required super.polyphony}); |
| 59 | + |
| 60 | + /// The following constructor declaration would not be possible |
| 61 | + /// because [polyphony] is not a positional argument. |
| 62 | + /// |
| 63 | + /// DigitalSynth(super.model, super.polyphony); |
| 64 | +} |
| 65 | + |
| 66 | +/// This class represents a complex multi-oscillator synthesizer. |
| 67 | +/// |
| 68 | +/// [MultiOscillator] requires all three parameters. |
| 69 | +class MultiOscillatorSynth extends Synth { |
| 70 | + /// This constructor has three positional parameters instead of one. |
| 71 | + /// |
| 72 | + /// [model] is forwarded to the super constructor. |
| 73 | + /// [polyphony] and [oscillators] positional parameters are then passed to the |
| 74 | + /// named parameters in the super constructor. |
| 75 | + MultiOscillatorSynth( |
| 76 | + super.model, |
| 77 | + int polyphony, |
| 78 | + int oscillators, |
| 79 | + ) : super( |
| 80 | + polyphony: polyphony, |
| 81 | + oscillators: oscillators, |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | +/// This class represents a synth with a fixed amount |
| 86 | +/// of polyphony and oscillators. |
| 87 | +/// |
| 88 | +/// [FixedOscillatorSynth] only requires the positional parameter [model]. |
| 89 | +class FixedOscillatorSynth extends Synth { |
| 90 | + /// [model] is forwarded to the super constructor. |
| 91 | + /// [polyphony] and [oscillators] are hardcoded. |
| 92 | + FixedOscillatorSynth(super.model) |
| 93 | + : super( |
| 94 | + polyphony: 1, |
| 95 | + oscillators: 3, |
| 96 | + ); |
| 97 | +} |
0 commit comments