基礎引數
interface,abstract,virtual,override
然後實現了這樣的效果
同一個方法,呼叫不同的實現類,那實現不一樣的結果
具體程式碼
public interface IStatsBase { string UserSpeak(string content); string UserHandel(string content); } public interface IUserStatsBase { string UserLook(string content); } public abstract class UserStatsBase : IUserStatsBase, IStatsBase { protected abstract string Name { get; } public virtual string UserHandel(string content) { return $"{Name}做:{content}"; } public virtual string UserSpeak(string content) { return $"{Name}說:{content}"; } public virtual string UserLook(string content) { return $"{Name}看:{content}"; } } public class XiaoHongStats : UserStatsBase { protected override string Name => "小紅"; public override string UserHandel(string content) { StringBuilder str = new StringBuilder(); str.AppendLine(UserSpeak("我那去掃地")); str.AppendLine("去掃地了"); return str.ToString(); } } public class ZhangSangStats : UserStatsBase { protected override string Name => "張三"; public override string UserSpeak(string content) { return $"{Name}說:{content},並且帶著大家去做"; } }