قالب وردپرس درنا توس
Home / Tips and Tricks / Learn C # for Android Part 2 – Classes and Loops (also: Rabbits!)

Learn C # for Android Part 2 – Classes and Loops (also: Rabbits!)



  Learn C # Coding

In the first part of this Android learning series for learning C #, we looked at the basics of C # programming. We covered methods (sets of code that perform certain tasks), some basic syntax (such as semicolons), variables (containers that store data), and "if statements" for flow control (branch code is from the values depending on variables). We also saw how to pass variables like strings as arguments between methods.

You should go back and see if you have not read it yet.

At this point, you should be able to make some basic console apps, such as quizzes, apps that store data, or calculators.

 Learn C # for Android

In Part Two, we'll be more ambitious and cover some more fundamentals – like loops – and explore how to create and interact with classes. This means that we can look at Android development and see how we can close this gap. Continue reading if you really want to learn C #!

Classes and Object-Oriented Programming

In the first part, the basics of object-oriented programming were explained. Languages ​​are described using "classes" to describe "objects". An object is a data element that can represent many things. It could be a literal object in a game world like a pen, or it could be a bit more abstract, like a manager handling the player's score.

A single class can create multiple objects. So you could write a "hostile" class but be able to create a whole lot of bad guys. This is one of the great advantages of object-oriented programming. Otherwise, the only way to deal with the behavior of a multitude of enemies is to use many individual methods, each of which contains instructions on how the villain should behave under different circumstances.

 C # Programming Programming

If this is still a bit tricky to turn your head around, you really just need to know that objects have properties and behavior. That's like real objects. For example, a rabbit has characteristics like size, color and name; and it has behaviors like jumping, sitting and eating. In essence, properties are variables and behavior methods.

The program we created in the last lesson is also an example of a class. The "object" we describe here is a kind of password control system. The property that it has is the UserName string, and the behavior is NewMethod (checking the name of the user and greeting the user).

 Programming Learning C #

If that's Still A bit confusing, the only way to get around is to create one or two new classes yourself!

Creating a New Class

If you want to learn C #, you need to know how to create a new class class. Luckily that is very easy. Just click on the "Project" menu item and select "+ Add class".

 Add C # Class

Select "C #" and name it "rabbit". We will use this class to create conceptual rabbit. You will immediately see what I mean.

If you look to the right of the Solution Explorer, you will see that a new file named Rabbit.cs has been created under Program.cs. Well done – that's one of the most important things you should know if you want to learn C # for Android.

The new Rabbit.cs file contains the same boilerplate code as before. It still belongs to the same namespace and has a class with the same name as the file.

  Namespace ConsoleApp2

{

Class rabbit

{

}

} 

Now we will give our rabbit some properties with a so-called constructor.

 Rabbit Class C #

A constructor is a method in a class that initializes the object so we can define its properties when we first create it. In that case, we'll say the following:

  Namespace ConsoleApp2

{



Class rabbit

{

public string RabbitName;

public string RabbitColor;

public int RabbitAge;

public int RabbitWeight;


public Rabbit (string name, string color, age, weight)

{

RabbitName = name;

RabbitColor = color;

Rabbit Age = age;

Rabbit weight = weight;



}



}

} 

In this way we can create a new rabbit from another class and define its properties as follows:

Rabbit Rabbit1 = new Rabbit ("Jeff", "brown", 1, 1);

Now I realize that weight in retrospect should probably have been a float or a double to account for decimals, but you have the idea. We will round up our rabbit to the next whole number.

When you advertise your rabbit, you are asked to submit the correct arguments. That way your class has become almost a part of the code.

Believe it or not, this code has created a hare! You can not see your rabbit because we do not have graphics, but it's there.

And to prove that, you can now use this line:

  Console.WriteLine (Rabbit1.RabbitName); [19659022

 Learn C # Rabbit Weight

We can also increase the weight of our rabbit as follows:

  Rabbit1.RabbitWeight ++;

Console.WriteLine (Rabbit1.RabbitName + "weighs" + Rabbit1.RabbitWeight + "kg"); 

Note that adding "++" at the end of something incrementally increases its value (you could also write "RabbitWeight = RabbitWeight + 1").

Because our class can produce as many rabbits as we like, we can create many different rabbits, each with its own characteristics.

Adding Behavior

We could then choose to behave in our rabbit. In that case, let them eat.

 Learning C # object classes

To do this, we would create a public method called "Eat", and this would also cause a choking sound to gradually increase the weight of the rabbit: [19659022] public void Eat ()

{

Console.WriteLine (RabbitName + ": Nibble Nibble!");

RabbitWeight ++;

}

Keep in mind that "public" is accessible from outside the class, and "void" means that the method returns no data.

You can then use this method and call them in Program.cs. This will eat the rabbit of our choice and grow larger:

  Console.WriteLine (Rabbit1.RabbitName + "Weighs" + Rabbit1.RabbitWeight + "kg");

Rabbit1.Eat ();

Rabbit1.Eat ();

Rabbit1.Eat ();

Console.WriteLine (Rabbit1.RabbitName + "weighs" + Rabbit1.RabbitWeight + "kg")) 

This causes Jeff to eat three times, then we hear it and can see that it has grown bigger! If we had another rabbit at the scene, they could eat too!

  Console.WriteLine (Rabbit1.RabbitName + "weighs" + Rabbit1.RabbitWeight + "kg");

Console.WriteLine (Rabbit2.RabbitName + "weighs" + Rabbit2.RabbitWeight + "kg");

Rabbit1.Eat ();

Rabbit1.Eat ();

Rabbit2.Eat ();

Rabbit2.Eat ();

Rabbit1.Eat ();

Console.WriteLine (Rabbit1.RabbitName + "weighs" + Rabbit1.RabbitWeight + "kg");

Console.WriteLine (Rabbit2.RabbitName + "Weighs" + Rabbit2.RabbitWeight + "kg")) 

On it like rabbit

This is not a particularly elegant way to handle many objects as we need to write execute the commands for each rabbit manually and can not increase the number of rabbits as much as we would like. We do not just want to learn C # – we want to learn how to write clean C # code!

 Learning object collections C #

That's why we might use a list. A list is a collection; Variable itself, which essentially contains references to other variables. In this case, we can make a list of rabbits, and the good news is that this is very easy to understand:

  List  RabbitList = new list  ();

RabbitList.Add (new rabbit ("Jeff", "brown", 1, 1));

RabbitList.Add (new rabbit ("Sam", "White", 1, 2)); 

This creates the new rabbit as before, but at the same time adds the rabbit to the list. Similarly, we could say this:

  Rabbit Rabbit3 = new rabbit ("Jonny", "orange", 1, 1);

RabbitList.Add (Rabbit3); 

In both cases, an object was created and added to the list.

Information from our rabbit list can also be conveniently and elegantly returned in this way:

  foreach (var Rabbit in rabbit list)

{

Console.WriteLine (Rabbit.RabbitName + "weighs" + Rabbit.RabbitWeight + "kg");

} 

As you might know, "foreach" means that you repeat a step once for each entry in the list. You can also retrieve information from your list as follows:

  RabbitList [1] .Eat (); 

Here, "1" is the index, ie it refers to the information stored in position 1. It's actually the second rabbit you've added: because lists in programming always start at 0.

Fibonacci

If you have not thought, let's continue now Use this information to create a Fibonacci sequence. If you're learning C # for Android, you should be able to actually do something interesting with all of this theory!

 Learning C # Development

In the Fibonacci sequence, rabbits are closed in a room and allowed to breed. They can multiply after a month, then they are sexually mature (I can not confirm if this is the correct biology of the rabbit). Now, if each pair of rabbits can produce once a month and produce two offspring, the sequence looks like this:

1,1,2,3,5,8,13,21,34

Magical Each number in the sequence is the value of the previous two numbers added together. In the opinion of science, this is a big deal.

The cool thing is, we can repeat that.

First we have to introduce a new concept: the loop. This simply repeats the same code over and over again until a condition is met. With the "for" loop, you can do this by creating a variable, specifying the conditions we want to meet, and then work on it – all in brackets:

  for (int months = 0; months <100 months ++)

{

//Do something

} 

So we create an integer called months and repeat the loop until it's 100. Then we increase the number of months by 1.

Would you like to see how this Fibonacci sequence becomes a sequence? See:

  Namespace ConsoleApp2

{

class program

{


static emptiness main (string [] args)

{

List  RabbitList = new list  ();

RabbitList.Add (new rabbit ("Jeff", "Brown", 0, 1));

RabbitList.Add (new rabbit ("Sam", "White", 0, 1));




for (int months = 0; months < 10; months++)

            {

                int firstRabbit = 0;

                int timesToReproduce = 0;




                foreach (var Rabbit in RabbitList)

                {

                    Console.Write("R");




                    if (Rabbit.RabbitAge > 0)

{

if (firstRabbit == 0)

{

firstRabbit = 1;

otherwise

{

firstRabbit = 0;

timesToReproduce ++;

}

}

Rabbit.RabbitAge ++;

}



for (int i = 0; i <timesToReproduce; i ++)

{

RabbitList.Add (new rabbit ("NewBabyRabbit", "Brown", 0, 1));

RabbitList.Add (new rabbit ("NewBabyRabbit", "Brown", 0, 1));

Console.Write ("r");

Console.Write ("r");

}


Console.WriteLine ("--- There are" + RabbitList.Count / 2 + "pairs of rabbits!");

Console.WriteLine ("");

}

Console.WriteLine ("All done!");

Console.ReadKey ();
}

}

}


Okay, that was harder than I thought!

I will not go through everything, but with what you have already learned, you should be able to reverse it.

There are definitely more elegant ways – I'm not a mathematician. But I think it's a pretty fun exercise and as soon as you can, you're ready for the big time.

Incidentally, I would like to see other approaches!

Where are we going? from here? How to Learn C # for Android

With all this knowledge you can start with bigger things. In particular, you can plunge with C # in Xamarin or Unity on the Android programming.

 Learn C # for Android

This is different because you use classes provided by Google, Microsoft and Unity. If you write something like "RigidBody2D.velocity", you are accessing a property from a class named RigidBody2D. It works the same way, the only difference is that you can not see RigidBody2D because you did not build it yourself.

With this C # under your belt, you should be ready to jump into one of these options and have a wide range of edge when it comes to understanding what's going on:

In an upcoming lesson we will too examine how you can turn around and create Windows apps instead!




Source link