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.实例化对象的两种形式
运行结果