Половина урока 3.2
This commit is contained in:
parent
2101d584f3
commit
a385581ef2
@ -20,4 +20,73 @@ class Person
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
=== ===
|
=== Конструкторы, инициализаторы и деконструкторы ===
|
||||||
|
|
||||||
|
Person tom = new Person();
|
||||||
|
Person bob = new Person("Bob");
|
||||||
|
Person sam = new Person("Sam", 25);
|
||||||
|
tom.Print();
|
||||||
|
bob.Print();
|
||||||
|
sam.Print();
|
||||||
|
|
||||||
|
class Person
|
||||||
|
{
|
||||||
|
public string name;
|
||||||
|
public int age;
|
||||||
|
public Person()
|
||||||
|
{
|
||||||
|
name = "Unknown";
|
||||||
|
age = 18;
|
||||||
|
}
|
||||||
|
public Person(string n) { name = n; age = 18; }
|
||||||
|
public Person(string n, int a) { name = n; age = a; }
|
||||||
|
|
||||||
|
public void Print()
|
||||||
|
{
|
||||||
|
Console.WriteLine($"name: {name} age: {age}");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Person sam = new Person("Sam", 25);
|
||||||
|
sam.Print();
|
||||||
|
|
||||||
|
class Person
|
||||||
|
{
|
||||||
|
public string name;
|
||||||
|
public int age;
|
||||||
|
public Person()
|
||||||
|
{
|
||||||
|
name = "Unknown";
|
||||||
|
age = 18;
|
||||||
|
}
|
||||||
|
public Person(string name) { this.name = name; age = 18; }
|
||||||
|
public Person(string name, int age)
|
||||||
|
{
|
||||||
|
this.name = name;
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
public void Print()
|
||||||
|
{
|
||||||
|
Console.WriteLine($"name: {name} age: {age}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
"Цепочка вызова конструкторов"
|
||||||
|
Person sam = new Person("Sam", 25);
|
||||||
|
sam.Print();
|
||||||
|
|
||||||
|
class Person
|
||||||
|
{
|
||||||
|
public string name;
|
||||||
|
public int age;
|
||||||
|
public Person() : this("Unknown") { }
|
||||||
|
public Person(string name) : this(name, 18) { }
|
||||||
|
public Person(string name, int age)
|
||||||
|
{
|
||||||
|
this.name = name;
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
public void Print() => Console.WriteLine($"Name: {name} Age: {age}");
|
||||||
|
}
|
||||||
@ -1,19 +1,16 @@
|
|||||||
Person tom = new Person();
|
Person sam = new Person("Sam", 25);
|
||||||
string personName = tom.name;
|
sam.Print();
|
||||||
int personAge = tom.age;
|
|
||||||
Console.WriteLine($"name: {personName} age: {personAge}");
|
|
||||||
|
|
||||||
tom.name = "Tom";
|
|
||||||
tom.age = 37;
|
|
||||||
tom.Print();
|
|
||||||
|
|
||||||
class Person
|
class Person
|
||||||
{
|
{
|
||||||
public string name = "Undefined";
|
public string name;
|
||||||
public int age;
|
public int age;
|
||||||
|
public Person() : this("Unknown") { }
|
||||||
public void Print()
|
public Person(string name) : this(name, 18) { }
|
||||||
|
public Person(string name, int age)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Имя: {name} Возраст: {age}");
|
this.name = name;
|
||||||
|
this.age = age;
|
||||||
}
|
}
|
||||||
|
public void Print() => Console.WriteLine($"Name: {name} Age: {age}");
|
||||||
}
|
}
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -13,7 +13,7 @@ using System.Reflection;
|
|||||||
[assembly: System.Reflection.AssemblyCompanyAttribute("CSHARP")]
|
[assembly: System.Reflection.AssemblyCompanyAttribute("CSHARP")]
|
||||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
||||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
||||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+aa050c247f6147a284a534e34522aed399958c7f")]
|
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+2101d584f338f6f88ae1a4305d8bd8c1bb8d13cf")]
|
||||||
[assembly: System.Reflection.AssemblyProductAttribute("CSHARP")]
|
[assembly: System.Reflection.AssemblyProductAttribute("CSHARP")]
|
||||||
[assembly: System.Reflection.AssemblyTitleAttribute("CSHARP")]
|
[assembly: System.Reflection.AssemblyTitleAttribute("CSHARP")]
|
||||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||||
|
|||||||
@ -1 +1 @@
|
|||||||
8b42992521c44adabdfff2f0c94aca9302acbb4a3d22a089bd2074043d71fed8
|
8e1d40d7011353f00b3998c361926632fbcce86fc7b441528c4e189b70a28f7a
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user