C 언어
이 문서에서는 예제를 통해 C#의 메서드 오버로딩에 대해 알아봅니다.
C#에서는 이름은 같지만 매개변수의 번호, 유형 및 순서가 다른 두 개 이상의 메서드가 있을 수 있으며 이를 메서드 오버로딩이라고 합니다. 예:
void display() { ... }
void display(int a) { ... }
float display(double a) { ... }
float display(int a, float b) { ... }
여기서 display() 메서드가 오버로드되었습니다. 이 메서드는 이름은 같지만 인수가 다릅니다.
참고 :위 메소드의 리턴 유형이 동일하지 않습니다. 메서드 오버로딩이 반환 형식과 관련이 없기 때문입니다. 오버로드된 메서드는 반환 유형이 같거나 다를 수 있지만 매개변수는 달라야 합니다.
<시간>다음과 같은 방법으로 메서드 오버로딩을 수행할 수 있습니다.
메소드의 매개변수 수가 다른 경우 메소드를 오버로드할 수 있습니다.
void display(int a) {
...
}
...
void display(int a, int b) {
...
}
여기에서는 동일한 이름을 가진 클래스에 두 개의 메소드가 있습니다 - display() . 메소드의 매개변수 수가 다르기 때문에 동일한 이름을 가진 메소드가 두 개 이상 있을 수 있습니다. 예를 들어,
using System;
namespace MethodOverload {
class Program {
// method with one parameter
void display(int a) {
Console.WriteLine("Arguments: " + a);
}
// method with two parameters
void display(int a, int b) {
Console.WriteLine("Arguments: " + a + " and " + b);
}
static void Main(string[] args) {
Program p1 = new Program();
p1.display(100);
p1.display(100, 200);
Console.ReadLine();
}
}
}
출력
Arguments: 100 Arguments: 100 and 200
위의 예에서 우리는 display()를 오버로드했습니다. 방법:
메소드 호출 시 전달된 인수의 수에 따라 해당 메소드가 호출됩니다.
p1.display(100)
- 단일 매개변수로 메소드 호출p1.display(100, 200)
- 두 개의 매개변수를 사용하여 메서드를 호출합니다.
void display(int a) {
...
}
...
void display(string b) {
...
}
여기에는 display()의 두 가지 메서드가 있습니다. 같은 수의 매개변수로 둘 이상의 display()를 가질 수 있습니다. 메소드에서 매개변수의 데이터 유형이 다르기 때문에 동일한 수의 매개변수를 갖는 메소드입니다. 예를 들어,
using System;
namespace MethodOverload {
class Program {
// method with int parameter
void display(int a) {
Console.WriteLine("int type: " + a);
}
// method with string parameter
void display(string b) {
Console.WriteLine("string type: " + b);
}
static void Main(string[] args) {
Program p1 = new Program();
p1.display(100);
p1.display("Programiz");
Console.ReadLine();
}
}
}
출력
int type: 100 string type: Programiz
위의 프로그램에서 display()를 오버로드했습니다. 다양한 유형의 매개변수가 있는 메소드
메소드 호출 시 전달된 인수의 유형에 따라 해당 메소드가 호출됩니다.
p1.display(100)
- int
메서드를 호출합니다. 유형 매개변수p1.display("Programiz")
- string
로 메소드 호출 유형 매개변수
void display(int a, string b) {
...
}
...
void display(string b, int a) {
...
}
여기에는 display()의 두 가지 메서드가 있습니다. . 둘 이상의 display()를 가질 수 있습니다. 메소드에서 매개변수의 데이터 유형 순서가 다르기 때문에 동일한 수와 유형의 매개변수를 갖는 메소드입니다. 예를 들어,
using System;
namespace MethodOverload {
class Program {
// method with int and string parameters
void display(int a, string b) {
Console.WriteLine("int: " + a);
Console.WriteLine("string: " + b);
}
// method with string andint parameter
void display(string b, int a) {
Console.WriteLine("string: " + b);
Console.WriteLine("int: " + a);
}
static void Main(string[] args) {
Program p1 = new Program();
p1.display(100, "Programming");
p1.display("Programiz", 400);
Console.ReadLine();
}
}
}
출력
int: 100 string: Programming string: Programiz int: 400
위의 프로그램에서 display()를 오버로드했습니다. 매개변수 순서가 다른 메소드
메서드 호출 시 전달된 인수의 순서에 따라 해당 메서드가 호출됩니다.
p1.display(100, "Programming")
- int
메서드를 호출합니다. 및 string
매개변수 각각p1.display("Programiz", 400)
- string
로 메소드 호출 및 int
매개변수 각각C 언어
자바 다형성 이 자습서에서는 예제를 통해 Java 다형성 및 구현에 대해 알아봅니다. 다형성은 객체 지향 프로그래밍의 중요한 개념입니다. 단순히 하나 이상의 형식을 의미합니다. 즉, 동일한 엔터티(메서드 또는 연산자 또는 개체)가 다른 시나리오에서 다른 작업을 수행할 수 있습니다. 예:자바 다형성 class Polygon { // method to render a shape public void render() { System.out.println(Rendering Polygon...); } } clas
자바 반복자 인터페이스 이 튜토리얼에서는 예제를 통해 Java Iterator 인터페이스에 대해 알아볼 것입니다. Iterator Java 컬렉션 프레임워크의 인터페이스를 사용하면 컬렉션의 요소에 액세스할 수 있습니다. ListIterator 하위 인터페이스가 있습니다. . 모든 Java 컬렉션에는 iterator()이 포함됩니다. 방법. 이 메서드는 컬렉션 요소를 반복하는 데 사용되는 iterator의 인스턴스를 반환합니다. 반복자 메소드 Iterator 인터페이스는 컬렉션 요소에 대해 다양한 작업을 수행하는 데 사용