zoukankan      html  css  js  c++  java
  • 2月1日 c#面向对象

    面向对象的核心概念

    (1 )封装

    封装的类=数据+对此数据所进行的操作(即属性+方法)

    通俗的说,封装就是:包起外界不必需要知道的东西,只向外界展露可供展示的东西。

    (2)抽象

    在使用面向对象的方法设计一个软件系统时,首先就要区分出现实世界的 事物所属的类型。

    分析问题的思路:

    首先将将实物的共性全抓出来。(也就是对它进行抽象)

    然后在逐个分析异性。(也就是具体)

    (3)继承

    不是我们认为的遗传的继承, 而是包含的意思。

    (4)多态

    基类拥有的特性,子类一定有,

    用抽象的类编程 。

    2 类与对象

    类是面向对象编程的 基本单元。(一切皆为对象 )

    只有声明为public的变量和函数可以被外界调用,其余声明为private的变量和函数是私有的,只能由自己的类使用。

    案例1、

    求游泳池的面积:

    class yongchi
        {
            public int r;
            public double zhouchang()
            {
                double c = 2 * 3.14 * r;
                return c;
            }
            public double mianji()
            {
                double s = 3.14 * r * r;
                return s;
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                yongchi small = new yongchi();
                yongchi big = new yongchi();
    
                Console.Write("游泳池的半经");
                small.r = Convert.ToInt32(Console.ReadLine());
    
                Console.Write("广场的半经");
                big.r = Convert.ToInt32(Console.ReadLine());
    
                Console.WriteLine("游泳池的周长为" + small.zhouchang());
                Console.WriteLine("游泳池的面积为" + small.mianji());
    
                Console.WriteLine("广场的周长为" + big.zhouchang());
                Console.WriteLine("广场的面积为" + (big.mianji() - small.mianji()));
    
            }
        }

    效果图:

    案例2、

    狗咬狗

    class Dog
        {
            public string Name;
            public int HP=100;
            public void Bark(Dog op)
            {
                Console.Write(Name+"咬了"+op.Name+"一口,");
                op.HP -= 5;
                Console.Write(op.Name+"剩余血量为"+op.HP+"
    ");
            }
        }
        class Class1
        {
            static void Main(string[] args)
            {
                Dog d1=new Dog();
                Dog d2=new Dog();
    
                d1.Name = "旺财";
                d2.Name = "大黄";
                d1.Bark(d2);
                d2.Bark(d1);
                d1.Bark(d2);
                d2.Bark(d1);
            }
        }

    效果图:

    案例3、

     class Ren
        {
            public string Name;
            private int Height;
            private int Weight;
    
            public void H1(int h)
            {
                if (h <= 0 || h >= 300)
                {
                    Console.WriteLine("你输入的身高有错误");
                }
                else
                {
                    Height = h;
                }
            }
            public void W1(int w)
            {
                if (w <= 0 || w >= 1000)
                {
                    Console.WriteLine("你输入的体重有错误");
                }
                else
                {
                    Weight = w;
                }
            }
            public void Speak()
            {
                Console.WriteLine("我叫" + Name + ",体重" + Weight + "千克,身高" + Height + "厘米");
            }
            public void yundong()
            { 
            }
        }
        class Class2
        {
            static void Main(string[] args)
            {
                Ren r1 = new Ren();
    
                r1.Name = "小明";
                Console.Write("请输入身高:");
                int h1 = Convert.ToInt32(Console.ReadLine());
                Console.Write("请输入体重:");
                int w1 = Convert.ToInt32(Console.ReadLine());
                r1.H1(h1);
                r1.W1(w1);
    
                r1.Speak();
    
            }
        }

    效果图:

  • 相关阅读:
    jquery select取值,赋值操作
    数据库性能监测工具
    Redis集群搭建与简单使用
    Docker安装指南
    yum常用操作
    centos6.8 安装Python2.7后, yum出现“No module named yum”错误
    Docker使用阿里云docker镜像加速
    高并发缓存架构
    mysql主从复制-方案1
    redis队列操作
  • 原文地址:https://www.cnblogs.com/tzq9308/p/4266123.html
Copyright © 2011-2022 走看看