幫幫我吧,感激不盡
求源代碼
uj5u.com熱心網友回復:
這意思不是很明白了嗎?uj5u.com熱心網友回復:
把類,繼承,多載這些基本概念搞清楚了不難的。uj5u.com熱心網友回復:
前天不是有問同樣問題的么 這不就是個作業題么 你和之前提問的是同班同學吧?
uj5u.com熱心網友回復:
幫你找到了https://bbs.csdn.net/topics/394347332#post-408779973
uj5u.com熱心網友回復:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, int> score = new Dictionary<string, int>();
score.Add("語文", 90);
score.Add("數學", 88);
score.Add("英語", 78);
score.Add("化學", 84);
score.Add("物理", 94);
Student student = new Student("張三", 20, "男");
student.Score = score;
Console.WriteLine("{0}的平均成績為{1}", student.Name, student.AverageScore);
Console.ReadKey();
}
}
public class Person
{
// 姓名
protected string name;
public string Name
{
get { return name; }
set { name = value; }
}
// 年齡
protected int age;
public int Age
{
get { return age; }
set { age = value; }
}
/// <summary>
/// 性別
/// </summary>
protected string sex;
public string Sex
{
get { return sex; }
set { sex = value; }
}
}
public class Student : Person
{
// 成績單
protected Dictionary<string, int> score;
public Dictionary<string, int> Score
{
get { return score; }
set { score = value; }
}
// 平均成績
public double AverageScore
{
get
{
if (score == null || score.Count == 0)
{
throw new Exception("成績不能為空");
}
else
{
double sum = 0;
foreach (KeyValuePair<string, int> kvp in score)
{
sum += kvp.Value;
}
return sum / score.Count;
}
}
}
// 建構式一
public Student(string name)
{
this.name = name;
}
// 建構式二
public Student(string name, int age)
{
this.name = name;
this.age = age;
}
// 建構式三
public Student(string name, int age, string sex)
{
this.name = name;
this.age = age;
this.sex = sex;
}
}
}
uj5u.com熱心網友回復:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Sex { get; set; }
}
public class Student : Person
{
public Student(string name)
{
this.Name = name;
}
public Student(string name, int age, string sex)
{
this.Name = name;
this.Age = age;
this.Sex = sex;
}
public Student(string name,int age,string sex, double Chinese, double Math, double English, double Physics, double Chemistry)
{
this.Name = name;
this.Age = age;
this.Sex = sex;
this.ChineseScore = Chinese;
this.MathScore = Math;
this.EnglishScore = English;
this.PhysicsScore = Physics;
this.ChemistryScore = Chemistry;
}
/// <summary>
/// 語文分數
/// </summary>
public double ChineseScore { get; set; }
/// <summary>
/// 數學分數
/// </summary>
public double MathScore { get; set; }
/// <summary>
/// 英語分數
/// </summary>
public double EnglishScore { get; set; }
/// <summary>
/// 物理分數
/// </summary>
public double PhysicsScore { get; set; }
/// <summary>
/// 化學分數
/// </summary>
public double ChemistryScore { get; set; }
/// <summary>
/// 獲取五科平均分
/// </summary>
/// <returns></returns>
public double GetAvgScore()
{
return (ChineseScore + MathScore + EnglishScore + PhysicsScore + ChemistryScore) / 5.0;
}
}
static void Main(string[] args)
{
Student li = new Student("李四", 18, "男");
li.ChineseScore = 91;
li.MathScore = 85;
li.EnglishScore = 48;
li.PhysicsScore = 95;
li.ChemistryScore = 85;
Console.WriteLine(String.Format("學生姓名:{0},性別:{1},年齡:{2}", li.Name, li.Sex, li.Age));
Console.WriteLine(String.Format("語文成績:{0},數學成績:{1},英語成績:{2},物理成績:{3},化學成績:{4}", li.ChineseScore, li.MathScore, li.EnglishScore, li.PhysicsScore, li.ChemistryScore));
Console.WriteLine(String.Format("學生姓名:{0},五科平均分:{1}", li.Name, li.GetAvgScore()));
Console.ReadKey();
}
uj5u.com熱心網友回復:
為什么不退學 ?uj5u.com熱心網友回復:
繼承、多型 基礎問題 。uj5u.com熱心網友回復:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, int> score = new Dictionary<string, int>();
score.Add("語文", 90);
score.Add("數學", 88);
score.Add("英語", 78);
score.Add("化學", 84);
score.Add("物理", 94);
Student student = new Student("張三", 20, "男");
student.Score = score;
Console.WriteLine("{0}的平均成績為{1}", student.Name, student.AverageScore);
Console.ReadKey();
}
}
public class Person
{
// 姓名
protected string name;
public string Name
{
get { return name; }
set { name = value; }
}
// 年齡
protected int age;
public int Age
{
get { return age; }
set { age = value; }
}
/// <summary>
/// 性別
/// </summary>
protected string sex;
public string Sex
{
get { return sex; }
set { sex = value; }
}
}
public class Student : Person
{
// 成績單
protected Dictionary<string, int> score;
public Dictionary<string, int> Score
{
get { return score; }
set { score = value; }
}
// 平均成績
public double AverageScore
{
get
{
if (score == null || score.Count == 0)
{
throw new Exception("成績不能為空");
}
else
{
double sum = 0;
foreach (KeyValuePair<string, int> kvp in score)
{
sum += kvp.Value;
}
return sum / score.Count;
}
}
}
// 建構式一
public Student(string name)
{
this.name = name;
}
// 建構式二
public Student(string name, int age)
{
this.name = name;
this.age = age;
}
// 建構式三
public Student(string name, int age, string sex)
{
this.name = name;
this.age = age;
this.sex = sex;
}
}
}
5樓,6樓都可以啊,利用 封裝多型繼承都可以實作的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/117617.html
標籤:C#
上一篇:VB.net Graphics畫在螢屏上的影像,如何再次使用Graphics.fromscreen截圖
下一篇:chart實作階梯線
