题目一:设计控制台应用程序,声明一个人类Person和一个动物类Animal,它们都包含公有字段legs(腿的只数)和保护的字段weight(重量),定义它们的对象并输出相关数据。
using System;
namespace zuoye71
{
class Person
{
// int x,y;
public int legs = 2;
protected int weight = 40;
public void print()
{
Console.WriteLine("某人有{0}条腿,重量为{1}kg", legs, weight);
}
}
class Animal
{
protected int weight = 20;
public const int legs = 4;
public void print()
{
Console.WriteLine("某动物有{0}只腿,重量为{1}kg", legs, weight);
}
}
class Program
{
static void Main(string[] args)
{
Person ren = new Person();
Animal shou = new Animal();
ren.print();
shou.print();
Console.ReadKey();
}
}
}
题目二: 请定义一个交通工具(Vehicle)的类,其中有:
⦁ 属性:速度(speed),体积(size);
⦁ 方法:移动(move()),设置速度(setSpeed(int speed)),设置体积(setSize(int size))加速speedUp(),减速speedDown();
在测试类Vehicle中的main()中实例化一个交通工具对象,通过方法给它初始化speed,size的值,并打印出来。另外,调用加速,减速的方法对速度进行改变。
using System;
namespace zuoye72
{
class Program
{
static void Main(string[] args)
{
Vehicle v1 = new Vehicle(100, 1000);
Console.WriteLine("速度为" + v1.getSpeed() + "体积为" + v1.getSize());
v1.move();
v1.speedUp();
v1.speedDown();
}
}
class Vehicle
{
private int speed;
private double size;
public Vehicle()
{
}
public Vehicle(int speed, double size)
{
this.speed = speed;
this.size = size;
}
public int getSpeed()
{
return speed;
}
public double getSize()
{
return size;
}
public void setSpeed(int speed)
{
this.speed = speed;
}
public void setSize(double size)
{
this.size = size;
}
public void move()
{
Console.WriteLine("Vehicle以" + this.speed + "的速度在移动");
}
public void speedUp()
{
this.speed++;
Console.WriteLine("Vehicle现在加速后速度是" + this.speed);
}
public void speedDown()
{
this.speed--;
Console.WriteLine("Vehicle现在减速后速度是" + this.speed);
}
}
}
题目三:试编写一个函数compute(int a,int b,int sum,int sub,int mul,int quo ),其中sum、sub、mul、quo分别是a,b的和、差、积、商。并编写主函数测试上述函数。提示:和、差、积、商分别采用ref和out形参两种方式实现。
using System;
namespace zuoye73
{
class Program
{
static void Main(string[] args)
{
Use c1 = new Use();
c1.usecom(new sum(), 2, 1);
c1.usecom(new sub(), 2, 1);
c1.usecom(new mul(), 2, 1);
c1.usecom(new quo(), 2, 1);
}
}
interface Computer
{
int computer(int n, int m);
}
class sum : Computer
{
public int computer(int n, int m)
{
return n + m;
}
}
class sub : Computer
{
public int computer(int n, int m)
{
return n - m;
}
}
class mul : Computer
{
public int computer(int n, int m)
{
return n * m;
}
}
class quo : Computer
{
public int computer(int n, int m)
{
return n / m;
}
}
class Use
{
public void usecom(Computer com, int n, int m)
{
Console.WriteLine(com.computer(n, m));
}
}
}
题目四: 创建一个控制台应用程序,定义一个Add方法,该方法有3重重载形式,分别用来计算两个int数据的和,计算一个int和一个double数据的和、计算3个int数据的和;数据在Main方法中分别使用Add方法的3种重载形式,并输出计算结果。
using System;
namespace zuoye74
{
class Program
{
private static int Add(int a1, int a2)
{
return a1 + a2;
}
private static double Add(int a1, double a2)
{
return a1 + a2;
}
private static int Add(int a1, int a2, int a3)
{
return a1 + a2 + a3;
}
static void Main(string[] args)
{
Console.WriteLine("整数和{0}", Add(1, 2));
Console.WriteLine("浮点数和{0}", Add(1, 2.1));
Console.WriteLine("3个整数和{0}", Add(2, 3, 4));
}
}
}