C#教科書を身につける.クラスを継承に拡張
12194 ワード
https://www.youtube.com/watch?v=kkKLpwowinc&list=PLO56HZSjrPTB4NxAsEP8HRk6YKBDLbp7m&index=66
親クラスの機能を再利用、拡張、修正し、子クラス とする.
親および子 親 スーパークラス、基本クラス、基本クラス クラス は、特定のクラスに継承されます.汎用機能セット サブクラス サブクラス、派生クラス は、特定のフォーマットの継承を付与タイプ である.
: に継承
3.BaseクラスとSubクラス
オブジェクトクラスは始祖トップクラスです は、基本的にすべてのクラスオブジェクトクラスを継承します. class ClassName : Object 5.親タイプ変数に子クラスのオブジェクトを割り当てる
this,this()はthis を表す基地、base()はsuper
sealキーワード を使用
8.抽象クラス
の保護キーワード を使用
10.基本クラスのメンバーを非表示にする

1.継承クラス
01.相続:親子
2.親と子
using System;
using static System.Console;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace testProject
{
// 자동차 관련 부모 클래스
class Car
{
public void Go() => WriteLine("달리다");
}
// Car의 자식 클래스
class Benz : Car
{
}
class Tesla : Car
{
}
class Program : Object
{
static void Main(string[] args)
{
(new Benz()).Go();
(new Tesla()).Go();
}
}
}

3.BaseクラスとSubクラス using System;
using static System.Console;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace testProject
{
// 열거형
public enum CarType { EV, ICEV }
class Car
{
public void Go() => WriteLine("달리다");
}
// Car의 자식 클래스
class Benz : Car
{
public CarType Style { get; private set; }
public Benz()
{
Style = CarType.ICEV;
}
public Benz(CarType carType)
{
Style = carType;
}
}
class Tesla : Car
{
public CarType Style { get; set; }
public Tesla()
{
Style = CarType.EV;
}
public Tesla(CarType carType)
{
Style = carType;
}
}
class Program : Object
{
static void Main(string[] args)
{
Benz benz = new Benz();
benz.Go();
WriteLine(benz.Style);
Tesla tesla = new Tesla();
tesla.Go();
WriteLine(tesla.Style);
}
}
}
4.継承対象クラス
using System;
using static System.Console;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace testProject
{
// 열거형
public enum CarType { EV, ICEV }
class Car
{
public void Go() => WriteLine("달리다");
}
// Car의 자식 클래스
class Benz : Car
{
public CarType Style { get; private set; }
public Benz()
{
Style = CarType.ICEV;
}
public Benz(CarType carType)
{
Style = carType;
}
}
class Tesla : Car
{
public CarType Style { get; set; }
public Tesla()
{
Style = CarType.EV;
}
public Tesla(CarType carType)
{
Style = carType;
}
}
class Program : Object
{
static void Main(string[] args)
{
Benz benz = new Benz();
benz.Go();
WriteLine(benz.Style);
Tesla tesla = new Tesla();
tesla.Go();
WriteLine(tesla.Style);
}
}
}
5.親タイプ変数に子クラスのオブジェクトを割り当てる
6.thisとthis()およびbaseとbase()
using System;
using static System.Console;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace testProject
{
// 열거형
public enum CarType { EV, ICEV }
// 추상 클래스
public abstract class CarBase
{
public abstract void Left(); // 추상 메서드, 본문X, 시그니처O => 표준(강제) => 인터페이스
public void Back() => WriteLine("후진하다");
}
class Car : CarBase
{
public CarType Style { get; private set; }
public void Go() => WriteLine("달리다");
// 추상화 재정의
public override void Left() => WriteLine("좌회전하다");
public Car(CarType carType)
{
Style = carType;
}
}
// Car의 자식 클래스
class Benz : Car
{
public Benz() : this(CarType.ICEV){}
public Benz(CarType carType) : base(carType) { }
}
class Tesla : Car
{
public Tesla() : this(CarType.EV) { }
public Tesla(CarType carType) : base(carType) { }
}
class Program : Object
{
static void Main(string[] args)
{
Benz benz = new Benz();
benz.Go();
benz.Back();
benz.Left();
WriteLine(benz.Style);
Tesla tesla = new Tesla();
tesla.Go();
tesla.Back();
tesla.Left();
WriteLine(tesla.Style);
}
}
}



7.パッケージクラス(継承パッケージ)
using System;
using static System.Console;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace testProject
{
// 열거형
public enum CarType { EV, ICEV }
// 추상 클래스
public abstract class CarBase
{
public abstract void Left(); // 추상 메서드, 본문X, 시그니처O => 표준(강제) => 인터페이스
public void Back() => WriteLine("후진하다");
}
class Car : CarBase
{
public CarType Style { get; private set; }
public void Go() => WriteLine("달리다");
// 추상화 재정의
public override void Left() => WriteLine("좌회전하다");
public Car(CarType carType)
{
Style = carType;
}
}
// Car의 자식 클래스
class Benz : Car
{
public Benz() : this(CarType.ICEV){}
public Benz(CarType carType) : base(carType) { }
}
class Tesla : Car
{
public Tesla() : this(CarType.EV) { }
public Tesla(CarType carType) : base(carType) { }
}
// 봉인 클래스(최종)
sealed class Future : Car
{
public Future() : this(CarType.EV) { }
public Future(CarType carType) : base(carType) { }
public new void Go()
{
base.Go();
WriteLine("날다");
}
}
class OtherFuture : Future
{
}
class Program : Object
{
static void Main(string[] args)
{
Benz benz = new Benz();
benz.Go();
benz.Back();
benz.Left();
WriteLine(benz.Style);
Tesla tesla = new Tesla();
tesla.Go();
tesla.Back();
tesla.Left();
WriteLine(tesla.Style);
Future future = new Future();
future.Go();
}
}
}

8.抽象クラス using System;
using static System.Console;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace testProject
{
// 열거형
public enum CarType { EV, ICEV }
// 추상 클래스
public abstract class CarBase
{
public abstract void Left(); // 추상 메서드, 본문X, 시그니처O => 표준(강제) => 인터페이스
public void Back() => WriteLine("후진하다");
}
class Car : CarBase
{
public void Go() => WriteLine("달리다");
// 추상화 재정의
public override void Left() => WriteLine("좌회전하다");
}
// Car의 자식 클래스
class Benz : Car
{
public CarType Style { get; private set; }
public Benz()
{
Style = CarType.ICEV;
}
public Benz(CarType carType)
{
Style = carType;
}
}
class Tesla : Car
{
public CarType Style { get; set; }
public Tesla()
{
Style = CarType.EV;
}
public Tesla(CarType carType)
{
Style = carType;
}
}
class Program : Object
{
static void Main(string[] args)
{
Benz benz = new Benz();
benz.Go();
benz.Back();
benz.Left();
WriteLine(benz.Style);
Tesla tesla = new Tesla();
tesla.Go();
tesla.Back();
tesla.Left();
WriteLine(tesla.Style);
}
}
}
9.メンバーのみをサブクラスに継承
using System;
using static System.Console;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace testProject
{
// 열거형
public enum CarType { EV, ICEV }
// 추상 클래스
public abstract class CarBase
{
public abstract void Left(); // 추상 메서드, 본문X, 시그니처O => 표준(강제) => 인터페이스
public void Back() => WriteLine("후진하다");
}
class Car : CarBase
{
public void Go() => WriteLine("달리다");
// 추상화 재정의
public override void Left() => WriteLine("좌회전하다");
}
// Car의 자식 클래스
class Benz : Car
{
public CarType Style { get; private set; }
public Benz()
{
Style = CarType.ICEV;
}
public Benz(CarType carType)
{
Style = carType;
}
}
class Tesla : Car
{
public CarType Style { get; set; }
public Tesla()
{
Style = CarType.EV;
}
public Tesla(CarType carType)
{
Style = carType;
}
}
class Program : Object
{
static void Main(string[] args)
{
Benz benz = new Benz();
benz.Go();
benz.Back();
benz.Left();
WriteLine(benz.Style);
Tesla tesla = new Tesla();
tesla.Go();
tesla.Back();
tesla.Left();
WriteLine(tesla.Style);
}
}
}
using System;
using static System.Console;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace testProject
{
// 열거형
public enum CarType { EV, ICEV }
// 추상 클래스
public abstract class CarBase
{
public abstract void Left(); // 추상 메서드, 본문X, 시그니처O => 표준(강제) => 인터페이스
public void Back() => WriteLine("후진하다");
protected string LeftMessage { get; private set; } = "좌회전하다";
}
class Car : CarBase
{
public CarType Style { get; private set; }
public void Go() => WriteLine("달리다");
// 추상화 재정의
public override void Left() => WriteLine(LeftMessage);
public Car(CarType carType)
{
Style = carType;
}
}
// Car의 자식 클래스
class Benz : Car
{
public Benz() : this(CarType.ICEV){}
public Benz(CarType carType) : base(carType) { }
}
class Tesla : Car
{
public Tesla() : this(CarType.EV) { }
public Tesla(CarType carType) : base(carType) { }
}
// 봉인 클래스(최종)
sealed class Future : Car
{
public Future() : this(CarType.EV) { }
public Future(CarType carType) : base(carType) { }
public new void Go()
{
base.Go();
WriteLine("날다");
}
}
class Program : Object
{
static void Main(string[] args)
{
Benz benz = new Benz();
benz.Go();
benz.Back();
benz.Left();
WriteLine(benz.Style);
Tesla tesla = new Tesla();
tesla.Go();
tesla.Back();
tesla.Left();
WriteLine(tesla.Style);
Future future = new Future();
future.Go();
}
}
}

10.基本クラスのメンバーを非表示にする
11.親メソッドの再定義(new)
親から継承するメソッドを再定義する場合は、新しいキーワードを使用します.using System;
using static System.Console;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace testProject
{
// 열거형
public enum CarType { EV, ICEV }
// 추상 클래스
public abstract class CarBase
{
public abstract void Left(); // 추상 메서드, 본문X, 시그니처O => 표준(강제) => 인터페이스
public void Back() => WriteLine("후진하다");
}
class Car : CarBase
{
public CarType Style { get; private set; }
public void Go() => WriteLine("달리다");
// 추상화 재정의
public override void Left() => WriteLine("좌회전하다");
public Car(CarType carType)
{
Style = carType;
}
}
// Car의 자식 클래스
class Benz : Car
{
public Benz() : this(CarType.ICEV){}
public Benz(CarType carType) : base(carType) { }
}
class Tesla : Car
{
public Tesla() : this(CarType.EV) { }
public Tesla(CarType carType) : base(carType) { }
}
class Future : Car
{
public Future() : this(CarType.EV) { }
public Future(CarType carType) : base(carType) { }
public new void Go()
{
base.Go();
WriteLine("날다");
}
}
class Program : Object
{
static void Main(string[] args)
{
Benz benz = new Benz();
benz.Go();
benz.Back();
benz.Left();
WriteLine(benz.Style);
Tesla tesla = new Tesla();
tesla.Go();
tesla.Back();
tesla.Left();
WriteLine(tesla.Style);
Future future = new Future();
future.Go();
}
}
}
Reference
この問題について(C#教科書を身につける.クラスを継承に拡張), 我々は、より多くの情報をここで見つけました
https://velog.io/@ansalstmd/C교과서-마스터하기-32.-상속으로-클래스-확장하기
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
親から継承するメソッドを再定義する場合は、新しいキーワードを使用します.
using System;
using static System.Console;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace testProject
{
// 열거형
public enum CarType { EV, ICEV }
// 추상 클래스
public abstract class CarBase
{
public abstract void Left(); // 추상 메서드, 본문X, 시그니처O => 표준(강제) => 인터페이스
public void Back() => WriteLine("후진하다");
}
class Car : CarBase
{
public CarType Style { get; private set; }
public void Go() => WriteLine("달리다");
// 추상화 재정의
public override void Left() => WriteLine("좌회전하다");
public Car(CarType carType)
{
Style = carType;
}
}
// Car의 자식 클래스
class Benz : Car
{
public Benz() : this(CarType.ICEV){}
public Benz(CarType carType) : base(carType) { }
}
class Tesla : Car
{
public Tesla() : this(CarType.EV) { }
public Tesla(CarType carType) : base(carType) { }
}
class Future : Car
{
public Future() : this(CarType.EV) { }
public Future(CarType carType) : base(carType) { }
public new void Go()
{
base.Go();
WriteLine("날다");
}
}
class Program : Object
{
static void Main(string[] args)
{
Benz benz = new Benz();
benz.Go();
benz.Back();
benz.Left();
WriteLine(benz.Style);
Tesla tesla = new Tesla();
tesla.Go();
tesla.Back();
tesla.Left();
WriteLine(tesla.Style);
Future future = new Future();
future.Go();
}
}
}


Reference
この問題について(C#教科書を身につける.クラスを継承に拡張), 我々は、より多くの情報をここで見つけました https://velog.io/@ansalstmd/C교과서-마스터하기-32.-상속으로-클래스-확장하기テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol