博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
20141211--C# 构造函数
阅读量:5250 次
发布时间:2019-06-14

本文共 3791 字,大约阅读时间需要 12 分钟。

namespace fengzhuang{    class Class2    {        private string _Name;        private string _Code;        public string _Sex;        public Class2()//构造函数        {            _Sex = "男";//每次初始化都会默认_Sex的值为“男”            Console.WriteLine("构造函数");        }        public string nv()//函数         {            _Sex = "女";            return _Sex;        }    }}

 

static void Main(string[] args)        {            Class2 op=new Class2();//本身就存在的,只要初始化一下类,便会调用函数,            Console.WriteLine(op._Sex);            op.nv();//调用函数,            Console.WriteLine(op._Sex);            Console.WriteLine();            Console.ReadLine();        }

 

习题:

using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Text;namespace xueshengxuanke{    class Program    {        static void Main(string[] args)        {            Console.WriteLine("请输入学生人数:");            int n = int.Parse(Console.ReadLine());            Console.WriteLine("请输入学生学号、姓名、分数");            ArrayList arr = new ArrayList();            for (int i = 0; i < n; i++)            {                student s = new student();                Console.Write("学号:");                s.Code = int.Parse(Console.ReadLine());                Console.Write("姓名:");                s.Name = Console.ReadLine();                Console.Write("分数:");                s.Score = decimal.Parse(Console.ReadLine());                arr.Add(s);            }            decimal sum = 0;            for (int i = 0; i < n; i++)            {                student avgs = (student)arr[i];                sum += avgs.Score;            }            ArrayList newarr = new student().Maopao(arr);            student ss1 = (student)newarr[0];            Console.WriteLine("姓名:{0}最高分为:{1}", ss1.Name, ss1.Score);            int x = n - 1;            student ss2 = (student)newarr[x];            Console.WriteLine("姓名:{0}最低分为:{1}", ss2.Name, ss2.Score);            Console.WriteLine("平均分:{0}", sum / n);            for (int i = 0; i < n; i++)            {                student avgs = (student)newarr[i];                Console.WriteLine("学生名次为:***********************************");                Console.WriteLine("学号         姓名           分数                 名次");                Console.WriteLine("{0}         {1}           {2}                 {3}", avgs.Code, avgs.Name, avgs.Score, i + 1);            }            Console.ReadLine();        }    }}

 

using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Text;namespace xueshengxuanke{    class student    {        private int _Code;        public int Code        {            get { return _Code; }            set { _Code = value; }        }        private string _Name;        public string Name        {            get { return _Name; }            set { _Name = value; }        }        private decimal _Score;        public decimal Score        {            get { return _Score; }            set { _Score = value; }        }        ///         /// 接收arraylist,排序后,返回arraylist        ///         /// 存放了student类对象的arraylist        /// 
返回排序后的集合
public ArrayList Maopao(ArrayList arr) { int n = arr.Count; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { student s1 = (student)arr[i]; student s2 = (student)arr[j]; if (s1.Score < s2.Score) { student zhong = new student(); zhong = s1; arr[i] = s2; arr[j] = zhong; } } } return arr; } }}

转载于:https://www.cnblogs.com/Tirisfal/p/4157714.html

你可能感兴趣的文章
tju 1782. The jackpot
查看>>
HTML5与CSS3基础(五)
查看>>
WinDbg调试C#技巧,解决CPU过高、死锁、内存爆满
查看>>
linux脚本中有source相关命令时的注意事项
查看>>
css样式表中的样式覆盖顺序
查看>>
湖南多校对抗赛(2015.03.28) H SG Value
查看>>
REST Web 服务(二)----JAX-RS 介绍
查看>>
hdu1255扫描线计算覆盖两次面积
查看>>
hdu1565 用搜索代替枚举找可能状态或者轮廓线解(较优),参考poj2411
查看>>
bzoj3224 splay板子
查看>>
程序存储问题
查看>>
Mac版OBS设置详解
查看>>
优雅地书写回调——Promise
查看>>
android主流开源库
查看>>
AX 2009 Grid控件下多选行
查看>>
PHP的配置
查看>>
Struts框架----进度1
查看>>
Round B APAC Test 2017
查看>>
MySQL 字符编码问题详细解释
查看>>
perl 学习笔记
查看>>