Objektum példa C Sharp nyelven
Innen: Programozás Wiki
Ugrás a navigációhozUgrás a kereséshezC# nyelven
Track.cs:
using System;
namespace Teszt
{
public class Track
{
public string Title { get; private set; }
public int Length { get; private set; }
public Track(string title, int length)
{
Title = title;
Length = length;
}
}
}
CD.cs:
using System;
namespace Teszt
{
public abstract class CD
{
public abstract int Capacity { get; protected set; }
public abstract int Allocated { get; }
}
}
MusicCD.cs:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Teszt
{
public class MusicCD : CD
{
public List<Track> Tracks { get; set; }
public override int Capacity { get; protected set; }
public override int Allocated
{
get { return Tracks.Sum(t => t.Length); }
}
public MusicCD(int capacity)
{
Capacity = capacity;
Tracks = new List<Track>();
}
public void PrintContent(TextWriter textWriter)
{
StringBuilder sb = new StringBuilder();
Tracks.ForEach(t =>
{
sb.AppendLine(string.Format("{0} - {1}", t.Title, t.Length));
});
textWriter.Write(sb.ToString());
}
}
}
És a használatuk, Program.cs:
using System;
namespace Teszt
{
class Program
{
static void Main(string[] args)
{
MusicCD cd = new MusicCD(4800);
cd.Tracks.Add(new Track("Pledge of Allegiance", 299));
cd.Tracks.Add(new Track("Lullaby for the New World Order", 232));
cd.Tracks.Add(new Track("Weapon", 358));
cd.Tracks.Add(new Track("In a World Called Catastrophe", 357));
cd.Tracks.Add(new Track("Avalanche", 446));
cd.Tracks.Add(new Track("21st Century Living", 190));
cd.PrintContent(Console.Out);
Console.ReadKey();
}
}
}