C#(十五)之C#中的类

53 篇文章 1 订阅
22 篇文章 0 订阅

C#中的类。

首先定义一个类文件。

点击项目 -> 添加类(这里没有办法截图)理解万岁。

C#中的类与PHP中的类是类似的,只要能实例化,在哪都可以调用。

person gc = new person("GC",23,new cat());  // 实例化
Console.WriteLine("我叫{0},{1}岁了,我有一只猫{2}", gc.name, gc.age, gc.jerry.Method());

具体执行请看下方代码:

静态变量的定义:

public static int killCount;
调用:
类名 + 变量名(不用实例化)

静态方法的调用:

类中的静态方法,在不实例化类的情况下,直接调用就好。

stat.Put();

主文件代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using gc; //使用自己定义类的命名空间
  
namespace gc
{
    class Program
    {
        /* C#主要的运行函数,就是main函数 */
        static void Main(string[] args)
        {
            /*声明对象*/
            cat Tom = new cat();
            Tom.name = "Tom"; // 设置姓名
            Tom.age = 23;  //设置年龄
            Tom.Method(); // 调用函数方法
            for (int i = 0; i < 5; i++)
            {
                Tom.CatchMouse();  //调用捉老鼠函数
            }
            person gc = new person("GC",23,new cat());
            Console.WriteLine("我叫{0},{1}岁了,我有一只猫{2}", gc.name, gc.age, gc.jerry.Method());
            wolf w = new wolf();
            tigger[] t = new tigger[5];  //参数是一个数组,对象数组
            for (int i = 0; i < t.Length; i++)
            {
                t[i] = new tigger();
            }
            Console.WriteLine("我们现在共有{0}只狼",tigger.killCount);
            for (int i = 0; i < t.Length; i++)
            {
                // 要给每只狼都赋一个对象
                t[i].goHome();
                w.seeTigger();
            }
            /*类中的静态方法不需要实例化,直接调用就行*/
            stat.Put();
        }
        /*定义一个静态方法类*/
        class stat {
            public static void Put()
            {
                Console.WriteLine("我是一个静态方法");
            }
        }
        class wolf {
            public void seeTigger() {
                if (tigger.killCount > 4)
                {
                    Console.WriteLine("上,兄弟们");
                }else {
                    Console.WriteLine("静观其变");
                }
            }
        }
        class tigger {
            public static int killCount;
            public tigger(){
                killCount++;
            }
            //tigger();
            public void goHome() {
                if (killCount > 5)
                {
                    Console.WriteLine("进攻" + killCount);
                }
                else {
                    Console.WriteLine("集合" + killCount);
                }
            }
        }
        class person {
            public string name;
            public int age;
            public cat jerry;
            public person(string getName,int getAge,cat catValue){
                name = getName;
                age = getAge;
                jerry = catValue;
            }
        }
    }
}
}

类文件:cat.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace gc
{
    class cat
    {
        public string name;       // 共有的
        public int age;
        private int miceCount = 0;// 私有的
        /*私有行为*/
        private void Hello()
        {
            Console.WriteLine("只有我自己能用");
        }
        /*公共行为*/
        public string Method()
        {
            return "我叫jerry";
           
        }
        /*捉老鼠*/
        public void CatchMouse()
        {
            miceCount++;
            //Hello();
            Console.WriteLine("我抓了{0}只老鼠", miceCount);
        }
    }
}

这个没啥可说的,使用方法基本与PHP的类基本类似。就是写法不太一样。

这里旧事重提说一下public和private两个修饰符。

Public:共有的,谁都可以用。

Privete:私有的,只有你能用。

简单粗暴,后边应该会再详细看,关于面向对象部分,在做PHP的时候对于思想可能理解的不是很透彻。

有好的建议,请在下方输入评论。

欢迎访问个人博客
https://guanchao.site

欢迎访问小程序:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值