By GokiSoft.com|
15:27 27/05/2020|
C Sharp
[Share Code] Hướng dẫn interface trong C# - Lập trình C# - Lập trình C Sharp
[Share Code] Hướng dẫn interface trong C# - Lập trình C# - Lập trình C Sharp
#Cat.cs
using System;
namespace Lession6
{
public class Cat : Animal
{
public Cat()
{
}
public override void ShowSound()
{
Console.WriteLine("Meo ... Meo ...");
}
}
}
#Citizen.cs
using System;
namespace Lession6
{
public class Citizen : ISound
{
public string Name { get; set; }
public int Age { get; set; }
public Citizen()
{
}
public void ShowSound() {
Console.WriteLine("Am thanh tu nguoi");
}
}
}
#Device.cs
using System;
namespace Lession6
{
public class Device : ISound
{
public string Name { get; set; }
public float Price { get; set; }
public Device()
{
}
public void ShowSound() {
Console.WriteLine("Hien thi am thanh thiet bi");
}
}
}
#Dog.cs
using System;
namespace Lession6
{
public class Dog : Animal
{
public Dog()
{
}
public override void ShowSound()
{
Console.WriteLine("Go ... Go ...");
}
}
}
#ISound.cs
using System;
namespace Lession6
{
public interface ISound
{
void ShowSound();
}
}
#Animal.cs
using System;
namespace Lession6
{
public abstract class Animal : ISound
{
public string Name { get; set; }
public string Foodtype { get; set; }
public Animal()
{
}
public abstract void ShowSound();
}
}
#Program.cs
using System;
using System.Collections.Generic;
namespace Lession6
{
class Program
{
static void Main(string[] args)
{
List<ISound> sounds = new List<ISound>();
sounds.Add(new Device());
sounds.Add(new Device());
Device device = new Device();
sounds.Add(device);
sounds.Add(new Citizen());
sounds.Add(new Citizen());
sounds.Add(new Citizen());
sounds.Add(new Dog());
sounds.Add(new Dog());
sounds.Add(new Cat());
sounds.Add(new Cat());
foreach(ISound s in sounds) {
s.ShowSound();
}
}
}
}
namespace Abc {
class Test {
}
class Test1 {
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)