using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Inheritance1
{
class Teacher
{
string tid;
string tname;
public Teacher(string tid,string tname)
{
this.tid = tid;
this.tname = tname;
}
public string Tid
{
get
{
return tid;
}
}
public string Tname
{
get
{
return tname;
}
}
}
class Advisor:Teacher
{
string major;
public Advisor(string tid,string tname,string major):base(tid,tname)
{ this.major = major; }
public string Major
{
get
{
return major;
}
}
}
class Student
{
protected string sid;
protected string sname;
public Student(string sid, string sname)
{
this.sid = sid;
this.sname = sname;
}
}
class UnderGraduate : Student
{
Teacher teacher;
public UnderGraduate(string sid, string sname, Teacher teacher) : base(sid, sname)
{ this.teacher = teacher; }
public void Udisplay()
{
Console.WriteLine("该生学号:{0},姓名:{1};他班主任的工号:{2},姓名:{3}。",sid,sname,teacher.Tid,teacher.Tname);
}
}
class Graduate : Student
{
Advisor advisor;
public Graduate(string sid, string sname, Advisor advisor) : base(sid, sname)
{ this.advisor = advisor; }
public void Gdisplay()
{
Console.WriteLine("该生学号:{0},姓名:{1};他导师的工号:{2},姓名:{3},研究方向:{4}。",sid,sname, advisor.Tid, advisor.Tname, advisor.Major);
}
}
class Program
{
static void Main(string[] args)
{
Teacher t = new Teacher("2020112", "Marry");
UnderGraduate ug = new UnderGraduate("20200101", "Rose",t);
ug.Udisplay();
Advisor a = new Advisor("2018111", "Jim", "信息行为");
Graduate g = new Graduate("20200201", "Jack", a);
g.Gdisplay();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Ploymorphism
{
class Circle
{
protected double radius;
public Circle(double bj)
{
radius = bj;
}
public virtual double Area()
{
return Math.PI * radius * radius;
}
}
class Cylinder : Circle
{
double high;
public Cylinder(double r, double h)
: base(r)
{
high = h;
}
public override double Area()
{
return base.Area() * 2 + 2 * Math.PI * radius * high;
}
}
class Cone : Circle
{
double high;
public Cone(double r, double h)
: base(r)
{
high = h;
}
public override double Area()
{
return base.Area() + Math.PI * radius * Math.Sqrt(radius * radius + high * high);
}
}
class Program
{
static void Main(string[] args)
{
Circle cylinder = new Cylinder(100, 100);
Console.WriteLine("半径为100,高为100的圆柱体的表面积为{0:N2}", cylinder.Area());
Circle cone = new Cone(100, 100);
Console.WriteLine("半径为100,高为100的圆锥体的表面积为{0:N2}", cone.Area());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StudentInfo
{
class Student
{
public string number; //学号
public string name; //姓名
public string sclass; //班级
public Student(string xh, string xm, string bj)
{
number = xh;
name = xm;
sclass = bj;
}
public void GetInfo()
{
Console.WriteLine("类Student中学号是{0}的学生的姓名是{1},来自{2};", number, name, sclass);
}
}
struct SStudent
{
public string number; //学号
public string name; //姓名
public string sclass; //班级
public void SetInfo(string xh, string xm, string bj)
{
number = xh;
name = xm;
sclass = bj;
}
public void GetInfo()
{
Console.WriteLine("结构类型SStudent中学号是{0}的学生的姓名是{1},来自{2};", number, name, sclass);
}
}
class Program
{
static void Main(string[] args)
{
Student s1 = new Student("101", "张三", "信息191");
Console.WriteLine("请输出学生s1的信息:");
s1.GetInfo();
Console.WriteLine("");
Student s2 = s1;
s2.number = "102";
s2.name = "王小花";
Console.WriteLine("请输出学生s1的信息:");
s1.GetInfo();
Console.WriteLine("");
Console.WriteLine("请输出学生s2的信息:");
s2.GetInfo();
Console.WriteLine("");
SStudent ss1 = new SStudent();
ss1.SetInfo("201", "云朵朵", "信息192");
Console.WriteLine("请输出学生ss1的信息:");
ss1.GetInfo();
Console.WriteLine("");
SStudent ss2 = ss1;
ss2.number = "202";
ss2.name = "令狐花";
Console.WriteLine("请输出学生ss1的信息:");
ss1.GetInfo();
Console.WriteLine("");
Console.WriteLine("请输出学生ss2的信息:");
ss2.GetInfo();
Console.WriteLine("");
}
}
}
多态2题
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Proj7_1
{
class Program
{
public class Employee
{
int no;
string name;
int lengthofservice;
const double basesalary = 1000;
// double realsalary;
public int No
{
get { return no; }
set { no = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public virtual double SetRealSalary()
{
Console.Write("请输入此人的工龄:");
lengthofservice = int.Parse(Console.ReadLine());
double realsalary = basesalary + 30 * lengthofservice;
return realsalary;
}
}
public class UEmployee:Employee
{
public override double SetRealSalary()
{
return base.SetRealSalary() * 2;
}
}
public class GEmployee : UEmployee
{
public override double SetRealSalary()
{
return base.SetRealSalary() * 1.5;
}
}
static void Main(string[] args)
{
Employee s1 = new Employee();
s1.No = 101;
s1.Name = "张三";
Console.WriteLine("普通职工 工号:{0} 姓名:{1}",s1.No,s1.Name);
Console.WriteLine("此人的实际工资为:{0}", s1.SetRealSalary());
Console.WriteLine();
UEmployee s2 = new UEmployee();
s2.No = 201;
s2.Name = "李四";
Console.WriteLine("本科生职工 工号:{0} 姓名:{1}", s2.No, s2.Name);
Console.WriteLine("此人的实际工资为:{0}", s2.SetRealSalary());
Console.WriteLine();
GEmployee s3 = new GEmployee();
s3.No = 301;
s3.Name = "王五";
Console.WriteLine("研究生职工 工号:{0} 姓名:{1}", s3.No, s3.Name);
Console.WriteLine("此人的实际工资为:{0}", s3.SetRealSalary());
}
}
}
多态四题
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace duotai1
{
abstract class Person
{
protected string no;
protected string name;
abstract public string No
{ set;}
abstract public string Name
{ set;}
abstract public void Display();
}
class Student : Person
{
string grade;
public override string No
{ set { no = value; } }
public override string Name
{ set { name = value; } }
public string Grade
{ set { grade = value; } }
public override void Display()
{
Console.WriteLine("学生的学号是{0},姓名是{1},班级是{2}!",no, name,grade);
}
}
class Teacher : Person
{
string professionalTitle;
string department;
public override string No
{ set { no = value; } }
public override string Name
{ set { name = value; } }
public Teacher(string no, string name, string professionalTitle, string department)
{
this.no = no;
this.name = name;
this.professionalTitle = professionalTitle;
this.department = department;
}
public override void Display()
{
Console.WriteLine("教师的学号是{0},姓名是{1},是{2}的{3}!", no, name, department, professionalTitle);
}
}
class Program
{
static void Main(string[] args)
{
Student s = new Student();
s.No = "19111701";
s.Name = "Mary";
s.Grade = "信息161";
s.Display();
Teacher t = new Teacher("2017001","Tom","副教授","信息管理系");
t.Display();
}
}
}
实践课C#
最新推荐文章于 2024-09-27 00:02:43 发布