1.设计员工类(Worker)及其子类经理类(Manager),员工类包含私有字段name,salary;并设置其属性Name,Salary;经理类还有自己的私有成员字段bonus,及其对应属性Bonus;员工类、经理类都要有自己的无参、有参构造方法;
在main中创建一个员工数组(经理作为其一个元素),并为数组没个元素赋值,要求打印输出该员工数组的姓名和薪水信息。
class Worker
{
string name;
int salary;
public Worker() { }
public Worker(string name,int salary)
{
this.Name = name;
this.Salary = salary;
}
public string Name { get => name; set => name = value; }
public int Salary { get => salary; set => salary = value; }
}
class Manager:Worker
{
int bonus;
public Manager() { }
public Manager(string name, int salary,int bonus):base( name, salary)
{
this.Bonus = bonus;
}
public int Bonus { get => bonus; set => bonus = value; }
}
static void Main(string[] args)
{
Worker[] workers = new Worker[2];
Worker worker = new Worker("张三", 2000);
Manager manager = new Manager("李四", 20000, 3000);
workers[0] = worker;
workers[1] = manager;
for(int i = 0; i < workers.Length; i++)
{
Console.WriteLine("姓名:{0},工资:{1}",workers[i].Name,workers[i].Salary);
}
}
2.设计学生类(Student)及其子类研究生类(Graduate),学生类包含私有成员字段name,credit;并包含其属性Name,Credit;研究生类包含自己的私有变量postCredit;并包含其属性PostCredit,学生类(Student)及其子类研究生类(Graduate)要有自己的无参、有参构造方法;
现需创建一个研究生对象并设置其postcredit,另建立学生数组(研究生作为其一个元素),要求打印输出该学生数组的姓名和学分信息。
class Student
{
string name;
int credit;
public Student() { }
public Student(string name,int credit)
{
this.Name = name;
this.Credit = credit;
}
public string Name { get => name; set => name = value; }
public int Credit { get => credit; set => credit = value; }
}
class Graduate:Student
{
int postCredit;
public Graduate() { }
public Graduate(string name, int credit,int postCredit) : base(name, credit)
{
this.PostCredit = postCredit;
}
public int PostCredit { get => postCredit; set => postCredit = value; }
}
static void Main(string[] args)
{
Student[] students = new Student[2];
Student student = new Student("张三", 80);
Graduate graduate = new Graduate("李四", 90, 80);
students[0] = student;
students[1] = graduate;
for(int i = 0; i < students.Length; i++)
{
Console.WriteLine("姓名:{0},学分:{1}", students[i].Name, students[i].Credit);
}
}
3.定义一个名为Vehicles交通工具的基类:
该类中包含私有成员字段商标和颜色,并设置其相应的公有属性;
类中包含成员方法Run来模拟交通工具开动,该方法只输出“我已经开动了”信息;
类中包含成员方法ShowInfo来显示信息,该方法输出显示商标和颜色;
完成基类的无参有参构造方法,
编写Car小汽车类继承于Vehicles类,对于此类:
增加成员字段座位,并设置其相应的公有属性;
增加成员方法ShowCar,输出显示小汽车的信息;
覆盖父类的Run方法,输出显示“汽车开动了的信息”;
完成小汽车类的无参有参构造方法;
在main方法中测试以上各类。
class Vehicles
{
string brand;
string color;
public virtual void Run()
{
Console.WriteLine("我已经开动了");
}
public void ShowInfo()
{
Console.WriteLine("商标:{0},颜色:{1}", this.Brand, this.Color);
}
public Vehicles() { }
public Vehicles(string brand,string color)
{
this.Brand = brand;
this.Color = color;
}
public string Brand { get => brand; set => brand = value; }
public string Color { get => color; set => color = value; }
}
class Car:Vehicles
{
int seat;
public void ShowCar()
{
Console.WriteLine("商标:{0},颜色:{1},座位:{2}", this.Brand, this.Color, this.Seat);
}
public override void Run()
{
Console.WriteLine("小汽车开动了");
}
public Car() { }
public Car(string brand,string color,int seat) : base(brand, color)
{
this.Seat = seat;
}
public int Seat { get => seat; set => seat = value; }
}
static void Main(string[] args)
{
Vehicles vehicles = new Vehicles("卡宴", "黑色");
Car car = new Car("劳斯莱斯", "红色", 2);
vehicles.Run();
vehicles.ShowInfo();
car.ShowCar();
car.Run();
}
4.定义一个名为Vehicles交通工具的基类:
该类中包含私有成员字段商标和颜色,并设置其相应的公有属性;
类中包含成员方法run来模拟交通工具开动,该方法输出显示“我已经开动了”信息;
类中包含成员方法ShowInfo来显示信息,该方法输出显示商标和颜色
完成父类的无参有参构造方法;
编写Truck卡车类继承于Vehicles类对于此类:
增加成员字段载重,并设置其相应的公有属性;
增加成员方法showTruck,输出显示卡车的信息;
完成卡车类的无参有参构造方法;
覆盖父类的run方法,输出显示“开车开动了的信息”;
在main方法中测试以上各类。
class Vehicles
{
string brand;
string color;
public virtual void Run()
{
Console.WriteLine("我已经开动了");
}
public void ShowInfo()
{
Console.WriteLine("商标:{0},颜色:{1}", this.Brand, this.Color);
}
public Vehicles() { }
public Vehicles(string brand,string color)
{
this.Brand = brand;
this.Color = color;
}
public string Brand { get => brand; set => brand = value; }
public string Color { get => color; set => color = value; }
}
class Truck:Vehicles
{
int load;
public void ShowTruck()
{
Console.WriteLine("商标:{0},颜色:{1},载重:{2}", this.Brand, this.Color, this.Load);
}
public Truck() { }
public Truck(string brand,string color,int load) : base(brand, color)
{
this.Load = load;
}
public override void Run()
{
Console.WriteLine("卡车开动了");
}
public int Load { get => load; set => load = value; }
}
static void Main(string[] args)
{
Vehicles vehicles = new Vehicles("卡宴", "黑色");
Truck truck = new Truck("奔驰", "红色", 3000);
vehicles.Run();
vehicles.ShowInfo();
truck.ShowTruck();
truck.Run();
}
5.创建一个名称为IVehicle的接口:
在接口中添加两个方法Start()和Stop()用以描述车辆的启动和停止。
创建Bike自行车类:
该类包含私有成员字段wheel车轮个数,并设置其相应的公有属性;
完成该类的无参有参构造方法;
实现IVehicle接口的两个方法;
创建Bus公共汽车类:
该类包含私有成员字段seat座位个数,并设置其相应的公有属性;
完成该类的无参有参构造方法;
实现IVehicle接口的两个方法;
在main方法中定义IVehicle数组,并存放Bike和Bus对象,来测试以上各类。
interface IVehicle
{
void Start();
void Stop();
}
class Bike:IVehicle
{
int wheel;
public Bike() { }
public Bike(int wheel)
{
this.Wheel = wheel;
}
public int Wheel { get => wheel; set => wheel = value; }
public void Start()
{
Console.WriteLine("我是{0}轮自行车,我要开动了",Wheel);
}
public void Stop()
{
Console.WriteLine("自行车停下了");
}
}
class Bus:IVehicle
{
int seat;
public Bus() { }
public Bus(int seat)
{
this.Seat = seat;
}
public void Start()
{
Console.WriteLine("我是{0}座公交车,我要开动了", Seat);
}
public void Stop()
{
Console.WriteLine("公交车停下了");
}
public int Seat { get => seat; set => seat = value; }
}
6.定义一个宠物类(Pet):
该类包括两个方法:叫Cry(),吃东西Eat();
该类中定义私有的成员字段name姓名和age年龄,并设置其相应的公有属性;
完成该类的无参有参构造方法;
定义宠物的子类狗(Dog):
覆盖父类的Cry(),Eat()方法;增加方法看门GuardEntrance()
完成该类的无参有参构造方法;
定义宠物的子类猫(Cat):
覆盖父类的Cry(),Eat()方法;
增加猫自己独有的方法捉老鼠HuntMice();
完成该类的无参有参构造方法;
在main中定义两个Pet变量,pet1,pet2,采用引用转型实例化Dog,Cat,分别调用Pet的Cry(),Eat();将Pet强制转换为具体的Dog,Cat,在调Dog的GuardEntrance(),Cat的HuntMice()。
class Pet
{
string name;
int age;
public string Name { get => name; set => name = value; }
public int Age { get => age; set => age = value; }
public Pet() { }
public Pet(string name,int age)
{
this.Name = name;
this.Age = age;
}
public virtual void Cry()
{
Console.WriteLine("我是宠物,我叫{0},我{1}岁了", this.Name, this.Age);
}
public virtual void Eat()
{
Console.WriteLine("我是爱吃的宠物");
}
}
class Dog:Pet
{
public Dog() { }
public Dog(string name,int age) : base(name, age)
{
}
public override void Cry()
{
Console.WriteLine("我是狗,我叫{0},我{1}岁了", this.Name, this.Age);
}
public override void Eat()
{
Console.WriteLine("我爱吃骨头");
}
public void GuardEntrance()
{
Console.WriteLine("我喜欢看门");
}
}
class Cat:Pet
{
public Cat() { }
public Cat(string name, int age) : base(name, age)
{
}
public override void Cry()
{
Console.WriteLine("我是猫,我叫{0},我{1}岁了", this.Name, this.Age);
}
public override void Eat()
{
Console.WriteLine("我爱吃鱼");
}
public void HuntMice()
{
Console.WriteLine("我喜欢捉老鼠");
}
}
static void Main(string[] args)
{
Pet pet1 = new Dog("小黄", 3);
pet1.Cry();
pet1.Eat();
((Dog)pet1).GuardEntrance();
Pet pet2 = new Cat("小白", 3);
pet2.Cry();
pet2.Eat();
((Cat)pet2).HuntMice();
}
7.创建一个名称为IShape的接口:
在接口中添加求面积方法Area()和求体积方法Volumn()。
定义一个立方体的类Prog:
字段包括长、宽、高;并定义相应属性;
方法包括:构造方法(初始化立方体的长宽高);
实现接口IShape;
在main中创建一个立方体对象,计算并显示其面积和体积。
interface Ishape
{
void Area();
void Volumn();
}
class Prog:Ishape
{
double length;
double width;
double height;
public double Length { get => length; set => length = value; }
public double Width { get => width; set => width = value; }
public double Height { get => height; set => height = value; }
public Prog() { }
public Prog(double length,double width,double height)
{
this.Length = length;
this.Width = width;
this.Height = height;
}
public void Area()
{
Console.WriteLine("立方体的表面积为:{0}", 2 * this.Length * this.Width + 2 * this.Length * this.Height + 2 * this.Width * this.Height);
}
public void Volumn()
{
Console.WriteLine("立方体的体积为:{0}", this.Length * this.Width * this.Height );
}
}
static void Main(string[] args)
{
Prog prog = new Prog(3, 4, 5);
prog.Area();
prog.Volumn();
}
8.创建一个名称为IShape的接口:
在接口中添加求面积方法Area()和求体积方法Volumn()。
定义一个球的类Ball:
字段包括半径;并定义相应属性;
方法包括:构造方法(初始化球的半径);
实现接口IShape;
在main中创建一个球对象,计算并显示其面积和体积。
interface Ishape
{
void Area();
void Volumn();
}
class Ball:Ishape
{
double radius;
public Ball() { }
public Ball(double radius)
{
this.Radius = radius;
}
public double Radius { get => radius; set => radius = value; }
public void Area()
{
Console.WriteLine("球的表面积为:{0}", 4 * Math.PI * this.Radius * this.Radius);
}
public void Volumn()
{
Console.WriteLine("球的体积为:{0}", 4 / 3 * Math.PI * this.Radius * this.Radius * this.Radius);
}
}
static void Main(string[] args)
{
Ball ball = new Ball(2);
ball.Area();
ball.Volumn();
}
9.创建一个名称为Square的类:
该类中定义私有的成员字段edge,并设置其相应的公有属性;
完成该类的无参有参构造方法;
该类包含方法Circumference(周长)和面积(Area);
定义子类正方体Cube类:
完成该类的无参有参构造方法;
实现该类的面积(Area)和体积(Volumn)方法。
在main中创建正方形对象,计算并显示其周长和面积;创建正方体对象,计算并显示其面积和体积。
class Square
{
double edge;
public double Edge { get => edge; set => edge = value; }
public Square() { }
public Square(double edge)
{
this.Edge = edge;
}
public void Circumference()
{
Console.WriteLine("正方形的周长为:{0}", this.Edge * 4);
}
public void Area()
{
Console.WriteLine("正方形的面积为:{0}", this.Edge * this.Edge);
}
}
class Cube:Square
{
public Cube() { }
public Cube(double edge) : base(edge)
{
}
public void Area()
{
Console.WriteLine("正方体的表面积为:{0}", 6 * this.Edge * this.Edge);
}
public void Volumn()
{
Console.WriteLine("正方体的体积为:{0}", this.Edge * this.Edge * this.Edge);
}
}
static void Main(string[] args)
{
Square square = new Square(4);
square.Circumference();
square.Area();
Cube cube = new Cube(4);
cube.Area();
cube.Volumn();
}
10. 创建一个名称为Circle的类:
该类中定义私有的成员字段radius,并设置其相应的公有属性;
完成该类的无参有参构造方法;
该类包含方法Circumference(周长)和面积(Area);
定义子类圆柱体Cylinder类:
字段包括高;并定义相应属性;
完成该类的无参有参构造方法;
实现该类的面积(Area)和体积(Volumn)方法。
在main中创建圆类对象,计算并显示其周长和面积;创建圆柱体对象,计算并显示其面积和体积。
class Circle
{
double radius;
public Circle() { }
public Circle(double radius)
{
this.Radius = radius;
}
public void Circumference()
{
Console.WriteLine("圆的周长为:{0}", 2 * Math.PI * this.Radius);
}
public void Area()
{
Console.WriteLine("圆的面积为:{0}", Math.PI * this.Radius * this.Radius);
}
public double Radius { get => radius; set => radius = value; }
}
class Cylinder:Circle
{
double height;
public Cylinder() { }
public Cylinder(double radius,double height) : base(radius)
{
this.Height = height;
}
public void Area()
{
Console.WriteLine("圆柱体的表面积为:{0}", 2 * Math.PI * this.Radius*(this.Radius + this.Height));
}
public void Volumn()
{
Console.WriteLine("圆柱体的体积为:{0}", Math.PI * this.Radius * this.Radius * this.Height);
}
public double Height { get => height; set => height = value; }
}
static void Main(string[] args)
{
Circle circle = new Circle(3);
circle.Circumference();
circle.Area();
Cylinder cylinder = new Cylinder(3, 4);
cylinder.Area();
cylinder.Volumn();
}
11.创建一个Student类,要求:
(1)封装学生的姓名、性别和成绩等信息;
(2)通过构造函数给姓名和性别信息赋值;
(3)成绩信息通过属性进行读写,对成绩赋值时,如果成绩大于100分赋值100,小于0分赋值0;
(4)具有一个判断成绩等级的方法;
在main中使用Student类。
class Student
{
string name;
char sex;
int score;
public string Name { get => name; set => name = value; }
public char Sex {
get
{
if (sex != '男' && sex != '女')
{
return sex = '男';
}
return sex;
}
set
{
sex = value;
}
}
public int Score {
get
{
return score;
}
set
{
if (score > 100)
{
score = 100;
}
else if (score < 0)
{
score = 0;
}
score = value;
}
}
public Student(string name, char sex, int score)
{
this.Name = name;
this.Sex = sex;
this.Score = score;
Console.WriteLine("姓名:{0},性别:{1},成绩:{2}", this.Name, this.Sex, this.Score);
Grade(score);
}
public void Grade(int score)
{
int n = score / 10;
switch (n)
{
case 10:
case 9:
Console.WriteLine("成绩等级为A");
break;
case 8:
Console.WriteLine("成绩等级为B");
break;
case 7:
Console.WriteLine("成绩等级为C");
break;
case 6:
Console.WriteLine("成绩等级为D");
break;
default:
Console.WriteLine("成绩等级为E");
break;
}
}
}
static void Main(string[] args)
{
Student ZSstudent = new Student("张三", '男', 90);
Student LSstudent = new Student("李四", '男', 70);
}
15.设计一个抽象类Calculate,该类包括:
(1)optA、optB、optC三个double类型的字段;
(2)带有两个double类型参数的构造函数(给optA和optB赋值);
(3)计算三个数和的平方根SqrtForSum抽象方法,该方法带有三个double类型的参数,返回值类型为double;
(4)设计一个继承Calculate的派生类Cal,该类包含一个带有三个double类型参数的构造函数,并重写SqrtForSum方法;
(5)在main中进行相关数据验证。
abstract class Caculate
{
double optA;
double optB;
double optC;
public Caculate() { }
public Caculate(double optA,double optB)
{
this.OptA = optA;
this.OptB = optB;
}
public double OptA { get => optA; set => optA = value; }
public double OptB { get => optB; set => optB = value; }
public double OptC { get => optC; set => optC = value; }
public abstract double SqrtForSum(double optA, double optB, double optC);
}
class Cal : Caculate
{
public Cal() { }
public Cal(double optA, double optB, double optC) : base(optA, optB)
{
this.OptC = optC;
}
public override double SqrtForSum(double optA, double optB, double optC)
{
return Math.Sqrt(optA) + Math.Sqrt(optB) + Math.Sqrt(optC);
}
}
static void Main(string[] args)
{
Caculate cal = new Cal(1, 1, 1);
Console.WriteLine(cal.SqrtForSum(cal.OptA, cal.OptB, cal.OptC));
}