C#でのシャドウイングとのオーバーライド
オーバーライド
オーバーライドでは、サブクラスタイプに固有の動作を定義できます。つまり、サブクラスはその要件に基づいて親クラスのメソッドを実装できます。
オーバーライドを実装する抽象クラスの例を見てみましょう-
例
using System;
namespace PolymorphismApplication {
abstract class Shape {
public abstract int area();
}
class Rectangle: Shape {
private int length;
private int width;
public Rectangle( int a = 0, int b = 0) {
length = a;
width = b;
}
public override int area () {
Console.WriteLine("Rectangle class area :");
return (width * length);
}
}
class RectangleTester {
static void Main(string[] args) {
Rectangle r = new Rectangle(10, 7);
double a = r.area();
Console.WriteLine("Area: {0}",a);
Console.ReadKey();
}
}
} シャドウイング
シャドウイングは、メソッドの非表示とも呼ばれます。親クラスのメソッドは、シャドウイングでoverrideキーワードを使用せずに、子クラスで使用できます。子クラスには、同じ関数の独自のバージョンがあります。
newキーワードを使用して、シャドウイングを実行し、基本クラス関数の独自のバージョンを作成します。
例を見てみましょう-
例
using System;
using System.Collections.Generic;
class Demo {
public class Parent {
public string Display() {
return "Parent Class!";
}
}
public class Child : Parent {
public new string Display() {
return "Child Class!";
}
}
static void Main(String[] args) {
Child child = new Child();
Console.WriteLine(child.Display());
}
} -
C#のコンソールクラス
C#のConsoleクラスは、コンソールアプリケーションの標準の入力、出力、およびエラーストリームを表すために使用されます。 C#のコンソールクラスプロパティの例をいくつか見てみましょう- Console.CursorLeftプロパティ C#でコンソールのCursorLeftを変更するには、Console.CursorLeftプロパティを使用します。 例 例を見てみましょう- using System; class Demo { public static void Main (string[] args) { Cons
-
Javaでオーバーライドするメソッド
オーバーライドとは、サブクラスタイプに固有の動作を定義する機能です。つまり、サブクラスは、その要件に基づいて親クラスのメソッドを実装できます。 オブジェクト指向の用語では、オーバーライドとは、既存のメソッドの機能をオーバーライドすることを意味します。 例 例を見てみましょう。 class Animal { public void move() { System.out.println("Animals can move"); } } class Dog extends Ani