📜  C#|返回对象的方法

📅  最后修改于: 2021-05-29 17:04:26             🧑  作者: Mango

在C#中,方法可以返回任何类型的数据,包括对象。换句话说,允许方法返回对象而没有任何编译时错误。

范例1:

// C# program to illustrate the concept
// of the method returning an object
using System;
  
class Example {
  
    // Private data member
    private string str;
  
    // Method to set the value of str
    public void setdata(string s)
    {
        str = s;
    }
  
    // Method to display the value of str
    public void Display()
    {
        Console.WriteLine("String is: " + str);
    }
  
    // Method that return object
    public Example Astr(Example ex)
    {
  
        // Creating object of Example
        Example obj = new Example();
  
        // Adding the value of passed 
        // an object in the current object
        // and adding the sum in another object
        obj.str = str + ex.str;
  
        // Returning the object
        return obj;
    }
}
  
// Driver Class
class GFG {
  
    // Main method
    static void Main()
    {
  
        // Declaring objects of Example
        Example o1 = new Example();
        Example o2 = new Example();
  
        // Initialize the values to the objects
        o1.setdata("Geeks");
        o2.setdata("forGeeks");
  
        // Adding value of both objects
        // and the result will be
        // assigned into third object
        Example o3 = o1.Astr(o2);
  
        // Display the data
        o1.Display();
        o2.Display();
        o3.Display();
    }
}
输出:
String is: Geeks
String is: forGeeks
String is: GeeksforGeeks

说明:在上面的示例中,我们有一个名为Example的类。示例类包含setdata()方法,该方法用于设置str的值, Display()方法用于显示str的值, Astr()用于在当前对象中添加传递的对象的值,并添加在另一个对象中求和。在Main方法中,创建了Example类的三个对象o1o2o3。在此语句中,示例o3 = o1.Astr(o2); ,将o1o2对象的值相加,并将结果分配给o3对象。

范例2:

// C# program to illustrate the 
// concept that how method returns 
// an object
using System;
  
class Triangle {
  
    // Data member of class
    int Base;
    int Height;
  
    // Constructor of class
    public Triangle(int b, int h)
    {
        Base = b;
        Height = h;
    }
  
    // Method return area of triangle
    public int Area()
    {
        return ((Base * Height) / 2);
    }
  
    // Method display the dimension of triangle
    public void Display()
    {
        Console.WriteLine("\nBase of the triangle is: " 
              + Base + "\nHeight of the triangle is:  " 
                                             + Height);
    }
  
    public Triangle newdimension(int d)
    {
        return new Triangle(Base * d, Height * d);
    }
}
  
class GFG {
  
    // Main method
    public static void Main()
    {
  
        // Creating and initializing object
        Triangle t1 = new Triangle(2, 8);
  
        // Display the dimensions and area of triangle
        Console.Write("Dimensions of Triangle is: ");
        t1.Display();
        Console.Write("Area of Triangle is: {0}", t1.Area());
  
        Console.WriteLine();
        Console.WriteLine();
  
        Triangle t2 = t1.newdimension(2);
  
        Console.Write("New Dimensions of Triangle is: ");
        t2.Display();
        Console.Write("New area of Triangle is: {0}", t2.Area());
    }
}
输出:
Dimensions of Triangle is: 
Base of the triangle is: 2
Height of the triangle is:  8
Area of Triangle is: 8

New Dimensions of Triangle is: 
Base of the triangle is: 4
Height of the triangle is:  16
New area of Triangle is: 32

说明:在上面的示例中,我们有一个名为Triangle的类。 Triangle类包含构造函数Triangle() ,方法Area()来查找三角形的区域,方法Display()来显示三角形的尺寸以及方法newdimension()来提供三角形的新尺寸。维的值由对象返回。现在,在Main方法中,有两个对象分别称为t1t2 。在此语句中,三角形t2 = t1.newdimension(2); ,则先前的尺寸(即三角形的2和8)将扩大2并将结果分配给t2对象。