Hello! He began to study using the C # book for schoolchildren and immediately faced with difficulty. Namely, in the examples that are given in the book it is not clear why everything is underlined with a red line and does not work.

Suppose this example:

enter image description here

using System; class Animal { public string kindOfAnimal; public string name; public int numberOfLegs; public int height; public int length; public string color; bool hasTail; protected bool isMammal; private bool spellingCorrect; } class Zoo { public void GetAnimal() { Animal a = new Animal(); a.kindOfAnimal = "Kangaroo"; a.isMammal = false; a.spellingCorrect = true; } } 

Is that right? As I understand it, I now underline two lines:

 a.isMammal = false; a.spellingCorrect = true; 

So how are they protected and private? So?

And here is a screenshot from the book. It is called C # for schoolchildren.

enter image description here

  • Can you share a quote or a link to a book where it is written? - Yurii Manziuk
  • Sharp schools teach? This is what class? - koks_rs
  • @YuriiManziuk, this is a book from the maykrosovtov site, easily googled by name. The required code is on page 31. download.microsoft.com/documents/eng/msdn/c_sharp2.pdf - Qwertiy
  • four
    @Qwertiy is not a book, but some evil. We need to create a theme with very bad examples and books so that no one else will be led to such nonsense - Yurii Manziuk
  • @YuriiManziuk, they also call the fields “properties” - it came across while scrolling for example ... - Qwertiy

3 answers 3

Yes, the example in the book is wrong. This should not work.


See it.

What is a class, in fact? This is a description of an object that contains data and actions (which are usually called code ).

The code in the class is not all one heap, but in chunks (they are called functions or methods ). In your example, the code is just in the class:

 class Zoo { Animal a = new Animal(); a.kindOfAnimal = "Kangaroo"; a.isMammal = false; a.spellingCorrect = true; } 

But you need to put it in a function so that later you can call this piece of code:

 class Zoo { public Animal CreateAnimal() { Animal a = new Animal(); a.kindOfAnimal = "Kangaroo"; a.isMammal = false; a.spellingCorrect = true; return a; } } 

In this case, you can make this piece of code run:

 Zoo zoo = new Zoo(); Animal kangaroo = zoo.CreateAnimal(); 

Update:

Yes, assigning the isMammal and spellingCorrect fields should not work either. Contact "outside" can only be public (that is, open) data.

  • public Animal CreateAnimal () - what's this? The same class that we create? - Piston
  • A. Is this the method we created in the Zoo class? - Piston
  • @Piston: Yeah, instead of the code right in the class, I put the code in the method. - VladD
  • But Animal a = new Animal (); can in fact lie in the classroom Zoo. Just the members of the Animal class are not available outside the methods of the constructors of the Zoo class. - Urmuz Tagizade
  • Zoo zoo = new Zoo (); Animal kangaroo = zoo.CreateAnimal (); And what's that? - Piston

For example, take 2 classes: Animal and Elephant

 class Animal{} class Elephant{} 

You can create and place an instance of the Animal class in the Elephant class field, but you cannot simply execute the statements directly inside the class (that is, outside methods, constructors, properties, etc.).

 class Animal { public string Name; } class Elephant { Animal elephant = new Animal(); elephant.Name = "Слон Микки"; //Нельзя } 

You can refer to members of the Animal class (methods, fields, etc.) through constructors, methods, events, etc.
For example:

 class Animal { public string Name; } class Elephant { Animal animal = new Animal(); public void GetName() { animal.Name = "Слон"; } } 

Described as briefly as possible. The book you are reading is burning :)
I advise you to read the book, which are described: here
Successful coding ...

  • one
    The point is not so much that one cannot “turn to them in the class”, but rather that there are no operators inside the class, only declarations. The first line is not emphasized because this is a valid declaration of a private field with initialization - but the remaining lines are not valid declarations. - Pavel Mayorov
  • It is very difficult to paint the thought, but corrected it a bit. - Urmuz Tagizade
  • _ "but you can’t access Animal members from within the Elephant class itself - o_O - Qwertiy
  • one
    How perversity a programmer is enough: ideone.com/aFNiME - Qwertiy
  • one
    But there is no limit to the perversity of programmers) Doku to that - comments and answers to any questions) - Urmuz Tagizade

Why everyone attacked the example. He is absolutely correct in the book. Or have we forgotten how to read? It also says in red and white that the next two lines will not be fulfilled. They just say that it will not work like that.

  • a.kindOfAnimal = "Kangaroo"; inside class, without method, will not compile - VladD
  • one
    Yes, I agree - the screenshot shows that there really is everything inside the class, and not inside the method. It was probably supposed to be inside the method, but it was forgotten. Then yes - the example from the screenshot is incorrect, but the comments in it are still correct, assuming that all these lines should be in the method of the Zoo class. - Alexander Y.