面向对象 理解 C#复习

本文介绍面向对象编程的基本概念,包括封装、继承、多态等核心特征,并通过C#语言实现了一个简单的学生信息管理系统,演示了如何创建类和对象,以及如何进行数据的输入输出和排序。

面向对象:

      是基于万物皆对象这个哲学观点. 所谓的面向对象就是将我们的程序模块化,对象化,把具体事物的特性属性和通过这些属性来实现一些动作的具体方法放到一个类里面

通俗点讲:

一切都是对象

举例:

将一栋房子 比作一个对象 【房子:对象】。 从图上也可以看到这是一种新类别的房子【房子:类-Class】  如图

这就是对象,那对象应该包含什么呢?

面向对象的三项基本特征:封装、继承、多态。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace _11._24
{
    class Program
    {
        struct student
        {
            public string code;
            public string name;
            public double score;
        }
        static void Main(string[] args)
        {
            ArrayList al = new ArrayList();
            Console.Write("请输入学生人数:");
            int a = int.Parse(Console.ReadLine());
            for (int i = 0; i < a; i++)
            {
                student st = new student();
                for (; ; )
                {
                    Console.Write("请输入第{0}个学生的学号:", i + 1);
                    string b = Console.ReadLine();
                    if (b.StartsWith("S"))
                    {
                        st.code = b;
                        break;
                    }
                }
                for (; ; )
                {
                    Console.Write("请输入第{0}个学生的姓名:", i + 1);
                    string c = Console.ReadLine();
                    if (c != "")
                    {
                        st.name = c;
                        break;
                    }
                }
                for (; ; )
                {
                    Console.Write("请输入第{0}个学生的成绩:", i + 1);
                    try
                    {
                        double d = double.Parse(Console.ReadLine());
                        if (d <= 100 && d >= 0)
                        {
                            st.score = d;
                            break;
                        }
                    }
                    catch
                    { Console.WriteLine("请输入数字!"); }
                }
                al.Add(st);
            }
            for (int i = 0; i < al.Count - 1; i++)
            {
                for (int j = i + 1; j < al.Count; j++)
                {
                    student s1 = (student)al[i];
                    student s2 = (student)al[j];
                    if (s1.score < s2.score)
                    {
                        Object b = al[i];
                        al[i] = al[j];
                        al[j] = b;
                    }
                }
            }
            Console.Write("序号" + "\t" + "学号" + "\t" + "姓名" + "\t" + "成绩" + "\n");
            for (int i = 0; i < a; i++)
            {
                student s = (student)al[i];
                Console.Write((i + 1) + "\t" + s.code + "\t" + s.name + "\t" + s.score + "\n");
            }
            Console.ReadLine();
        }
    }
}
View Code

 

转载于:https://www.cnblogs.com/shadow-wolf/p/6097964.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值