Notice
Recent Posts
Recent Comments
Link
거의 알고리즘 일기장
클래스의 상속과 초기화 예제 + virtual까지의 예제 본문
상속과 초기화
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using System.Diagnostics.CodeAnalysis;
namespace ConsoleApp1
{
class Car
{
public string color;
public float speed;
public Car()
{ }
public Car (string color_in ="red", float speed_in = 0.0f)
{
color = color_in;
speed = speed_in;
}
public void print()
{
Console.WriteLine("컬러 :{0}, 스피드 : {1}", color, speed);
}
}
class SportsCar : Car
{
public string tubo;
public SportsCar(string color_in = "red", float speed_in = 0.0f, string tubo_in ="good") : base(color_in, speed_in)
{
this.tubo = tubo_in;
}
public void print()
{
Console.WriteLine("컬러 :{0}, 스피드 : {1}, tubo : {2}", color, speed, tubo);
}
}
class Program
{
static void Main(string[] args)
{
SportsCar sportsCar = new SportsCar();
sportsCar.print();
}
}
}
virtual까지를 이용한 간단예제
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using System.Diagnostics.CodeAnalysis;
namespace ConsoleApp1
{
class Car
{
public string color;
public float speed;
public Car(string color_in = "red", float speed_in = 0.0f)
{
color = color_in;
speed = speed_in;
}
public virtual void booster(float addSpeed)
{
speed += addSpeed;
}
public virtual void SetColor(string color_in)
{
color = color_in;
}
public virtual void SetSpeed(float speed_in)
{
speed = speed_in;
}
public virtual void print()
{
Console.WriteLine("일반소형차ㅠㅠ");
Console.WriteLine("컬러 :{0},\n스피드 : {1}\n", color, speed);
}
}
class SportsCar : Car
{
public string tubo;
public SportsCar(string color_in = "red", float speed_in = 0.0f, string tubo_in ="good") : base(color_in, speed_in)
{
this.tubo = tubo_in;
}
public override void print()
{
Console.WriteLine("스포츠카!!");
Console.WriteLine("컬러 : {0},\n스피드 : {1},\ntubo : {2}\n", color, speed, tubo);
}
}
class Program
{
//다형성을 이용한 함수
static void print(Car car)
{
car.print();
}
static void Main(string[] args)
{
SportsCar sportsCar = new SportsCar();
sportsCar.SetSpeed(speed_in: 15.0f);
sportsCar.SetColor(color_in: "yellow");
sportsCar.booster(addSpeed: 2.0f);
sportsCar.print();
Car normalCar = new Car();
normalCar.SetSpeed(speed_in: 5.0f);
normalCar.SetColor(color_in: "blue");
normalCar.print();
//다형성 이용
List<Car> cars = new List<Car>();
cars.Add(sportsCar);
cars.Add(normalCar);
Console.WriteLine("리스트에 넣었던 값들을 출력!");
foreach (var car in cars)
car.print();
Console.WriteLine("print함수를 이용한 출력");
foreach (var car in cars)
print(car);
}
}
}
반응형
'c# 문법' 카테고리의 다른 글
ref, out 예제 (0) | 2020.05.14 |
---|---|
클래스의 private의 접근도 편하게 _ get, set (0) | 2020.05.13 |
문자열 위치찾기 (0) | 2020.05.12 |
박싱과 언박싱 예제 (0) | 2020.05.11 |
class를 이용한 간단 예제 2개 (0) | 2020.05.11 |
Comments