用c# 编写一个程序

 ①编一个程序,输入三个double类型数据,自定义一个静态方法,把这三个数送给它,返回找出的最大数。
 ②编写代码,定义一个基类MyClass,其中包含虚拟方法GetString().这个方法应返回存储在受保护字段myString中的字符串,该字段可以通过只写公共属性ContainedString来访问。
 ③从类MyClass中派生一个类MyDerivedClass.重写GetString()方法,使用该方法的基类执行代码从基类中返回一个字符串,但在返回的字符串中添加文本”output from derived class”.
要求,都要有主函数进行调用,要能输出结果或相应的字段。

1、取得最大值类:
class GetMax
{
public static double GetMaxNum(double NumOne,double NumTwo,double NumThree)
{
return Math.Max(Math.Max(NumOne, NumTwo), NumThree);
}
}
2、基类MyClass
abstract class MyClass
{
private string myString = "我是基类中的默认值 ";
public string ContainedString
{
set { myString = value; }
}
public virtual string GetString()
{
return myString;
}

}
3、子类MyDerivedClass
class MyDerivedClass:MyClass
{
public override string GetString()
{
return base.GetString() + "output from derived class";
}
}
主函数代码:
static void Main(string[] args)
{
//最大值
double Num1, Num2, Num3;
Console.WriteLine("请输入三个数字");
Num1 =Convert .ToDouble( Console.ReadLine());
Num2 = Convert.ToDouble(Console.ReadLine());
Num3 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("三个数中的最大值是:{0}",GetMax.GetMaxNum(Num1,Num2,Num3));

//输入基类和子类中的内容
MyClass myclass = new MyDerivedClass();
Console.Write(myclass.GetString());
Console.Read();
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-09-26
hehe
第2个回答  2011-09-26
你那个学校的?
第3个回答  2011-09-26
初学者要自己写,这么简单的问题不要到处求助到处抄,要不然是学不会的

using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
double x = double.Parse(Console.ReadLine());
double y = double.Parse(Console.ReadLine());
double z = double.Parse(Console.ReadLine());
Console.WriteLine(Max(x, y, z));
MyClass myc = new MyClass();
myc.ContainedString = "myClassString";
Console.WriteLine(myc.GetString());
MyDerivedClass mydc=new MyDerivedClass();
mydc.ContainedString = "myDerivedClassString";
Console.WriteLine(mydc.GetString());
}
public static double Max(double x, double y, double z)
{
double t = 0;
if (x > y)
{
t = x;
}
else
{
t = y;
}
if (t < z)
{
t = z;
}
return t;
}

public static void Swap(ref double x, ref double y)
{
double t = 0;
t = x;
x = y;
y = t;
}
}
public class MyClass
{
protected string myString;
public string ContainedString { set { myString = value; } }
public virtual string GetString()
{
return myString;
}
}

public class MyDerivedClass : MyClass
{
public override string GetString()
{
return base.GetString() + " output from derived class";
}
}
}
第4个回答  2011-09-26
楼主加油哦!
1.
static int ReturnMax(int a, int b, int c)
{
int max = a;
if (b > max)
{
max = b;
}
if (c > max)
{
max = c;
}
return max;
}
2.3 两题答案
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication5
{
class MyClass
{
protected string _myString="";
public virtual string GetString()
{
return _myString;
}
public string ContainedString
{
//get { return _myString; }//write only property shouldn't have get.
set { _myString = value; }
}
}
class MyDerivedClass : MyClass
{
public override string GetString()
{
return (base.GetString()+" output from derived class");
}
}
class Program
{
static void Main(string[] args)
{
MyClass MC = new MyClass();
MC.ContainedString = "My Class";
MyDerivedClass MDC = new MyDerivedClass();
MDC.ContainedString = "My DerivedClass";
Console.WriteLine(MC.GetString());
Console.WriteLine(MDC.GetString());
Console.ReadLine();
}
}
}
第5个回答  2011-09-26
哈哈。。。。同个学校的吧
相似回答