07. 03. 2025 Pá 0. OOP Cell model - encapsulation, cell - attributes and methods, objects vs. instancies, inheritance, polymorphism How to conceptualize model and code 0.2 Private, protected and public Create class, that inderit to derivated class a try access to method depending on their privacy 1. OOP Lets have an Animals. All Animals have NameOfSpecies, Name of Individual and can produce Voice. All Species are Animals.(All Species are inherit from Animal.) 1.1 Define and create a class Animal - attributes - voice() method prints string "I am prototype of all Animals - I have no voice" 1.1.1 Create an instancies (as variable) of Animal and call method voice() 1.2 Define and create classes of Species that inherit from Animal: - a Dog that can bark - (implement now) produces sound "Haf, Haf" - a Cat that can meows - (do not implement now) produces sound "Mnau" - a Cow that can moo - (implement now) produces sound "Buuu" 1.3 Create an instancies (as variable) of Dogs, Cats nad Cow: 2 dogs, 3 cats and 1 cow. 1.3.1 Call on the instancies method voice() directly on the objects. 1.4.4 Set created instancies (of 2 dogs, 3 cats and 1 cow) to an array/s. (We have static count of animals, but it should not be usefull, and we dont know how many of dogs ... we should have. Solution? List of instancies? What type of the array is?) 1.3.4.1 Set one array of the Dogs and during iteration of array call method voice() on the each instance/dog. 1.3.4.2 Set one array of the Cats and during iteration of array call method voice() on each instance. 1.3.7 Set all instancies of the Animal to the one array. 1.3.9 Iterate array of all animals and call voice() method on them. (What type of an array? What function is called?) 1.4 Overload the methods of the objects so that when the voice function is called, each animal will write ("I am {name of animal} and {the sound the animal makes}") 1.6. Register of all Animals Registr a count of all instancies of the species (How? Eg. by incrementator of class (static atrib)) 1.6.1 Set uID of all instancies (next id) - static atrib - method - class - more (constructor, ...) class Animal { string name; int id = 0; static int next_id; public Animal(string name) { this.name = name; this.id = next_id++; } } 1.7. reImplement function ToString() https://learn.microsoft.com/cs-cz/dotnet/csharp/programming-guide/classes-and-structs/how-to-override-the-tostring-method Každá třída nebo struktura v jazyce C# implicitně dědí Object třídu. Proto každý objekt v jazyce C# získá metodu ToString , která vrátí řetězcovou reprezentaci tohoto objektu. Například všechny proměnné typu int mají metodu ToString , která jim umožňuje vrátit jejich obsah jako řetězec. public override string ToString(){eg. return "Person: " + Name + " " + Age;} https://learn.microsoft.com/cs-cz/dotnet/api/system.object.tostring?view=net-9.0 Při implementaci vlastních typů byste měli přepsat metodu ToString() , aby vrátila hodnoty, které jsou pro tyto typy smysluplné. Odvozené třídy, které vyžadují větší kontrolu nad formátováním, než ToString() poskytuje, mohou implementovat IFormattable rozhraní. Přepsání ToString() metody by se mělo řídit těmito pokyny: Vrácený řetězec by měl být přátelský a čitelný pro lidi. Vrácený řetězec by měl jednoznačně identifikovat hodnotu instance objektu. Vrácený řetězec by měl být co nejkratší, aby byl vhodný pro zobrazení ladicím programem. Přepsání ToString() by nemělo vrátit Empty řetězec s hodnotou null. Přepsání ToString() by nemělo vyvolat výjimku. Pokud je řetězcová reprezentace instance citlivá na jazykovou verzi nebo může být formátována více způsoby, implementujte IFormattable rozhraní. ... 1.8. Extend classes that holds information about age and weight all Animals pro priste ..... sem pridat properties 1.9. Find approach for comparsion of all Animals 1.9.1 By what attribut/metric? 1.9.2 Operator overweight "<" (note: comparators come later with delegates) 1.10. Operator overweight "/" Animal gives a new life Co nam zbyva: - interface s spolu s interface abstraktni tridy a metody - properties: set/get - delegaty - pretizene operatory (souvisi se static) a komparatory (souvisi s delegaty a intefaci)