class1 as property of class2, class1 property initialization?
Vous suivez désormais cette question
- Les mises à jour seront visibles dans votre flux de contenu suivi.
- Selon vos préférences en matière de communication il est possible que vous receviez des e-mails.
Une erreur s'est produite
Impossible de terminer l’action en raison de modifications de la page. Rechargez la page pour voir sa mise à jour.
0 votes
Partagez un lien vers cette question
Réponse acceptée
0 votes
Partagez un lien vers cette réponse
10 commentaires
Partagez un lien vers ce commentaire
Partagez un lien vers ce commentaire
Partagez un lien vers ce commentaire
Partagez un lien vers ce commentaire
Partagez un lien vers ce commentaire
Partagez un lien vers ce commentaire
Partagez un lien vers ce commentaire
- By removing the validation from the property itself, you eliminate the need to define a default value, and MATLAB won't attempt to populate it with a scalar instance of your class.
- By making the property Immutable the user won't be able to change the value after the constructor is called, so you don't need the validation on the property itself any more anyway.
- By making the property Immutable it also follows your stated behavior, which is that MyClassA depends on a specific instance of MyClassB, so it can be set once and never changed.
- Using Function Argument Validation in the MyClassA constructor allows you to basically reuse the same property validation you have now, but within an arguments block, to guarantee that the user provided value is always valid.
- By requiring the property to be set in the constructor, you can still rely on the value of the property always being valid (with the possible exception of save/load, which might need to be handled separately), so you don't need to have isempty checks scattered throughout your code.
Partagez un lien vers ce commentaire
Plus de réponses (0)
Catégories
En savoir plus sur Functions dans Centre d'aide et File Exchange
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Sélectionner un site web
Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .
Vous pouvez également sélectionner un site web dans la liste suivante :
Comment optimiser les performances du site
Pour optimiser les performances du site, sélectionnez la région Chine (en chinois ou en anglais). Les sites de MathWorks pour les autres pays ne sont pas optimisés pour les visites provenant de votre région.
Amériques
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
