Содержание
- 2. 3) public class MyNumber { private int number; public MyNumber(int number) { this.number = number; }
- 3. 6) class MyClass { static MyClass () { // код инициализации } // остаток определения класса
- 4. { DateTime now = DateTime.Now; if (now.DayOfWeek == DayOfWeek.Saturday || now.DayOfWeek == DayOfWeek.Sunday) BackColor = Color.Green;
- 5. 9) class MainEntryPoint { static void Main(string[] args) { Console.WriteLine("Предпочтение пользователя: BackColor равно: " + UserPreferences.BackColor.ToString());
- 6. public Car(string description, uint nWheels) { this.description = description; this.nWheels = nWheels; } public Car(string description)
- 7. public Car(string description) : this(description, 4) { } // и т.д. 13) Car myCar = new
- 8. // Читаем дату создания файла. Предположим, что в результате // получаем 1 января 2010 г., но
- 9. 20) Листинг 2.8 // Использовать модификатор static. using System; class StaticDemo { // Переменная типа static.
- 10. class SDemo { static void Main() { Console.WriteLine("Исходное значение переменной " + StaticDemo.Val); StaticDemo.Val = 8;
- 11. 22) Листинг 2.9 class StaticError { public int Denom = 3; // обычная переменная экземпляра public
- 12. void NonStaticMeth() { Console.WriteLine("В методе NonStaticMeth()."); } /* Ошибка! Непосредственный вызов нестатического метода из статического метода
- 13. /* Нестатический метод может быть вызван из статического метода по ссылке на объект. */ public static
- 14. // Инкременировать подсчет при создании объекта. public CountInst() { count++; } // Декременировать подсчет при уничтожении
- 15. class CountDemo { static void Main() { CountInst ob; for(int i=0; i ob = new CountInst();
- 16. Текущий подсчет: 6 Текущий подсчет: 7 Текущий подсчет: 8 Текущий подсчет: 9 Текущий подсчет: 10 27)
- 17. t.a = i; t.b = j; return t; // возвратить объект } public void Show() {
- 18. // Создать объекты, используя фабрику. for(i=0, j=10; i MyClass ob = MyClass.Factory(i, j); // создать объект
- 19. 29) class Dimensions { public double Length; public double Width; } 30) struct Dimensions { public
- 20. Dimensions(double length, double width) { Length=length; Width=width; } public int Diagonal { get { return Math.Sqrt(Length*Length
- 21. 33) Dimensions point; point.Length = 3; point.Width = 6; 34) Dimensions point; Double D = point.Length;
- 22. 36) partial class TheBigClass { public void MethodOne() { } } // BigClassPart2.cs partial class TheBigClass
- 23. 38) Листинг 2.15 [CustomAttribute] partial class TheBigClass : TheBigBaseClass, IBigClass { public void MethodOne() { }
- 24. 39) [CustomAttribute] [AnotherAttribute] partial class TheBigClass:TheBigBaseClass,IBigClass,OtherBigClass { public void MethodOne() { } public void MethodTwo() {
- 25. 41) StaticUtilities.HelperMethod(); 42) int i = -50; string str = i.ToString(); // возвращает "-50" 43) enum
- 26. static void Main(string[] args) { Money cashl = new Money(); cashl.Amount = 40M; Console.WriteLine("cashl.ToString() возвращает: "
- 27. set { amount = value; } } public override string ToString() { return "$'* + Amount.ToString();
- 29. Скачать презентацию