目录
类和对象
类的声明
修饰符 class 类的名称
{
类的成员
}
类的成员包括数据成员和函数成员,他们就是属性和方法
修饰符
new
internal 可省略不写
public
protected
private
abstract
类的实例
类名 实例名 = Student(参数);
例如:
Student 张三 = new Student();
Person myTest = new Person("LiFei",25,001);
拓展
类是一种“引用类型”
结构体是一种“值类型”
构造函数和析构函数
引:构造函数为对象分配空间,完成对象初始化的过程、
析构函数完成释放实例化对象所占的内存空间
构造函数
1.构造函数封城通常与类名相同,大小写一致的。
2.构造函数不声明返回值类型,也没有void修饰符,可以有参数,也可以没有参数。
3.构造函数通常都是public类型的,如果声明为private类型,说明这个类不能被实例化。
注:只要创建基于Person类的对象,这个构造函数就会被调用,并在初始化过程中通过参数传递的方式为数据成员指定初始值。
实例构造函数可以被重载,但不能被继承。
析构函数
析构函数用于释放实例,回收对象所占用的资源。析构函数名称也与类的名称相同,但是在名称前加了运算符“~”。在一个类中只能有一个析构函数。他不能被重载也不能被继承,析构函数是自动调用的。
实例如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Chap3_4
{
class Student
{
public Student()
{
Console.WriteLine("Constructing a student……");
}
~Student()
{
Console.WriteLine("Destroying a student……");
}
}
class Program
{
static void Main(string[] args)
{
Student b = new Student();
}
}
}
字段和属性
常量
在类中声明常量成员时使用关键字const.
例如:
class MyClass
{
public const int a = 1;
public const double b = 2.0;
}
静态属性调用如下:
using System;
using System.Collections.Generic;
using System.Text;
namespace TestApp01
{
class Student
{
private static int intNo;
private string name;
private int age;
private static int counter = 0;
public static int IntNo { get => intNo; set => intNo = value; }
public string Name { get => name; set => name = value; }
public int Age { get => age; set => age = value; }
public static int Counter { get => counter; set => counter = value; }
public Student(string name, int age)
{
this.name = name;
this.age = age;
}
public Student()
{
}
public void Disply()
{
}
}
}
using System;
namespace TestApp01
{
class Program
{
static void Main(string[] args)
{
Student.IntNo = 20111032;
Student s = new Student();
s.Name = "Deng Liwei";
Console.WriteLine("Student.intNo = {0}", Student.Counter);
Console.WriteLine("s.Name = {0}", s.Name);
Console.WriteLine("s.Age = {0}", s.Age);
Console.Read();
}
}
}
类的方法
方法是表示类或对象行为的成员函数。
方法的声明
修饰符 返回值类型 方法名称(形参列表)
{
方法体
}
方法的参数类型
1.值参数
值参数是没有使用任何修饰符声明的参数。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Chap3_10
{
class Program
{
public static void Fun(int a)
{
a += 5;
Console.WriteLine("a={0}", a);
}
static void Main(string[] args)
{
int b = 10;
Fun(b);
Console.WriteLine("b={0}", b);
Console.ReadLine();
}
}
}
2.引用参数
声明方法时,在形参列表中使用ref修饰符声明的形参是引用参数。这样实参和形参都指向相同的地址。所以在程序中对引用参数的修改就是对响应的实参的修改。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Chap3_11
{
class Program
{
public static void Fun(ref int a)
{
a += 5;
Console.WriteLine("a={0}", a);
}
static void Main(string[] args)
{
int b = 10;
Fun(ref b);
Console.WriteLine("b={0}", b);
Console.ReadLine();
}
}
}
注:声明和调用有引用参数的方法时,形参和实参前面都要使用ref修饰符。
3.输出参数
在形参列表中使用out修饰符声明的形参是输出函数。
简单来说就是给out所修饰的形参或实参传值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Chap3_12
{
class Program
{
public static void calc(int x, int y, out int cAdd, out int cSub)
{
cAdd = x + y;
cSub = x - y;
}
static void Main(string[] args)
{
int n1 = 10, n2 = 5;
int add, sub;
calc(n1, n2, out add, out sub);
Console.WriteLine("n1={0},n2={1}", n1, n2);
Console.WriteLine("add={0}", add);
Console.WriteLine("sub={0}", sub);
Console.ReadLine();
}
}
}
注:声明和调用有输出参数的方法时,形参和实参前面都要使用out修饰符。
4.数组参数
在形参列表中使用params修饰符声明的形参是数组参数。
数组参数适用于方法的参数个数不确定的情况,这样可以在调用方法过程中传递实参的个数。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Chap3_13
{
class Program
{
static void MutiParams(params int[] var)
{
for (int i = 0; i < var.Length; i++)
{
Console.WriteLine("var[{0}]={1}", i, var[i]);
}
Console.WriteLine("-------------------------");
}
static void Main(string[] args)
{
int[] arr = { 10, 20, 30 };
MutiParams(arr);
MutiParams(100, 200);
MutiParams();
}
}
}
静态方法和实例方法
在声明方法时如果使用了static修饰符,则该方法被称为静态方法;如果没有使用static修饰符,则该方法被称为实例方法。
静态方法只能通过类而不能通过实例访问。类名.静态方法名();
静态方法只能访问类中的静态成员。在静态方法中引用this也是错误的。
注:静态变量初始化默认为0;
方法的重载
在类中,如果有两个或两个以上的方法名称相同,但是参数的个数或者参数的类型不同,这被称为方法的重载。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Chap3_15
{
class Program
{
static void Area(float r)
{
Console.WriteLine(1.14 * r * r);
}
static void Area(float a, float b)
{
Console.WriteLine(a * b);
}
static void Area(string n)
{
Console.WriteLine("hello " + n);
}
static void Main(string[] args)
{
Area(10);
Area(10, 10);
Area("Brown");
}
}
}
运算符的重载
对运算符进行重载需要使用operator关键字来定义静态成员函数。运算符重载的格式如下:
public static 返回值类型 operator 运算符(参数列表)
在运算符重载时,参数只能是值参数。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Chap3_16
{
class Point
{
int x, y;
public Point(int a, int b)
{
x = a;
y = b;
}
public static Point operator ++(Point p)
{
p.x++;
p.y++;
return p;
}
public void Display()
{
Console.WriteLine("Point.x={0},Point.y={1}", x, y);
}
public static Point operator +(Point p1, Point p2)
{
Point p = new Point(0, 0);
p.x = p1.x + p2.x;
p.y = p1.y + p2.y;
return p;
}
}
class Program
{
static void Main(string[] args)
{
Point a = new Point(10, 20);
Point b = new Point(30, 40);
a = a + b;
a.Display();
a++;
a.Display();
}
}
}