c#题 求解答
代码分析重点内容1,输出数组排序结果2,输出值传递和引用传递的结果(PPT中valtest1示例程序)程序设计题重点内容1,编写程序数组排序找出最小值2,定义Person...
代码分析重点内容 1,输出数组排序结果 2,输出值传递和引用传递的结果(PPT中valtest1示例程序) 程序设计题重点内容 1,编写程序数组排序找出最小值 2,定义Person类,并调用 3,定义shape基类,并定义派生类Circle和Rectangle,在主程序调用(要求定义虚函数及在继承类实现)
展开
展开全部
1.排序找最小值
int[] array = new int[] { 1,3,2,10,8,6 };
//排序
for (int i = 0; i < array.Length - 1; i++)
{
for (int j = 0; j < array.Length - 1 - i; j++)
{
if (array[j] > array[j + 1])
{
array[j] ^= array[j + 1];
array[j + 1] ^= array[j];
array[j] ^= array[j + 1];
}
}
}
//输出最小值
Console.WriteLine(array[0]);
2.值传递和引用传递
static void Main()
{
Person person = new Person() { Age = 19 };
int age = 18;
Console.WriteLine(string.Format("传递前, person.Age = {0}, age = {1}", person.Age, age));
ChangeAge(person);
ChangeAge(age);
Console.WriteLine(string.Format("传递后, person.Age = {0}, age = {1}", person.Age, age));
Console.ReadLine();
}
static void ChangeAge(Person person)
{
person.Age = 21;
Console.WriteLine(string.Format("引用传递: 方法内部改变person.Age = {0}", person.Age));
}
static void ChangeAge(int age)
{
age = 22;
Console.WriteLine(string.Format("值传递: 方法内部改变age = {0}", age));
}
class Person
{
public int Age
{
get;
set;
}
}
3.继承
static void Main()
{
Shape shape = new Shape();
Shape circle = new Circle();
Shape ractangle = new Ractangle();
shape.Paint();
circle.Paint();
ractangle.Paint();
Console.ReadLine();
}
class Shape
{
public virtual void Paint()
{
Console.WriteLine("父类方法");
}
}
class Circle : Shape
{
public override void Paint()
{
Console.WriteLine("这是个Circle");
}
}
class Ractangle : Shape
{
public override void Paint()
{
Console.WriteLine("这是个Ractangle");
}
}
int[] array = new int[] { 1,3,2,10,8,6 };
//排序
for (int i = 0; i < array.Length - 1; i++)
{
for (int j = 0; j < array.Length - 1 - i; j++)
{
if (array[j] > array[j + 1])
{
array[j] ^= array[j + 1];
array[j + 1] ^= array[j];
array[j] ^= array[j + 1];
}
}
}
//输出最小值
Console.WriteLine(array[0]);
2.值传递和引用传递
static void Main()
{
Person person = new Person() { Age = 19 };
int age = 18;
Console.WriteLine(string.Format("传递前, person.Age = {0}, age = {1}", person.Age, age));
ChangeAge(person);
ChangeAge(age);
Console.WriteLine(string.Format("传递后, person.Age = {0}, age = {1}", person.Age, age));
Console.ReadLine();
}
static void ChangeAge(Person person)
{
person.Age = 21;
Console.WriteLine(string.Format("引用传递: 方法内部改变person.Age = {0}", person.Age));
}
static void ChangeAge(int age)
{
age = 22;
Console.WriteLine(string.Format("值传递: 方法内部改变age = {0}", age));
}
class Person
{
public int Age
{
get;
set;
}
}
3.继承
static void Main()
{
Shape shape = new Shape();
Shape circle = new Circle();
Shape ractangle = new Ractangle();
shape.Paint();
circle.Paint();
ractangle.Paint();
Console.ReadLine();
}
class Shape
{
public virtual void Paint()
{
Console.WriteLine("父类方法");
}
}
class Circle : Shape
{
public override void Paint()
{
Console.WriteLine("这是个Circle");
}
}
class Ractangle : Shape
{
public override void Paint()
{
Console.WriteLine("这是个Ractangle");
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询