C#(类和对象、自定义类)

1.类和对象

常见的类:
object类;TextBo文本控制类;Button按钮类;Form窗体类;label标签控件类;GroupBox分组类;ProgressBar进度条类,SerialPort串口类;Times定时器类;MessageBox窗口提示类;String字符串类;Convert类型转换类;Console输入输出流类;Exception系统异常类;Image图像类;SystemSounds系统提示音类;Bitmap位图类;Process进程类;Thread进程类。

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

namespace 类和对象
{
    class Program
    {
        static void Main(string[] args)
        {
            //使用指定的格式信息,将指定对象(后跟当前行终止符)的文本表示形式写入标准输出流。
            Console.WriteLine("HELLO",10);
            //从标准输入流读取下一行字符。
            Console.ReadLine();
        }
    }
}

运行结果
在这里插入图片描述
2 自定义类和对象

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

class Test
{   
    public string str;
}

namespace 类和对象
{
    class Program
    {
        static void Main(string[] args)
        {
            //实例化一个对象
            Test test1 = new Test();
            Test test2 = new Test();

            test1.str = "aa";
            test2.str = "bb";

            Console.WriteLine(test1.str + "  " + test2.str);
            Console.ReadLine();
        }
    }
}

在这里插入图片描述
3.创建带有参数的构造函数
在这里插入图片描述

上述代码会报错,因为我们创造的构造函数是私有的。将private改为public就OK喽。

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

class Test
{   
    public string str;

    public Test(string Init)
    {
        str = Init;
    }
}

namespace 类和对象
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "aa";
            Test test1 = new Test(str);
            Console.WriteLine(test1.str);
            Console.ReadLine();
        }
    }
}

在这里插入图片描述
运行结果
在这里插入图片描述
4.实例化对象的两种形式
在这里插入图片描述
运行结果
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值