0727 综合作业#
来源:
D:\study\C#1\stu0727\0727作业
作业 1:抽象类 + 虚方法 — 动物叫声#
abstract class Animal
{
public string Name { get; set; }
public int MaxAge { get; set; }
int nowage;
// 属性 set 中包含验证逻辑
public int NowAge
{
get => nowage;
set
{
if (value > MaxAge)
throw new Exception("年龄超出最大值");
nowage = value;
}
}
public string Sex { get; set; }
// 抽象方法 — 子类必须实现
public abstract void Speak();
public Random r = new Random();
public Animal(int maxage)
{
MaxAge = maxage;
NowAge = r.Next(0, MaxAge + 1); // 随机年龄
}
}
class Dog : Animal
{
public Dog() : this(20) { }
public Dog(int max) : base(max) { }
public override void Speak()
{
Console.WriteLine("汪汪汪");
}
}
class Cat : Animal
{
public Cat() : this(10) { }
public Cat(int max) : base(max) { }
public override void Speak()
{
Console.WriteLine("喵喵喵");
}
}
使用:
Dog dog = new Dog(20);
Console.WriteLine(dog.NowAge + "岁");
dog.Speak(); // → "汪汪汪"
Cat cat = new Cat(20);
cat.Speak(); // → "喵喵喵"
作业 2:构造函数重载 — 面积计算#
class Calculate
{
int Length { get; set; }
int Wide { get; set; }
int High { get; set; }
public int S { get; set; }
// 无参构造函数
public Calculate() { }
// 正方形:1 个参数
public Calculate(int length)
{
Length = length;
S = Length * Length;
}
// 长方形:2 个参数
public Calculate(int length, int wide)
{
Length = length;
Wide = wide;
S = Length * Wide;
}
// 梯形:3 个参数
public Calculate(int length, int wide, int high)
{
Length = length;
Wide = wide;
High = high;
S = (Length + Wide) * High / 2;
}
}
使用:
Console.WriteLine("正方形:" + new Calculate(10).S); // → 100
Console.WriteLine("长方形:" + new Calculate(10, 5).S); // → 50
Console.WriteLine("梯形:" + new Calculate(10, 100, 70).S); // → 3850
作业 3:抽象类 + 多态 — 借阅系统#
abstract class Account
{
public string Acc { get; set; }
public string Password { get; set; }
public Account(string acc, string passwd)
{
Acc = acc;
Password = passwd;
}
public abstract string pay(decimal money);
}
class Weixin : Account
{
public Weixin(string acc, string passwd) : base(acc, passwd) { }
public override string pay(decimal money)
{
return $"微信支付+{money} 元";
}
}
class Ali : Account
{
public Ali(string acc, string passwd) : base(acc, passwd) { }
public override string pay(decimal money)
{
return $"支付宝支付+{money} 元";
}
}
作业 4:多层抽象继承 — 图书馆借阅#
class Library
{
public string StuName { get; set; }
int StuBook { get; set; }
public Library(string stuname)
{
StuName = stuname;
}
}
abstract class Book : Library
{
public Book(string name) : base(name) { }
public abstract void Borrow(int book);
}
class Benke : Book // 本科生 — 最多借 5 本
{
public Benke(string name) : base(name) { }
public override void Borrow(int book)
{
if (book > 5)
Console.WriteLine("超出本科生借阅量");
else
Console.WriteLine($"你还可以借书{5 - book}本");
}
}
class Shuoshi : Book // 硕士生 — 最多借 10 本
{
public Shuoshi(string name) : base(name) { }
public override void Borrow(int book)
{
if (book > 10)
Console.WriteLine("超出硕士生借阅量");
else
Console.WriteLine($"你还可以借书{10 - book}本");
}
}
class Boshi : Book // 博士生 — 最多借 15 本
{
public Boshi(string name) : base(name) { }
public override void Borrow(int book)
{
if (book > 15)
Console.WriteLine("超出博士生借阅量");
else
Console.WriteLine($"你还可以借书{15 - book}本");
}
}
使用:
Benke benke = new Benke("张三");
benke.Borrow(4); // → "你还可以借书1本"
Shuoshi shuoshi = new Shuoshi("李四");
shuoshi.Borrow(5); // → "你还可以借书5本"
Boshi boshi = new Boshi("王五");
boshi.Borrow(10); // → "你还可以借书5本"