You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
573 lines
19 KiB
573 lines
19 KiB
using System; |
|
using System.Text; |
|
using System.Collections; |
|
using System.Linq; |
|
using static System.Console; |
|
using System.Collections.Generic; |
|
|
|
namespace test_3d_chapter_ |
|
{ |
|
class Program |
|
{ |
|
static void Main(string[] args) |
|
{ |
|
DateTime data = new DateTime(); |
|
int god = data.Year; |
|
|
|
Wine vin = new Wine("Zaboristo", 200); //ПЕРЕГРУЗКА КОНСТРУКТОРА |
|
Wine vin2 = new Wine("Normas", 1000, 2015); |
|
vin.Vino(); |
|
vin2.Vino(); |
|
Otstup(); |
|
|
|
//СПОСОБЫ ПЕРЕДАЧИ ЗНАЧЕНИЙ В КОНСТРУКТОР |
|
Vodka vodka = new Vodka { Name = "Tsarskaya", Capacity = 1, good = true }; |
|
Vodka vodka2 = new Vodka("5 ozer") { Capacity = 2, good = false }; |
|
vodka.Voda(); |
|
vodka2.Voda(); |
|
Otstup(); |
|
|
|
neznau nz = new neznau(10); |
|
WriteLine("Задано конструктором: {0} \nТолько для чтения: {1} и {2} \nСоздано автоматически: {3} {4} {5}" |
|
,nz.znachenie, nz.DlyaChteniya, nz.DlyaChteniya2, nz.avto, nz.avto+=20, nz.avto); |
|
Otstup(); |
|
|
|
double a = 1.99, b = 1.234, c = 1.234567; //ОКРУГЛЕНИЕ |
|
WriteLine("{0:F1} {1:F2} {2:F3}", a, b, c); |
|
Otstup(); |
|
|
|
string s1 = "string", s2 = null; //ИНДЕК В STRING |
|
WriteLine("Обращение по индексу [0] к слову string: {0} и к null: {1}", s1?[0], s2?[0]); |
|
Otstup(); |
|
|
|
|
|
Sentence s = new Sentence(); //РЕАЛИЗАЦИЯ ИНДЕКСАТОРА |
|
WriteLine("{0} {1}", s[3], s[2, " dva"]); |
|
for(int i = 0; i < s.Count; i++) |
|
Write(s[i] + " "); |
|
Otstup(); |
|
|
|
Write("{0} vtoroe\n", pole1.x); |
|
Write("{0} vtoroe", pole2.x); |
|
Otstup(); |
|
|
|
WriteLine("{0}.{1}", nameof(StringBuilder), nameof(StringBuilder.Length)); //РЕАЛИЗАЦИЯ NAMEOF |
|
Otstup(); |
|
|
|
Beer beer1 = new Beer { Name = "KrushoviceT", Capacity = 1, Color = "black" }; //НАСЛЕДОВАНИЕ |
|
Beer beer2 = new Beer { Name = "Bud", Capacity = 2, Color = "bright" }; |
|
beer1.beer(); |
|
beer2.beer(); |
|
beer1.Voda(); |
|
Otstup(); |
|
Imya(vodka2, vin); //ПОЛИМОРФИЗМ |
|
Imya(beer1, vin2); |
|
Otstup(); |
|
|
|
Beer vverh = new Beer{Name = "name_vverh", Color = "color_vverh"}; |
|
Vodka vod = vverh; //ПРИВЕДЕНИЕ ВВЕРХ (всегда успешно) |
|
string vo = vod.Name; |
|
//string color = vod.Color; - ОШИБКА, т.к. Color не инициализирован в Vodka |
|
|
|
Beer vniz = (Beer)vod; //ПРИВЕДЕНИЕ ВНИЗ |
|
WriteLine("{0} {1} {2} {3} \n{4} {5}", |
|
vo, vverh.Name, vniz.Name, vniz.Color, vverh == vniz, vverh == vod); |
|
Otstup(); |
|
|
|
|
|
Vodka vodkatemp = new Vodka(); |
|
Beer beertemp = vodkatemp as Beer; //НЕУДАЧНОЕ ПРИВЕДЕНИЕ ВНИ3 С ПОМОЩЬЮ AS |
|
if (beertemp == null) WriteLine("null"); |
|
|
|
Beer beertemp2 = new Beer { Name = "Beertemp2" }; |
|
Vodka vodkatemp2 = beertemp2; |
|
Beer beertempo = vodkatemp2 as Beer; //УДАЧНОЕ ПРИВЕДЕНИЕ ВНИЗ |
|
WriteLine("{0} {1} {2}\n", beertempo.Name, beertemp2.Name = "name", beertempo.Name); |
|
|
|
if (vodkatemp is Beer) |
|
WriteLine("Complete {0}", ((Beer)vodkatemp).Color); |
|
else WriteLine("Not complete"); |
|
Otstup(); |
|
|
|
|
|
Vodka vverh2 = beer2; //ВИРТУАЛЬНОЕ СВОЙСТВО И ПЕРЕОПРЕДЕЛНИЕ |
|
WriteLine("{0} {1} {2}\n", vodka.Virtualka, beer1.Virtualka, vverh2.Virtualka); |
|
|
|
Abs2 abstr = new Abs2(5, 10); //АБСТРАКТНОЕ СВОЙСТВО И ПЕРЕОПРЕДЕЛЕНИЕ |
|
WriteLine("{0}", abstr.abs); |
|
Otstup(); |
|
|
|
|
|
Overrider overrider = new Overrider(); |
|
Hider hider = new Hider(); |
|
BaseClass b1 = overrider; |
|
BaseClass b2 = hider; |
|
WriteLine("Overrider: {0} Вверх от overrider: {1} \nHider: {2} Вверх от hider: {3}", overrider.Foo(), b1.Foo(), hider.Foo(), b2.Foo()); |
|
Otstup(); |
|
|
|
|
|
WriteLine("{0} {1}", vodka.Virtu, beer1.Virtu); |
|
Subclass sb = new Subclass(10); |
|
WriteLine(sb.X); |
|
Otstup(); |
|
|
|
WriteLine("{0} {1}", Metod(beer1), Metod(vodka)); |
|
Otstup(); |
|
|
|
Stack stack = new Stack(); |
|
stack.Push(12); |
|
stack.Push('g'); |
|
stack.Push("стэк"); |
|
int stk = 10; |
|
stack.Push(stk); //УПАКОВКА |
|
string stackstring = (string)stack.Pop(2); //РАСПАКОВКА |
|
int stackint = (int)stack.Pop(0); |
|
stk = 20; //ЗНАЧЕНИЕ В СТЭКЕ ЭТО КОПИЯ ЗНАЧЕНИЯ ПОЛЯ |
|
WriteLine("{0} {1} {2} {3} \n{4} {5}\n{6} {7}\n{8} {9}", |
|
stack.Pop(), stack.Pop(), stack.Pop(), stack.Pop(1), stackstring, stackint, stk, stack.Pop(3), stack.Pop(3).GetType().Name, typeof(Stack)); |
|
Otstup(); |
|
|
|
Point p1 = new Point(); //СТРУКТУРЫ |
|
Point p2 = new Point(1, 1); |
|
Point p3 = new Point(y: 1, x: 2); |
|
p1.ToString(); |
|
|
|
|
|
Class2 class2 = new Class2(); //ДОСТУП К ПОЛЯМ |
|
Class1 class1 = new Class1(); |
|
class1.ToString(); //PRIVATE |
|
class2.x.ToString(); //INTERNAL |
|
|
|
|
|
IEnumerator1 e = new CountDown(); //ИНТЕРФЕЙСЫ |
|
while (e.MoveNext()) |
|
Write("{0} ", e.Current); |
|
WriteLine(); |
|
|
|
ClassI classI = new ClassI(); |
|
classI.Foo(); |
|
((I1)classI).Foo(); |
|
((I2)classI).Foo(); |
|
WriteLine(); |
|
|
|
TexBox t = new TexBox(); |
|
RichTextBox r = new RichTextBox(); |
|
WriteLine("Наследование только суперкласса:"); |
|
t.Udo(); |
|
((IUndo)t).Udo(); |
|
r.Udo(); |
|
((TexBox)r).Udo(); |
|
((IUndo)r).Udo(); |
|
WriteLine("\nНаследование суперкласса и интерфейса"); |
|
t.Undo(); |
|
((IUndo)t).Undo(); |
|
r.Undo(); |
|
((TexBox)r).Undo(); |
|
((IUndo)r).Undo(); |
|
Otstup(); |
|
|
|
|
|
int hond = (int)Cars.Honda; |
|
Cars honda = (Cars)hond; |
|
bool h = (int)honda == 3; |
|
CarsAsian asi = (CarsAsian)Cars.Mitsu; |
|
asi.ToString(); |
|
Car asian = Car.Mitsu | Car.Toyota; |
|
if ((asian & Car.Ford) != 0) |
|
WriteLine("Форд есть"); |
|
else WriteLine("Форда нет"); |
|
|
|
if ((asian & Car.Toyota) != 0) |
|
WriteLine("Тойота есть"); |
|
else WriteLine("Тойоты нет"); |
|
|
|
Car hk = Car.Toyota; |
|
hk |= Car.Mitsu; |
|
WriteLine(hk == asian); |
|
Otstup(); |
|
|
|
for(int i = 0; i <= 16; i++) |
|
{ |
|
Car cc = (Car)i; |
|
WriteLine("{0} {1} {2}", i, IsFlagDefined(cc), cc); |
|
} |
|
int blue = (int)TopLevel.Color.blue; |
|
blue.ToString(); |
|
Otstup(); |
|
|
|
var stack2 = new Stack<int>(); |
|
stack2.Push(10); |
|
stack2.Push(20); |
|
int twen = stack2.Pop(); |
|
int ten = stack2.Pop(); |
|
WriteLine("ten: {0}, twen: {1}", ten, twen); |
|
|
|
|
|
Swap(ref ten, ref twen); |
|
WriteLine("ten: {0}, twen: {1}", ten, twen); |
|
Car Ford = Car.Ford; |
|
Car Mazda = Car.Mazda; |
|
Swap(ref Ford, ref Mazda); |
|
WriteLine("Ford: {0}, Mazda: {1}", Ford, Mazda); |
|
Otstup(); |
|
|
|
WriteLine(Max(20, 30)); |
|
Otstup(); |
|
|
|
Stack2<Bear> bears = new Stack2<Bear>(); |
|
// Stack<Animal> animals = bears; ОШИБКА |
|
IPoppable<Animal> animals = bears; |
|
IPushable<Animal> animalsp = new Stack2<Animal>(); |
|
IPushable<Bear> bearsp = animalsp; |
|
|
|
var objectComparer = Comparer<object>.Default; |
|
IComparer<string> comparer = objectComparer; |
|
int result = comparer.Compare("Bret", "Breta"); |
|
WriteLine(result); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ReadKey(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//МЕТОДЫ И КЛАССЫ |
|
|
|
|
|
|
|
|
|
internal class CountDown : IEnumerator1 |
|
{ |
|
int count; |
|
internal CountDown (int count = 10) |
|
{ |
|
this.count = count; |
|
} |
|
public bool MoveNext() => count-- > 0; |
|
public object Current => count; |
|
public void Reset() { throw new NotSupportedException(); } |
|
} |
|
|
|
class Class1 |
|
{ |
|
int x; |
|
public Class1() { } |
|
} |
|
|
|
public class Class2 |
|
{ |
|
internal int x; |
|
public int y; |
|
public Class2() { } |
|
} |
|
|
|
static void Otstup() |
|
{ |
|
WriteLine("\n"); |
|
} |
|
|
|
public class Wine |
|
{ |
|
public string Name; |
|
public int Price; |
|
public int Year; |
|
public Wine (string name, int price) { Price = price; Name = name; } |
|
public Wine (string name, int price, int year) : this(name, price) { Year = year; } |
|
|
|
public void Vino() |
|
{ |
|
if (Year != 0) |
|
WriteLine("Название: {0}. Цена: {1} р. {2} год", Name, Price, Year); |
|
else |
|
WriteLine("Название: {0}. Цена: {1} р. Год неизвестен", Name, Price); |
|
} |
|
} |
|
|
|
public class Vodka |
|
{ |
|
public int chislo = 1; |
|
public string Name; |
|
public bool good; |
|
public int Capacity; |
|
public virtual int Virtualka => 0; |
|
public virtual string Virtu => chislo.ToString(); |
|
|
|
public Vodka() { } |
|
public Vodka(string n) { Name = n; } |
|
|
|
public void Voda() |
|
{ |
|
WriteLine("Название: {0}. Объём: {1}л. Качественная: {2} ", Name, Capacity, good); |
|
} |
|
} |
|
|
|
public class Beer : Vodka //НАСЛЕДОВАНИЕ |
|
{ |
|
public new int chislo = 2; |
|
public string Color; |
|
public sealed override int Virtualka => Capacity + 13000; //ПЕРЕОПРЕДЕЛЕНИЕ ВИРТУАЛЬНОГО СВОЙСТВА |
|
public override string Virtu => base.Virtu + chislo.ToString(); |
|
|
|
public void beer() |
|
{ |
|
WriteLine("Название: {0}. Объём: {1}л. Цвет: {2}.", Name, Capacity, Color); |
|
} |
|
|
|
} |
|
|
|
public static void Imya (Vodka vodka, Wine wine) //ПОЛИМОРФИЗМ |
|
{ |
|
WriteLine(vodka.Name + " " + wine.Name); |
|
} |
|
|
|
public class neznau |
|
{ |
|
public int znach; |
|
public int znachenie |
|
{ |
|
get { return znach + 2; } |
|
set { znach = value; } |
|
} |
|
|
|
public int DlyaChteniya => (int)Math.Pow(znachenie, 2); |
|
public int DlyaChteniya2 { get; } = 100; |
|
|
|
|
|
public neznau(int a) |
|
{ |
|
znach = a; |
|
} |
|
|
|
|
|
public int avto { get; set; } = 123; |
|
} |
|
|
|
public class Sentence |
|
{ |
|
public string[] words = "Это какое-то предложение. Что же делать?".Split(); |
|
public int Count = 6; |
|
|
|
// public string this[int wordNum] => words[wordNum]; - ТОЛЬКО ДЛЯ ЧТЕНИЯ |
|
public string this [int wordNum] |
|
{ |
|
get { return words[wordNum]; } |
|
set { words[wordNum] = value; } |
|
} |
|
|
|
public string this[int wordNum, string s] |
|
{ |
|
get { return words[wordNum] + s; } |
|
set { words[wordNum] = value; } |
|
} |
|
} |
|
|
|
public class pole1 |
|
{ |
|
public static pole1 Instance = new pole1(); |
|
public static int x = 3; |
|
pole1() { Write(x + " pervoe. "); } |
|
} |
|
|
|
public class pole2 |
|
{ |
|
public static int x = 3; |
|
public static pole2 Instance = new pole2(); |
|
pole2() { Write(x + " pervoe. "); } |
|
} |
|
|
|
public abstract class Abs1 //Абстрактный класс 3апрещает создавать экземпляры своего класса |
|
{ //Такой класс может только наследоваться |
|
public abstract int abs { get; } |
|
} |
|
|
|
public class Abs2 : Abs1 |
|
{ |
|
public int a, b; |
|
public Abs2 (int a, int b) |
|
{ |
|
this.a = a; |
|
this.b = b; |
|
} |
|
public override int abs => a*b; //Абстрактное свойство не может вызываться в абстрактном классе |
|
} |
|
|
|
|
|
public class BaseClass |
|
{ |
|
public virtual string Foo() => "BaseClass.Foo"; |
|
} |
|
|
|
public class Overrider : BaseClass |
|
{ |
|
public override string Foo() => "Overrider.Foo"; |
|
} |
|
|
|
public class Hider : BaseClass |
|
{ |
|
public new string Foo() => "Hider.Foo"; |
|
} |
|
|
|
|
|
public class Baza |
|
{ |
|
public int X; |
|
public Baza (int x) |
|
{ |
|
X = x; |
|
} |
|
} |
|
|
|
public class Subclass : Baza |
|
{ |
|
public Subclass(int x) : base(x) { } //НАСЛЕДОВАНИЕ КОНСТРУКТОРА |
|
} |
|
|
|
public static string Metod(Vodka v) => v.Name; |
|
public static string Metod(Beer b) => b.Color; |
|
|
|
|
|
public class Stack //СТЭК |
|
{ |
|
int position; |
|
object[] data = new object[10]; |
|
public void Push(object obj) { data[position++] = obj; } |
|
public object Pop(int i) => data[i]; |
|
public object Pop() => data[--position]; |
|
} |
|
|
|
public struct Point |
|
{ |
|
int x, y; |
|
public Point(int x, int y) { this.x = x; this.y = y; } |
|
} |
|
|
|
public interface IEnumerator1 //ИНТЕРФЕЙСЫ |
|
{ |
|
bool MoveNext(); |
|
object Current { get; } |
|
void Reset(); |
|
} |
|
|
|
internal class CountD : IEnumerator |
|
{ |
|
int count; |
|
internal CountD(int count = 10) |
|
{ |
|
this.count = count; |
|
} |
|
public object Current => count; |
|
public bool MoveNext() => count-- > 0; |
|
public void Reset() { throw new NotSupportedException(); } |
|
} |
|
|
|
interface I1 |
|
{ |
|
void Foo(); |
|
} |
|
|
|
interface I2 |
|
{ |
|
int Foo(); |
|
} |
|
|
|
public class ClassI : I1, I2 |
|
{ |
|
public int Foo() { WriteLine("Foo из I2"); return 42; } |
|
void I1.Foo() { WriteLine("Foo из I1"); } |
|
} |
|
|
|
interface IUndo |
|
{ |
|
void Undo(); |
|
void Udo(); |
|
} |
|
|
|
public class TexBox : IUndo |
|
{ |
|
public virtual void Undo() => WriteLine("TextBox.Undo"); |
|
public void Udo() => WriteLine("TextBox.Udo"); |
|
} |
|
|
|
public class RichTextBox : TexBox |
|
{ |
|
public override void Undo() => WriteLine("RichTextBox.Undo"); |
|
public new void Udo() => WriteLine("RichTextBox.Udo"); |
|
} |
|
|
|
public enum Cars { Ford, Mers, Toyota, Honda, Mitsu, Kia, Reno} //ПЕРЕЧИСЛЕНИЯ |
|
public enum Cars2 { Ford = 1, Mers = 2, Toyota, Honda = 10, Mitsu, Kia, Reno } |
|
//public enum Cars2 : byte { Ford = 1, Mers = 2, Toyota=3, Honda = 10, Mitsu=11, Kia=12, Reno=12 } - эквивалентно предыдущей строке |
|
public enum CarsAsian { Toyota = Cars.Toyota, Honda = Cars.Honda, Mizu} |
|
|
|
[Flags] |
|
public enum Car { None = 0, Mitsu = 1, Toyota = 2, Ford = 4, Mazda = 8 } |
|
|
|
|
|
|
|
public static bool IsFlagDefined(Enum e) //ПРОВЕРКА НА СООТВЕТСТВИЕ ENUM |
|
{ |
|
decimal d; |
|
return !decimal.TryParse(e.ToString(), out d); |
|
} |
|
|
|
|
|
public static class TopLevel |
|
{ |
|
public enum Color { green, blue, red} |
|
} |
|
|
|
public class Animal { public string Name; } |
|
public class Bear : Animal { } |
|
public class Camel : Animal { } |
|
|
|
public class Stack<T> //ОБОБЩЕННЫЕ ОБЪЕКТЫ |
|
{ |
|
int position; |
|
T[] data = new T[100]; |
|
public void Push(T obj) { data[position++] = obj; } |
|
public object Pop(int i) => data[i]; |
|
public T Pop() => data[--position]; |
|
public T this [int index] => data[index]; //ИНДЕКС В ОБОБЩЕННОМ ТИПЕ |
|
} |
|
|
|
public class Stack2<T> : IPoppable<T>, IPushable<T> |
|
{ |
|
int position; |
|
T[] data = new T[100]; |
|
public void Push(T obj) { data[position++] = obj; } |
|
public object Pop(int i) => data[i]; |
|
public T Pop() => data[--position]; |
|
public T this[int index] => data[index]; |
|
} |
|
|
|
public interface IPoppable<out T> { T Pop(); } |
|
public interface IPushable<in T> { void Push(T obj); } |
|
|
|
public static void Swap<T> (ref T x, ref T y) |
|
{ |
|
T temp = x; |
|
x = y; |
|
y = temp; |
|
} |
|
|
|
static T Max <T> (T a, T b) where T: IComparable<T> |
|
{ |
|
return a.CompareTo(b) > 0 ? a : b; |
|
} |
|
} |
|
}
|
|
|