类是用来描述现实世界事务,事务一般有特征(属性)、动作(行为)。如学生(学号、姓名、院系、专业、…;注册、缴费、选课等);类是具有相同特征与行为的一类事务(对象)整体。类是抽象的。
对象是类中的一个实例,是具体的。如学生(2010002,张三、数学系、信息与计算专业等)。
类是解决一个应该是什么的问题。对象是解决具体是什么的问题。
class Student
{
private string sno, sname, sdept; //属性域
private int sage;
public void setInfo(string no, string name, string dept, int age)
{
sno = no;
sname = name;
sdept = dept;
sage = age;
}
public void dispInfo()
{
Console.WriteLine(sno + " " + sname + " " + sdept + " " + age);
}
}
类一般需要生成对象,通过对象调用其属性以及函数。注意对象需要实例化。
实例化过程其实就是为某个对象分配其存储空间。
Student s1;//定义一个对象变量,并未实例化。s1=null;
s1=new Student();//将对象实例化,将新空间首地址(引用)赋予对象s1;
//Student s1=new Student();
Student s2=s1;
Student s3=new Student();
构造函数与类同名,没有任何返回值类型(不能在构造函数前加void),一般建议放在public部分。
构造函数一般是为了对象中的属性成员赋予初始值。
class Student
{
private string sno, sname, sdept; //属性域
private int sage;
public Student()
{
}
public Student(string sno,string name,string dept,int age)
{
this.sno = sno;
sname = name; sdept = dept; sage = age;
}
public void dispInfo()
{
Console.WriteLine(sno + " " + sname + " " + sdept + " " + sage);
}
}
构造函数如何调用?构造函数不允许通过对象去直接调用。在对象实例化时系统自动调用相应的构造函数。
Student s1 = new Student("2010003","李四","Math",20);
Student s2;
s2 = new Student("2010001","李四","Math",20);
函数重载:在一个类中,有一组函数,函数名称相同,但形参的类型或个数不同。这样一组函数构成函数重载。函数重载与函数返回值类型无关。
eg:
class A{
public void fun(int x){….}
public long fun(){…}
public void fun(double x){…}
public void fun(int x,int y){…}
}
A obj=new A();
obj.fun(10);
eg:
class B{
public void fun(int x){….}
public void fun(int x,int y){…}
public void fun(params int[] x){…}
}
B obj=new B();
obj.fun(10);//
obj.fun(10,20);//
obj.fun(10,30,40);//
class B{
public void fun(int x)
{
Console.WriteLine("fun(int x)");
}
public void fun(int x,int y){
Console.WriteLine("fun(int x,int y)");
}
public void fun(params int[] x){
Console.WriteLine("fun(params int[] x)");
}
}
构造函数重载:
class Student
{
private string sno, sname, sdept; //属性域
private int sage;
public Student()
{
Console.WriteLine("default构造函数调用");
}
public Student(string no, string name)
{
Console.WriteLine("core构造函数调用");
sno = no; sname = name;
}
public Student(string no,string name,string dept,int age)
{
Console.WriteLine("full构造函数调用");
sno = no; sname = name; sdept = dept; sage = age;
}
public void dispInfo()
{
Console.WriteLine(sno + " " + sname + " " + sdept + " " + sage);
}
}
class Ex1_1
{
static void Main(string[] args)
{
Student s1 = null;
Student s2 = new Student();
Student s3 = new Student("2010003","liu");
Student s4 = new Student("2010004", "wang","computer",23);
Console.ReadLine();
}
}
class Student
{
private string sno, sname, sdept; //属性域
private int sage;
public Student(string sno, string sname, string sdept,int sage)
{
this.sno = sno;
this.sname = sname;
this.sdept = sdept;
this.sage = sage;
}
public void dispInfo()
{
Console.WriteLine(sno + " " + sname + " " + sdept + " " + sage);
}
}
class Student
{
private string sno, sname, sdept; //属性域
private int sage;
public int Age
{
get
{
return this.sage;
}
}
public string Dept //属性访问器,Dept属性名
{
get
{
return this.sdept;
}
set
{
this.sdept = value;
}
}
public Student(string sno, string sname, string sdept,int sage)
{
this.sno = sno;
this.sname = sname;
this.sdept = sdept;
this.sage = sage;
}
public void dispInfo()
{
Console.WriteLine(sno + " " + sname + " " + sdept + " " + sage);
}
}
class Ex1_1
{
static void Main(string[] args)
{
Student s2 = new Student("200010","z","math",20);
s2.dispInfo();
s2.Dept = "Computer";//设置属性值
Console.WriteLine(s2.Dept);
s2.dispInfo();
Console.ReadLine();
}
}
示例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ex2013_07_22
{
class Point
{
private int _x, _y;//属性域变量
/*构造函数*/
public Point()
{
}
public Point(int x, int y)
{
this._x = x;
this._y = y;
}
/*属性访问器*/
public int X //属性名
{
get
{
return _x;
}
set
{
_x = value;
}
}
public int Y
{
get
{
return _y;
}
set
{
_y = value;
}
}
public override string ToString()
{
return "(" + _x + "," + _y + ")";
}
public void MoveX(int x)
{
this.X += x;
}
public void MoveY(int y)
{
this.Y += y;
}
public double distance(Point p)
{
return Math.Sqrt((this.X - p.X) * (this.X - p.X)+
(this.Y - p.Y) * (this.Y - p.Y));
}
}
class Ex1_1
{
static void Main(string[] args)
{
Point p1 = new Point();
p1.X = 10;
p1.Y = 10;
p1.MoveX(5);
Point p2 = new Point(20, 20);
Console.WriteLine("{0}与{1}距离:{2}",p1.ToString(),
p2.ToString(),p1.distance(p2));
Console.ReadLine();
}
}
}
eg:
Student [] s=new Student[17];//new 开辟一个能够存储17个元素的对象数组,s[0],s[1],…,s[16]=null;
s[0]=new Student("2010001","周");
s[0].dispInfo();
s[1].dispInfo();//错误,出现null引用异常。
eg:
Point[] p={new Point(1,1),new Point(2,1),….,};
p[0].X=10;
double d=p[0].distance(p[1]);
类中static 成员(属性域,函数);在运行前开辟存储空间或入口地址。
class TestStatic
{
private int x=0;
private static int y=0;
public TestStatic()
{
x++; y++;
}
public void disp()
{
Console.WriteLine(x+" "+y);
}
}
TestStatic t1 = new TestStatic();
TestStatic t2 = new TestStatic();
t1.disp();
eg:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ex2013_07_22
{
class TestStatic
{
public int x=0;//实例属性,对象属性
public static int y=0;//类属性
public TestStatic()
{
x++; y++;
}
public static void disp()//类函数
{
Console.WriteLine(y);
}
}
class Ex1_1
{
static void Main(string[] args)
{
TestStatic t1 = new TestStatic();
TestStatic t2 = new TestStatic();
TestStatic.y = 10;
TestStatic.disp();//static 只能通过类调用
//non static函数只能通过对象调用。
Console.ReadLine();
}
}
}
本章练习题下载地址:点此下载