2015 5.16 C# 继承和多态

时间:2023-03-09 01:51:45
2015 5.16  C# 继承和多态

类的层次结构有两种基本的构造方式  自顶向下  自底向上

基类的保护成员是指允许派生类的方法代码访问,而不是指通过派生类的对象访问

如果基类中的字段通过公有且可读写的属性进行了封装,那么建议将字段定义为私有的,这样包括其派生类在内的所有其他类型都必须通过属性进行访问

隐藏基类成员

如果派生类中定义了与基类相同的成员,默认情况下基类的成员在派生类中会被隐藏,即派生类成员覆盖了基类成员   用new来修饰提高代码可读性

base 关键字,当派生类隐藏了基类成员时,base关键字就能发挥作用:直接写出的成员名表示派生类的成员,增加了base引用的成员表示被隐藏的基类成员

用base声明创建对象时调用的基类重载构造函数

虚拟方法和重载方法

 using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main()
{
foreach (Automobile a in GetAutos())
{
a.Speak();
Console.WriteLine("{0}行驶1000公里需要{1}小时", a.Name, a.Run());
}
//Console.ReadLine();
}
static Automobile[] GetAutos()
{
Automobile[] autos = new Automobile[];
autos[] = new Bus("客车", );
autos[] = new Truck("东风卡车", );
autos[] = new Truck("黄河卡车", );
autos[] = new Automobile("汽车", , );
return autos;
}
} public class Automobile
{
private string name;
public string Name
{
get { return name; }
} private float speed;
public float Speed
{
get { return speed; }
} private float weight;
public float Weight
{
get { return weight; }
set { weight = value; }
} public Automobile(string name, float speed, float weight)
{
this.name = name;
this.speed = speed;
this.weight = weight;
} public virtual float Run(float distance)//虚拟方法
{
return distance / speed;
} public virtual void Speak()
{
Console.WriteLine("汽车鸣笛……");
}
}
public class Bus : Automobile
{
private int passangers;
public int Passangers
{
get { return passangers; }
set { passangers = value; }
}
public Bus(string name, int passangers)
: base(name, , )
{
this.passangers = passangers;
}
public override void Speak()
{
Console.WriteLine("嘀……嘀……");
}
} public class Truck : Automobile
{
private float load;
public float Load
{
get { return load; }
set { load = value; }
}
public Truck(string name, int load)
: base(name, , )
{
this.load = load;
}
public override float Run(float distance)//重载方法
{
return ( + load / Weight / ) * base.Run(distance);
}
public override void Speak()//重载方法
{
Console.WriteLine("叭……叭……");
}
} }

抽象类和抽象方法(抽象类不能直接用new创建对象,但可以与派生类的实例相关联)

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main()
{
Vehicle v1 = new Train();
v1.Speak();
Console.WriteLine("行驶1000公里需{0}小时", v1.Run());
v1 = new Truck(, );
v1.Speak();
Console.WriteLine("行驶1000公里需{0}小时", v1.Run());
Console.ReadLine();
}
} public abstract class Vehicle
{
private float speed;
public float Speed
{
get { return speed; }
} public virtual float Run(float distance)
{
return distance / speed;
} public abstract void Speak();//抽象方法:无执行代码 public Vehicle(float speed)
{
this.speed = speed;
}
} public class Train : Vehicle
{
public Train()
: base()
{ } public override void Speak()
{
Console.WriteLine("呜……");
}
} public abstract class Automobile:Vehicle
{
public Automobile(float speed):base(speed)
{ } public override abstract void Speak();//重载+抽象
} public class Truck : Automobile
{
private float weight;
public float Weight
{
get { return weight; }
} private float load;
public float Load
{
get { return load; }
} public Truck(int weight, int load)
: base()
{
this.weight = weight;
this.load = load;
} public override float Run(float distance)
{
return ( + load / Weight / )* base.Run(distance);
} public override void Speak()
{
Console.WriteLine("叭……叭……");
}
}
}

密封类和密封方法

一些类型不允许或是不需要再有派生类型   sealed   密封类是对类继承的截止   密封方法是对类继承中方法重载的截止

 sing System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main()
{
Student s1=new Student(,"王小红");
Console.WriteLine(s1);
Student s2=new Graduate(,"王晓红","张大伟");
Console.WriteLine(s2);
Console.WriteLine(s1.Equals(s2));
Console.ReadLine();
}
} public class Student
{
private string name;
public string Name
{
get { return name; }
} private int id;
public int ID
{
get { return id; }
} public Student(int id, string name)
{
this.id = id;
this.name = name;
} public override string ToString()
{
return string.Format("学号{0},姓名{1}", id, name);
} public sealed override bool Equals(object obj)
{
if (obj is Student && ((Student)obj).id == this.id)
return true;
else
return false;
}
} public class Graduate : Student
{
private string supervisor;
public string Supervisor
{
get { return supervisor; }
set { supervisor = value; }
} public Graduate(int id, string name, string supervisor)
: base(id, name)
{
this.supervisor = supervisor;
} public override string ToString()
{
return base.ToString() + ",导师:" + supervisor;
}
}
}