Csharp/CSharp Tutorial/Class/Member Method
Содержание
Define methods
- Methods are subroutines that manipulate the data defined by the class and provide access to that data.
- Other parts of your program will interact with a class through its methods.
- The general form of a method is shown here:
access ret-type name(parameter-list) {
// body of method
}
Define Rectangle class with method to compute the area
using System;
class Rect {
public int width;
public int height;
public Rect(int w, int h) {
width = w;
height = h;
}
public int area() {
return width * height;
}
}
class UseRect {
public static void Main() {
Rect r1 = new Rect(4, 5);
Rect r2 = new Rect(7, 9);
Console.WriteLine("Area of r1: " + r1.area());
Console.WriteLine("Area of r2: " + r2.area());
}
}
Area of r1: 20 Area of r2: 63
Member Functions
using System;
class Point
{
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
// accessor functions
public int GetX() {
return(x);
}
public int GetY() {
return(y);
}
// variables now private
int x;
int y;
}
class MainClass
{
public static void Main()
{
Point myPoint = new Point(10, 15);
Console.WriteLine("myPoint.X {0}", myPoint.GetX());
Console.WriteLine("myPoint.Y {0}", myPoint.GetY());
}
}
myPoint.X 10 myPoint.Y 15
Nested methods
using System;
class MainClass
{
static void MethodA(int par1, int par2)
{
Console.WriteLine("Enter MethodA: {0}, {1}", par1, par2);
MethodB(11, 18);
Console.WriteLine("Exit MethodA");
}
static void MethodB(int par1, int par2)
{
Console.WriteLine("Enter MethodB: {0}, {1}", par1, par2);
Console.WriteLine("Exit MethodB");
}
static void Main()
{
MethodA(15, 30);
}
}
Enter MethodA: 15, 30 Enter MethodB: 11, 18 Exit MethodB Exit MethodA
Object Class Methods
using System;
public class Product {
public string make;
public string model;
public Product(string make, string model) {
this.make = make;
this.model = model;
}
public void Display() {
Console.WriteLine("make = " + make);
Console.WriteLine("model = " + model);
}
public static Product Copy(Product p) {
return (Product)p.MemberwiseClone();
}
}
class MainClass {
public static void Main() {
Console.WriteLine("Creating Product objects");
Product myProduct = new Product("Toyota", "MR2");
Product myOtherProduct = new Product("Porsche", "Boxter");
myProduct.Display();
myOtherProduct.Display();
Console.WriteLine("myProduct.ToString() = " + myProduct.ToString());
Console.WriteLine("myProduct.GetType() = " + myProduct.GetType());
Console.WriteLine("myProduct.GetHashCode() = " + myProduct.GetHashCode());
Console.WriteLine("Product.Equals(myProduct, myOtherProduct) = " + Product.Equals(myProduct, myOtherProduct));
Console.WriteLine("Product.ReferenceEquals(myProduct, myOtherProduct) = " + Product.ReferenceEquals(myProduct, myOtherProduct));
myProduct = myOtherProduct;
Console.WriteLine("Product.Equals(myProduct, myOtherProduct) = " + Product.Equals(myProduct, myOtherProduct));
Console.WriteLine("Product.ReferenceEquals(myProduct, myOtherProduct) = " + Product.ReferenceEquals(myProduct, myOtherProduct));
Product myOldProduct = Product.Copy(myProduct);
myOldProduct.Display();
}
}
Return a value from a member method
using System;
class Building {
public int area;
public int occupants;
// Return the area per person.
public int areaPerPerson() {
return area / occupants;
}
}
class MainClass {
public static void Main() {
Building house = new Building();
Building office = new Building();
int areaPP; // area per person
house.occupants = 4;
house.area = 2500;
office.occupants = 25;
office.area = 4200;
areaPP = house.areaPerPerson();
Console.WriteLine("house has:\n " +
house.occupants + " occupants\n " +
house.area + " total area\n " +
areaPP + " area per person");
Console.WriteLine();
// obtain area per person for office
areaPP = office.areaPerPerson();
Console.WriteLine("office has:\n " +
office.occupants + " occupants\n " +
office.area + " total area\n " +
areaPP + " area per person");
}
}
house has: 4 occupants 2500 total area 625 area per person office has: 25 occupants 4200 total area 168 area per person