using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Step37
{
internal class Program
{
static void Main(string[] args)
{
Marine marineA = new Marine();
marineA.Init("마린 A", 10);
marineA.damage = 2;
Marine marineB = new Marine();
marineB.Init("마린 B", 10);
marineB.damage = 2;
Medic medic = new Medic();
medic.healValue = 3;
marineB.Attack(marineA);
Console.WriteLine("마린 A의 체력 : {0}",marineA.GetHp());
Console.WriteLine("마린 B의 체력 : {0}",marineB.GetHp());
medic.Heal(marineA);
Console.WriteLine("마린 A의 체력 : {0}", marineA.GetHp());
Console.WriteLine("마린 B의 체력 : {0}", marineB.GetHp());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Step37
{
internal class TerranUnit : StarcraftUnit
{
protected int hp;
protected int maxHp;
public TerranUnit()
{
Console.WriteLine("TerranUnit 클래스 생성자");
}
public void Init(string name, int maxHp)
{
this.name = name;
this.maxHp = maxHp;
this.hp = this.maxHp;
}
public void Hit(int damage)
{
this.hp -= damage;
Console.WriteLine("{0}이 피해{1}를 받았습니다", this.name, damage);
}
public int GetHp()
{
return this.hp;
}
public void AddHp(int heal)
{
this.hp += heal;
Console.WriteLine("{0}의 체력이 회복 되었습니다", this.name);
if(this.hp >= this.maxHp)
{
this.hp = this.maxHp;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Step37
{
internal class StarcraftUnit
{
public string name;
//생성자
public StarcraftUnit()
{
Console.WriteLine("StarcraftUnit 생성자");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Step37
{
internal class Marine : TerranUnit
{
public int damage;
public Marine()
{
Console.WriteLine("Marine 클래스 생성자");
}
public void Attack(TerranUnit target)
{
target.Hit(this.damage);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Step37
{
internal class Medic : TerranUnit
{
public int healValue;
public Medic()
{
Console.WriteLine("Medic 클래스 생성자");
}
public void Heal(TerranUnit target)
{
target.AddHp(this.healValue);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Step36
{
internal class Program
{
static void Main(string[] args)
{
//Calculator calc = new Calculator();
//int result = calc.Add(1, 2);
//Console.WriteLine(result);
//result = calc.Add(1, 2, 3);
//Console.WriteLine(result);
//calc.Subtract(1.3f, 1.5f);
//calc.Subtract(5, 1.5f);
//calc.Subtract(10f, 5);
//calc.Multiple(1.5f, 2);
//calc.Multiple(2, 1.5f);
//생성자 오버로딩
Hero hong = new Hero();
Hero lim = new Hero("임꺽정");
Hero jang = new Hero("장길산", 3, 10);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Step36
{
internal class Calculator
{
//클래스가 생성되면 기본 생성자를 만들자
public Calculator()
{
}
//메서드 오버로딩
public int Add(int a, int b)
{
int result = a + b;
return result;
}
//다른 매개변수의 수
public int Add(int a, int b, int c)
{
int result = a + b + c;
return result;
}
public int Subtract(int a, int b)
{
int result = a - b;
return result;
}
//다른 타입의 매개변수
public int Subtract(float a, float b)
{
int result = Convert.ToInt32(a - b);
return result;
}
public int Subtract(int a, float b)
{
int result = a - (int)b;
return result;
}
//서로다른 매개변수의 순서 변경
public int Multiple(int a, float b)
{
int result = a * (int)b;
return result;
}
public int Multiple(float a, int b)
{
int result = (int)a * b;
return result;
}
//반환타입이 다른것은 오버로딩 불가능
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Step36
{
internal class Hero
{
//생성자
public Hero()
{
Console.WriteLine("매개변수가 없는 기본 생성자");
}
//생성자 오버로딩
public Hero(string anme)
{
Console.WriteLine("매개변수가 1개 있는 생성자");
}
//생성자 오버로딩
public Hero(string name, int damage, int maxHp)
{
Console.WriteLine("매개변수가 3개 있는 생성자");
}
}
}