Csharp/CSharp Tutorial/Class/Method Return

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

How to define methods that return a value and accept parameters

class Car
{
  public int yearBuilt;
  public double maximumSpeed;
  public int Age(int currentYear)
  {
    int age = currentYear - yearBuilt;
    return age;
  }
  public double Distance(double initialSpeed, double time)
  {
    return (initialSpeed + maximumSpeed) / 2 * time;
  }
}
class MainClass
{
  public static void Main()
  {
    Car myCar = new Car();
    myCar.yearBuilt = 2000;
    myCar.maximumSpeed = 150;
    int age = myCar.Age(2001);
    System.Console.WriteLine("myCar is " + age + " year old.");
    System.Console.WriteLine("myCar travels " + myCar.Distance(31, .25) + " miles.");
  }
}
myCar is 1 year old.
myCar travels 22.625 miles.

Methods That Return a Value and Accept Parameters

public class Product {
    public int yearBuilt;
    public double maximumSpeed;
    public int Age(int currentYear) {
        int age = currentYear - yearBuilt;
        return age;
    }
    public double Distance(double initialSpeed, double time) {
        return (initialSpeed + maximumSpeed) / 2 * time;
    }
}

class MainClass{
    public static void Main() {
        Product redPorsche = new Product();
        redPorsche.yearBuilt = 2000;
        redPorsche.maximumSpeed = 150;
        int age = redPorsche.Age(2001);
        System.Console.WriteLine("redPorsche is " + age + " year old.");
        System.Console.WriteLine("redPorsche travels " + redPorsche.Distance(31, .25) + " miles.");
    }
}

Return an array

using System; 
 
class Factor { 
  public int[] findfactors(int num, out int numfactors) { 
    int[] facts = new int[80]; 
    int i, j; 
 
    for(i=2, j=0; i < num/2 + 1; i++)  
      if( (num%i)==0 ) { 
        facts[j] = i; 
        j++; 
      } 
     
    numfactors = j; 
    return facts; 
  } 
} 
  
class MainClass { 
  public static void Main() {   
    Factor f = new Factor(); 
    int numfactors; 
    int[] factors; 
 
    factors = f.findfactors(1000, out numfactors); 
 
    Console.WriteLine("Factors for 1000 are: "); 
    for(int i=0; i < numfactors; i++) 
      Console.Write(factors[i] + " "); 
       
    Console.WriteLine();    
  } 
}
Factors for 1000 are:
2 4 5 8 10 20 25 40 50 100 125 200 250 500

Return an object

using System; 
 
class Rect { 
  int width; 
  int height; 
 
  public Rect(int w, int h) { 
    width = w; 
    height = h; 
  } 
 
  public int area() { 
    return width * height; 
  } 
 
  public void show() { 
    Console.WriteLine(width + " " + height); 
  } 
 
  public Rect enlarge(int factor) { 
    return new Rect(width * factor, height * factor); 
  } 
} 
  
class MainClass { 
  public static void Main() {   
    Rect r1 = new Rect(4, 5); 
 
    Console.Write("Dimensions of r1: "); 
    r1.show(); 
    Console.WriteLine("Area of r1: " + r1.area()); 
 
    Console.WriteLine(); 
 
    // create a rectangle that is twice as big as r1 
    Rect r2 = r1.enlarge(2); 
 
    Console.Write("Dimensions of r2: "); 
    r2.show(); 
    Console.WriteLine("Area of r2 " + r2.area()); 
  } 
}
Dimensions of r1: 4 5
Area of r1: 20
Dimensions of r2: 8 10
Area of r2 80

Return int from function

Methods return a value to the calling routine using this form of return:


return value;

Use a class factory

using System; 
 
class MyClass { 
  int a, b; // private 
 
  // Create a class factory for MyClass. 
  public MyClass factory(int i, int j) { 
    MyClass t = new MyClass(); 
    
    t.a = i; 
    t.b = j; 
 
    return t; // return an object 
  } 
 
  public void show() { 
    Console.WriteLine("a and b: " + a + " " + b); 
  } 
 
} 
  
class MakeObjects { 
  public static void Main() {   
    MyClass ob = new MyClass(); 
    int i, j; 
 
    // generate objects using the factory 
    for(i=0, j=10; i < 10; i++, j--) { 
      MyClass anotherOb = ob.factory(i, j); // make an object 
      anotherOb.show(); 
    } 
       
    Console.WriteLine();    
  } 
}
a and b: 0 10
a and b: 1 9
a and b: 2 8
a and b: 3 7
a and b: 4 6
a and b: 5 5
a and b: 6 4
a and b: 7 3
a and b: 8 2
a and b: 9 1