Csharp/CSharp Tutorial/Class/Method Overload — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 15:31, 26 мая 2010

Automatic type conversions can affect overloaded method resolution: byte

using System; 
 
class Overload2 { 
  public void f(byte x) { 
    Console.WriteLine("Inside f(byte): " + x); 
  } 
 
  public void f(int x) { 
    Console.WriteLine("Inside f(int): " + x); 
  } 
 
  public void f(double x) { 
    Console.WriteLine("Inside f(double): " + x); 
  } 
} 
 
class TypeConv { 
  public static void Main() { 
    Overload2 ob = new Overload2(); 
 
    int i = 10; 
    double d = 10.1; 
 
    byte b = 99; 
    short s = 10; 
    float f = 11.5F; 
 
 
    ob.f(i); // calls ob.f(int) 
    ob.f(d); // calls ob.f(double) 
 
    ob.f(b); // calls ob.f(byte) -- now, no type conversion 
 
    ob.f(s); // calls ob.f(int) -- type conversion 
    ob.f(f); // calls ob.f(double) -- type conversion 
  } 
}
Inside f(int): 10
Inside f(double): 10.1
Inside f(byte): 99
Inside f(int): 10
Inside f(double): 11.5

Automatic type conversions can affect overloaded method resolution: int, double

using System; 
 
class Overload2 { 
  public void f(int x) { 
    Console.WriteLine("Inside f(int): " + x); 
  } 
 
  public void f(double x) { 
    Console.WriteLine("Inside f(double): " + x); 
  } 
} 
 
class MainClass { 
  public static void Main() { 
    Overload2 ob = new Overload2(); 
 
    int i = 10; 
    double d = 10.1; 
 
    byte b = 99; 
    short s = 10; 
    float f = 11.5F; 
 
 
    ob.f(i); // calls ob.f(int) 
    ob.f(d); // calls ob.f(double) 
 
    ob.f(b); // calls ob.f(int) -- type conversion 
    ob.f(s); // calls ob.f(int) -- type conversion 
    ob.f(f); // calls ob.f(double) -- type conversion 
  } 
}
Inside f(int): 10
Inside f(double): 10.1
Inside f(int): 99
Inside f(int): 10
Inside f(double): 11.5

Demonstrate method overloading: different number of parameters

using System; 
 
class Overload {  
  public void ovlDemo() {  
    Console.WriteLine("No parameters");  
  }  
  
  // Overload ovlDemo for one integer parameter.  
  public void ovlDemo(int a) {  
    Console.WriteLine("One parameter: " + a);  
  }  
  
  // Overload ovlDemo for two integer parameters.  
  public int ovlDemo(int a, int b) {  
    Console.WriteLine("Two parameters: " + a + " " + b);  
    return a + b; 
  }  
  
  // Overload ovlDemo for two double parameters.  
  public double ovlDemo(double a, double b) { 
    Console.WriteLine("Two double parameters: " + 
                       a + " "+ b);  
    return a + b;  
  }  
}  
  
class MainClass {  
  public static void Main() {  
    Overload ob = new Overload();  
    int resI; 
    double resD;      
  
    // call all versions of ovlDemo()  
    ob.ovlDemo();   
    Console.WriteLine(); 
 
    ob.ovlDemo(2);  
    Console.WriteLine(); 
 
    resI = ob.ovlDemo(4, 6);  
    Console.WriteLine("Result of ob.ovlDemo(4, 6): " + 
                       resI);  
    Console.WriteLine(); 
 
 
    resD = ob.ovlDemo(1.1, 2.32);  
    Console.WriteLine("Result of ob.ovlDemo(1.1, 2.32): " + 
                       resD);  
  }  
}
No parameters
One parameter: 2
Two parameters: 4 6
Result of ob.ovlDemo(4, 6): 10
Two double parameters: 1.1 2.32
Result of ob.ovlDemo(1.1, 2.32): 3.42

Method Overloading

  1. In C#, two or more methods within the same class can share the same name, as long as their parameter declarations are different.
  2. These methods are said to be overloaded
  3. The process is referred to as method overloading.
  4. Method overloading is one of the ways to implement polymorphism.
  5. It is not sufficient for two methods to differ only in their return types.
  6. The methods must differ in the types or number of their parameters.

7.8.Method Overload 7.8.1. Method Overloading 7.8.2. <A href="/Tutorial/CSharp/0140__Class/Demonstratemethodoverloadingdifferentnumberofparameters.htm">Demonstrate method overloading: different number of parameters</a> 7.8.3. <A href="/Tutorial/CSharp/0140__Class/Automatictypeconversionscanaffectoverloadedmethodresolutionintdouble.htm">Automatic type conversions can affect overloaded method resolution: int, double</a> 7.8.4. <A href="/Tutorial/CSharp/0140__Class/Automatictypeconversionscanaffectoverloadedmethodresolutionbyte.htm">Automatic type conversions can affect overloaded method resolution: byte</a> 7.8.5. <A href="/Tutorial/CSharp/0140__Class/Overloadedconstructors.htm">Overloaded constructors</a> 7.8.6. <A href="/Tutorial/CSharp/0140__Class/Overridingamethod.htm">Overriding a method.</a>

Overloaded constructors

public class Car
{
  private string make;
  private string model;
  private string color;
  private int yearBuilt;
  public Car()
  {
    this.make = "Ford";
    this.model = "Mustang";
    this.color = "red";
    this.yearBuilt = 1970;
  }
  public Car(string make)
  {
    this.make = make;
    this.model = "Corvette";
    this.color = "silver";
    this.yearBuilt = 1969;
  }
  public Car(string make, string model, string color, int yearBuilt)
  {
    this.make = make;
    this.model = model;
    this.color = color;
    this.yearBuilt = yearBuilt;
  }
  public void Display()
  {
    System.Console.WriteLine("make = " + make);
    System.Console.WriteLine("model = " + model);
    System.Console.WriteLine("color = " + color);
    System.Console.WriteLine("yearBuilt = " + yearBuilt);
  }
}
class MainClass
{
  public static void Main()
  {
    Car myCar = new Car("Toyota", "MR2", "black", 1995);
    Car myCar2 = new Car();
    Car myCar3 = new Car("Chevrolet");
    System.Console.WriteLine("myCar details:");
    myCar.Display();
    System.Console.WriteLine("myCar2 details:");
    myCar2.Display();
    System.Console.WriteLine("myCar3 details:");
    myCar3.Display();
  }
}
myCar details:
make = Toyota
model = MR2
color = black
yearBuilt = 1995
myCar2 details:
make = Ford
model = Mustang
color = red
yearBuilt = 1970
myCar3 details:
make = Chevrolet
model = Corvette
color = silver
yearBuilt = 1969

Overriding a method.

using System;
public class Employee {
    private int fAge;
    public Employee() {
        fAge = 21;
    }
    public virtual void setAge(int age) {
        fAge = age;
    }
    public virtual int getAge() {
        return fAge;
    }
}
public class AdultEmployee : Employee {
    public AdultEmployee() {
    }
    override public void setAge(int age) {
        if (age > 21)
            base.setAge(age);
    }
}
class MainClass {
    public static void Main() {
        Employee p = new Employee();
        p.setAge(18);
        AdultEmployee ap = new AdultEmployee();
        ap.setAge(18);
        Console.WriteLine("Employee Age: {0}", p.getAge());
        Console.WriteLine("AdultEmployee Age: {0}", ap.getAge());
    }
}