深入类的方法
--构造函数。
示例--
static void Main(string[] args){
SE se=new SE();
se.Name="天天向上";
se.Age="15";
se.ID="110";
Console.WriteLine(se.SayHi());
}
示列中可以看到调用构造函数创建SE对象并为其赋值,如不赋值的话,系统会给类的各个参数赋予默认值,
构造函数特点:
1.方法名和类名相同
2.没有返回值类型
3.主要完成对象的初始话工作
--无参构造函数
访问修饰赋 类名(){
//方法体
}
示例--
public class SE
{
public string Id { get; set; }
public string Name { get; set; }
public string Age { get; set; }
public string Money { get; set; }
public SE()
{
this.Id = "1";
this.Name = "王维娜";
this.Age ="15";
this.Money = "5000";
}
public void mian() {
Console.WriteLine("学号{0} \t 名字{1} \t 年龄{2} \t金钱{3}",Id,Name,Age,Money);
}
static void Main(string[] args)
{
SE se = new SE();
se.mian();
Console.ReadLine();
}
--带参构造函数
访问修饰赋 类名 (参数列表){
//方法体
}
示例--
public class SE
{
public string Id { get; set; }
public string Name { get; set; }
public string Age { get; set; }
public string Money { get; set; }
public SE(string id, string Name, string Age, string money)
{
this.Id = id;
this.Name = Name;
this.Age =Age;
this.Money = money;
}
public void mian() {
Console.WriteLine("学号{0} \t 名字{1} \t 年龄{2} \t金钱{3}",Id,Name,Age,Money);
}
static void Main(string[] args)
{
SE se = new SE("11","wang","1","500000000");
se.mian();
Console.ReadLine();
}
--方法重载
构造函数的重载
示例--
Public Class Player{
public void play(乐器){
//弹奏乐器
}
public void play(歌曲){
//演唱歌曲
}
public void play(剧本){
//根据剧本表演
}
}
从上面的示列可已总结出重载的特点:
1.方法名相同
2.方法的参数不同和方法的个数不同
3.在同一个类中
注意:方法名和参数列表相同的方法,仅是返回值类型不同,不能称为方法重载。
示列--
SE类
public class SE
{
public string Id { get; set; }
public string Name { get; set; }
public string Age { get; set; }
public string Money { get; set; }
public SE(string id, string Name, string Age )
{
this.Id = id;
this.Name = Name;
this.Age =Age;
this.Money = "10000";
}
public void mian() {
Console.WriteLine("学号{0} \t 名字{1} \t 年龄{2} ",Id,Name,Age);
}
}
PM类
public class PM
{
public string Id { get; set; }
public string Name { get; set; }
public string Age { get; set; }
public string Money { get; set; }
public PM(string id, string Name, string Age )
{
this.Id = id;
this.Name = Name;
this.Age =Age;
this.Money = "100000";
}
public void mian()
{
Console.WriteLine("学号{0} \t 名字{1} \t 年龄{2}", Id, Name, Age);
}
方法重载--
public class CommpSalary
{
public void Pay(SE se) {
string money = se.Money;
Console.WriteLine("员工工资" + money);
}
public void Pay(PM pm)
{
string money = pm.Money;
Console.WriteLine("经理工资"+money);
}
}
测试——————————
class Program
{
static void Main(string[] args)
{
CommpSalary sa = new CommpSalary();
PM pm = new PM("111", "经理", "20");
pm.mian();
sa.Pay(pm);
SE se = new SE("11","员工","1");
se.mian();
sa.Pay(se);
Console.ReadLine();
}
}
--对象交互
每个类都有自己的特性和功能,我们把它们封装为属性和方法对象之间通过属性和方法进行交互,可以认为方法的参数及方法的返回值都是对象相互传递的信息
示列
一个TV类
public class Tv
{
//维护一个状态
bool state = false;//关闭
public void Open()
{
if (state==false)//关闭的
{
state = true;//状态切换到开 机状态
Console.WriteLine("电视机打开啦!!!!!~~~~~");
}
}
public void Close()
{
if (state == true)//开着的
{
state = false;//状态切换到关 机状态
Console.WriteLine("电视机关闭啦!!!!!~~~~~");
}
}
public void ChangeChannel(string channelName)
{
if (state==true)
{
Console.WriteLine("您已经成功切换到"+channelName);
}
}
一个遥控器
//遥控器
public class Contol
{
public void Open(Tv tv)
{
tv.Open();
}
public void Close(Tv tv)
{
tv.Close();
}
public void ChangeChannel(Tv tv,string channelName)
{
tv.ChangeChannel(channelName);
}
}
测试————————————————
class Program
{
static void Main(string[] args)
{
//01.买一个电视机
Tv tv = new Tv();
//02.买一个遥控器
Contol control = new Contol();
//03.进行操作
control.Open(tv);
control.ChangeChannel(tv, "凤凰卫视");
control.Close(tv);
}
总结:
1.带参构造函数提供初始化对象时的多种选择,我们可以有效的选择初始化相应的信息。
2.如果没用给类添加显式构造函数,那么系统在初始化对象时会调用隐式构造函数,并且系统会根据属性的数据类型给其赋予默认值。
3.构造函数通常于类名相同,构造函数不声明返回值,一般情况下,构造函数总是public类型
4.方法重载是指方法名相同,而参数类型和参数个数不同,在同一个类中,构造函数和普通方法都可以重载
5.同一个类中,方法名相同,参数列表相同,但返回值类型不同的方法不能构成方法重载
6.在面向对象的程序中,对象通过公开方法和属性完成与其他对象的交互,可以认为方法的参数及方法的返回值都是对象间相互传递的消息。

浙公网安备 33010602011771号