logo search
CSharp_Prog_Guide

Порядок переопределения метода OnString в классе или структуре

  1. Объявите метод ToString со следующими модификаторами и типом возвращаемого значения:

    public override string ToString(){}

  2. Реализуйте этот метод таким образом, чтобы он возвращал строку.

Приведенный ниже пример возвращает не только имя класса, но и специфические данные для конкретного экземпляра класса. Обратите внимание, что он также использует метод ToString в переменной age для преобразования типа int в строку, которая может служить выходными данными.

----

Interfaces

Interfaces are defined by using the interface keyword. For example:

interface IComparable

{

int CompareTo(object obj);

}

Interfaces describe a group of related functionalities that can belong to any class or struct. Interfaces can consist of methods, properties, events, indexers, or any combination of those four member types. An interface cannot contain fields. Interfaces members are automatically public.

Classes and structs can inherit from interfaces in a manner similar to how classes can inherit a base class or struct, with two exceptions:

public class Minivan : Car, IComparable

{

public int CompareTo(object obj)

{

//implementation of CompareTo

return 0; //if the Minivans are equal

}

}

To implement an interface member, the corresponding member on the class must be public, non-static, and have the same name and signature as the interface member. Properties and indexers on a class can define extra accessors for a property or indexer defined on an interface. For example, an interface may declare a property with a get accessor, but the class implementing the interface can declare the same property with both a get and set accessor. However, if the property or indexer uses explicit implementation, the accessors must match.