, , : " 5 ".
? , , , . .
, . . - , , , , , , .
: | |
, . , . , "LuckyNumber" . | class Person { // string firstName; string lastName; // public void LuckyNumber( int numberOfTeeth, int age) { Console.WriteLine(" " + numberOfTeeth * age); } } Person Petr; Petr= new Person(); Petr.LuckyNumber(24, 14); |
. : " Console.WriteLine, , ?". , (, C#) .
void-. , : " ". . , , void.
. , , .
, , . , , , C#:
- , , , number of legs = 4;
- , , , , number of legs = 2;
- , , , , number of legs = 1;
- , - , number of legs = 0.
int NumberOfLegs(string animalName)
{
if (animalName == "") //
{
// 4
return 4;
}
else if (animalName == "") //,
{
// 2
return 2;
}
|
|
else if (animalName == "")//,
{
// 1
return 1;
}
else // ( )
{
// 0
return 0;
}
}
. :
int i;
// "i" .
i = NumberOfLegs("");
// i = 2, , NumberOfLegs
Console.WriteLine(" " + i);
i = NumberOfLegs("");
// i = 0. , !
Console.WriteLine(" " + i);
: " 2", " 0". , , .
:
int NumberOfLegs(string animalName)
{
...
}
:
void NumberOfLegs(string animalName)
{
...
}
:
string NumberOfLegs(string animalName)
{
...
}
, , "" (void), , . Integer, int .
, . , void .
void JustWriteSomething(string someThing)
{
Console.WriteLine(someThing);
}
: , , return . , .
: | |
. void, , " ", , . , . , , . WriteLine. | class Person { // string firstName; string lastName; // int LuckyNumber(int numberOfTeeth, int age) { return (numberOfTeeth * age); } } Person Anna; Anna = new Person(); int num = Anna.LuckyNumber(24, 14); Console.WriteLine(" :" + num); Console.WriteLine(" :" + Anna.LuckyNumber(24, 14)); |
, , . private (), . , public .
public void JustWriteSomething(string someThing)
{
Console.WriteLine(someThing);
}
. , . , . .
|
|
. : (private) (public) Animal .
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;
//
// , ,
public string GetFoodInfo()
{
// , ,
}
//
private void SpellingCorrect()
{
// ,
}
// ,
protected bool IsValidAnimalType()
{
//
}
}
class Zoo
{
Animal a = new Animal ();
a.name = "Kangaroo";
string food;
bool animalExists;
// , "Zoo"
// "Animal"
food = a.GetFoodInfo(); //
// , "Zoo"
//
a.spellingCorrect(); //
animalExists = a.IsValidAnimalType(); //
}
, "". ( ) , - . () . .
. , . , , . , . .
class Person
{
//
string firstName;
string lastName;
// - Person
public Person()
{
firstName = "Johnny";
lastName = "Rocket";
}
}
- -: new.
:
" " . , " " , Animal.
, :
Person p = new Person();
Console.WriteLine(p.lastName);
"Rocket". new Person(), Person . p, "Rocket" lastName.
. , , , .
|
|
-, . - , -. , " ".