根据一下代码画出uml类图
part1
using System;
namespace xuqinan {
public abstract class Animal
{
public abstract void cry();
public abstract string getAnimalName();
}
public class Simulator
{
public void playSound(Animal animal)
{
Console.WriteLine("现在播放" +animal.getAnimalName() + "的声音"); animal.cry();
}
}
public class Dog : Animal
{
public override void cry()
{
Console.WriteLine("汪汪汪");
}
public override string getAnimalName()
{ return "旺财";
}
}
public class Cat : Animal
{
public override void cry()
{
Console.WriteLine("喵喵喵");
}
public override string getAnimalName()
{
return "Tom";
}
}
public class Example
{
public static void main(string[] args)
{
Simulator simulator = new Simulator(); simulator.playSound(new Dog()); simulator.playSound(new Cat());
}
}
}
类图说明:
- Dog/Cat 与 Animal 存在泛化关系
- Simulator 依赖于 Animal
- Example 依赖于 Simulator/Dog/Cat
part2
using System;
using System.Collections;
namespace xuqinan
{
public abstract class Song
{
protected string title;
protected int duration;
public Song(string t)
{
title = t;
}
public abstract void play();
}
//MP3类
public class MP3 : Song
{
public MP3(string t):base(t) {}
public override void play()
{
Console.WriteLine("MP3 dididi");
}
}
//Wav歌曲类
public class Wav : Song
{
Wav(string t) : base(t) {}
public override void play()
{
Console.WriteLine("Wav dadada");
}
}
//歌单类
public class playList
{
private ArrayList songs; public playList()
{
songs = new ArrayList();
}
public void add(Song t)
{
songs.Add(t);
}
public void playAll()
{
foreach(Song song in songs)
{
song.play();
}
}
public void randomPlayAll() { }
}
}
类图说明:
- MP3/Wav与Song存在泛化关系,即继承关系。
- Song(歌曲类)和PlayList(歌单类)存在关联关系,并且是一对多的关系,即聚合关系。
点击查看类图