20141211--C# 构造函数
2024-09-06 02:31:42
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; }
} /// <summary>
/// 接收arraylist,排序后,返回arraylist
/// </summary>
/// <param name="arr">存放了student类对象的arraylist</param>
/// <returns>返回排序后的集合</returns>
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;
}
}
}
最新文章
- Lind.DDD.Utils.HttpHelper关于对HttpClient的正确使用
- iphone中 input圆角bug
- 用Dart&;Henson玩转Activity跳转
- 使用B或BL跳转时,下一条指令的地址是这样计算的
- linux中mysql密码找回的两种方式
- 设置peoplecode trace
- C# Multilanguage messagebox z
- 【java】利用异常机制,往前台写错误信息
- Web Api 2, Oracle and Entity Framework
- 基于Redis缓存几十万条记录的快速模糊检索的功能实现(c#)
- Docker学习系列之一——安装篇
- entity.Database.SqlQuery() 和entity.Database.SqlCommand()
- instanceof 操作符实现原理解析
- css隐藏滚动条依旧可以滚动
- linux使用rz、sz快速上传、下载文件
- selector的例子
- js中JSON.stringify用于自定义的类
- xlsxwriter
- OC中的私有方法
- PHP图片加水印文字及图片合成缩放
热门文章
- PHP中的九大缓存技术
- CCNA 之 一 网络基础
- Java-多线程第一篇多线程相关认识(1)
- Linux删除自带的openjdk,安装jdk1.8
- [HDU5807] [BestCoder Round #86 1004] Keep In Touch (DP)
- python中bytes和str
- BZOJ 1040 [ZJOI2008]骑士 (基环树+树形DP)
- 左侧菜单收缩的实现(包括,筛选器,addclass、removeclass、绑定事件,链式编程)
- git如何删除远程仓库的某次错误提交
- tensorflow的阶、形状、数据类型