C#王楠老师留的作业

本文最后更新于:7 months ago

题目
question
Human类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace homework
{
public class Human:Interface1
{
public Human()
{

}
public Human(string name)
{
_name = name;

}
public Human(int age)
{

_age = age;
}
public Human(string name, int age) { _name = name; if (age > 0 && age < 130) { _age = age; } }
protected string _name;
protected int _age;


public virtual void introduce()
{
Console.WriteLine($"姓名:{_name}");

if (_age == 0) { Console.WriteLine($"年龄:input is not legal"); }
else { Console.WriteLine($"年龄:{_age}"); }
}

public void speak()
{
introduce();
}
}
}




Star,Singer,Producer类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace homework
{
public class Star : Human
{ string _realm;
int _outyear;
public Star(string name, int age, string realm, int outyear) : base(name, age)
{
_realm = realm;
_outyear = outyear;

}
public override void introduce()
{
base.introduce();
Console.WriteLine($"出道时间:{_outyear}");
Console.WriteLine($"领域:{_realm}");
}

}
public class Singer:Star
{
string ?_song;
public Singer(string name, int age, string realm, int outyear,string song) :base( name, age, realm, outyear)
{
_song=song;
}
public override void introduce()
{
base.introduce();
Console.WriteLine($"成名曲:{_song}");
}

}
public class Producer : Star
{
string _movie;
public Producer(string name, int age, string realm, int outyear, string movie) : base(name, age, realm, outyear)
{
_movie = movie;
}
public override void introduce()
{
base.introduce();
Console.WriteLine($"成名电影:{_movie}");

}


}
}



接口Interface1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace homework
{
public interface Interface1
{
void speak();
}
}


Program 控制台程序入口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;
namespace homework
{
class Program
{
static void Main(string[] args)
{
Producer leo = new Producer("leonardo dicaprio", 148,"movie actor",1990,"Titannic");
leo.speak();
Singer jay = new Singer("Jay Chou", 43, "rap", 2000, "晴天");
jay.introduce();
Star Dayao = new Star("Yao ming", 42, "basketball", 1998);
Dayao.speak();
}
}
}

运行结果
result