>  기사  >  백엔드 개발  >  C#의 일반적인 메서드에 대한 자세한 소개

C#의 일반적인 메서드에 대한 자세한 소개

Y2J
Y2J원래의
2017-04-21 14:15:141369검색

이 글에서는 C#의 메소드에 대한 자세한 설명을 소개합니다. 필요한 친구들은

을 참고하세요. 1. 메소드가 여러 매개변수를 반환하도록 합니다

1.1 메소드 외부에 변수를 정의하여 결과를 저장합니다

코드는 다음과 같습니다.

using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 namespace Method
 {
     class Program
     {
         public static int quotient;
         public static int remainder;
         public static void pide(int x, int y)
         {
             quotient = x / y;
             remainder = x % y;
         }
         static void Main(string[] args)
         {
             Program.pide(6,9);
             Console.WriteLine(Program.quotient);
             Console.WriteLine(Program.remainder);
             Console.ReadKey();
         }
     }
 }

1.2 출력 및 입력 매개변수 사용

코드는 다음과 같습니다.

using System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace Method{
    class Program
    {
       public static void pide(int x, int y, out int quotient, out int remainder) 
       {
            quotient = x / y;
            remainder = x % y;
        }
        static void Main(string[] args)
        {
            int quotient, remainder;
            pide(6,9,out quotient,out remainder);
            Console.WriteLine("{0} {1}",quotient,remainder);
            Console.ReadKey();
        }
    }
}

2. 메소드 오버로딩

메서드 오버로딩은 구조적 프로그래밍 기능의 중요한 객체 지향 확장입니다

오버로딩을 구성하는 메소드에는 다음 특징:

(1) 메소드 이름이 동일함

(2) 메소드 매개변수 목록이 다름

위의 두 번째 사항을 판단하는 기준은 세 가지입니다. 포인트 중 하나라도 충족되면 메소드 매개변수 목록이 다른 것으로 간주될 수 있습니다.

(1) 메소드 매개변수 수가 다릅니다.

(2) 메소드에는 다음이 있습니다. 매개변수 개수는 같지만 매개변수 유형이 다릅니다.

(3) 메소드는 매개변수 개수와 매개변수 유형이 동일하지만 매개변수 유형이 나타나는 순서가 다릅니다.

메서드 반환값 유형이 아니라는 점에 유의하세요. 메소드 오버로딩 상태의 판단.

3. 메소드 숨기기

코드는 다음과 같습니다.

namespace 方法隐藏
 {
     class Program
     {
         static void Main(string[] args)
         {
             Parent p = new Child();
             p.show();
             Console.ReadKey();
         }
     }
     class Parent
     {
         public void show()
         {
             Console.Write("父类方法");
         }
     }
     class Child : Parent
     {
         public new void show()
         {
             Console.Write("子类方法");
         }
     }
 }

코드는 다음과 같습니다.

namespace 方法隐藏
{
    class Program
    {
        static void Main(string[] args)
        {
            Parent.show();
            Console.ReadKey();
            Child.show();//父类方法
        }
    }
    class Parent
    {
        public static void show()
        {
            Console.Write("父类方法");
        }
    }
    class Child : Parent
    {
        public static new void show()
        {
            Console.Write("子类方法");
        }
    }
}


회원 저장 권한이 지정되지 않는다는 전제하에 회원은 비공개입니다.

코드는 다음과 같습니다.

namespace 方法隐藏 {
     class Program
     {
         static void Main(string[] args)
         {
             Parent p1= new Parent();
             Parent p2 = new Child();
             p1.show();//父类方法
             p2.show();//父类方法
             ((Child)p2).show();//父类方法
             Console.ReadKey();
         }
     }
     class Parent
     {
         public  void show()
         {
             Console.WriteLine("父类方法");
         }
     }
     class Child : Parent
     {
           new void show()
         {
             Console.WriteLine("子类方法");
         }
     }
 }

4. 메소드 재작성 및 가상 메소드 호출

코드 다음과 같습니다:

namespace 方法重写
 {
     class Program
     {
         static void Main(string[] args)
         {
             Parent p1 = new Parent();
             Parent p2 = new Child();
             p1.show();
             p2.show();
             ((Parent)p2).show();//子类方法
             Console.ReadKey();
         }
     }
     class Parent
     {
         public virtual void show()
         {
             Console.WriteLine("父类方法");
         }
     }
     class Child:Parent
     {
         public override void show()
         {
             Console.WriteLine("子类方法");
         }
     }
 }

위 내용은 C#의 일반적인 메서드에 대한 자세한 소개의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.