一.静态多态
1.静态多态:编译时实现
2.方法的重载:多个方法拥有相同的方法名称,而参数个数或类型不同
class Program
{
static void Main(string[] args)
{
PrintHello();
PrintHello("world");
Console.ReadLine() ;
}
public static void PrintHello()
{
Console.WriteLine("Hello");
}
public static void PrintHello(string toWho)
{
Console.WriteLine("Hello {0}", toWho);
}
}
运行结果
方法重载和方法覆盖的不同就是在于方法重载,方法有相同的方法名称而参数不同。但是在方法覆盖中方法不仅有相同的方法名称,而且参数必须相同,返回值类型也相同。方法覆盖只能用在派生类中。
3.运算符的重载
class Program
{
static void Main(string[] args)
{
PrintHello();
PrintHello("world");
Complex c1 = new Complex();
Complex c2 = new Complex();
c1.Number = 2;
c2.Number = 3;
Console.WriteLine((c1 + c2).Number);
Console.ReadLine() ;
}
public static void PrintHello()
{
Console.WriteLine("Hello");
}
public static void PrintHello(string toWho)
{
Console.WriteLine("Hello {0}", toWho);
}
}
class Complex
{
public int Number { get; set; }
public static Complex operator + (Complex c1,Complex c2)
{
Complex c = new Complex();
c.Number = c1.Number + c2.Number;
return c;
}
}
运行结果
即,加号的重载,因为之前complex并没有相加的功能,但是对加号重载后,complex就可以互相相加了
(complex类有一个名为number的属性)
二.动态多态:运行时实现
1.
class Program
{
static void Main(string[] args)
{
Human human1 = new Man();
Human huamn2 = new Woman();
human1.CleanRoom();
huamn2.CleanRoom();
Console.ReadLine();
}
}
class Human
{
public virtual void CleanRoom()
{
Console.WriteLine("Human Clean Room");
}
}
class Man:Human
{
public override void CleanRoom()
{
Console.WriteLine("Man Clean Room Slowly");
}
}
class Woman:Human
{
public override void CleanRoom()
{
Console.WriteLine("Woman Clean Room Quickly");
}
}
运行结果
三.静态方法、静态类 static
1.静态类不可以被继承,不能被实例化
在密闭类里,可以既有静态方法,又有非静态方法
在静态类里,只能存在静态成员
2.静态方法、静态变量等在程序一开始执行就会分配相应的内存,而非静态的是在被实例化的时候分配内存
3.调用静态类里的方法和变量
调用语法:类名.方法名
例如
public static class OwnerofficialRoom
{
public static void AllMyPersonalItems()
{
Console.WriteLine("All Items in this rooms are personal to me no one else can access or inherit me");
}
}
class HouseStaticClass
{
static void Main(string[] args)
{
OwnerofficialRoom.AllMyPersonalItems();
Console.ReadLine();
}
}
运行结果
4.在非静态类中创建静态方法
例如
public class OwnerofficialRoom
{
public static void AllMyPersonalItems()
{
Console.WriteLine("All Items in this rooms are personal to me no one else can access or inherit me");
}
public void non_staticMethod()
{
Console.WriteLine("You need to create an Object to Access Me :(");
}
}
class StaticmethodClass
{
static void Main(string[] args)
{
OwnerofficialRoom.AllMyPersonalItems();
OwnerofficialRoom obj = new OwnerofficialRoom();
obj.non_staticMethod();
Console.ReadLine();
}
}
运行结果
四.接口
1.接口里面只能存在方法的声明,不可以实现方法。抽象类中既可以声明方法,也可以实现方法。
2.有时候我们确定有些方法派生类中要用,但是每个派生类对该方法的需求不同,那么就可以使用接口,把该方法写到接口里面,让每个派生类自己去实现自己的需求。
3.在抽象类中,可以有抽象方法也可以有非抽象方法。但是接口里的所有方法都被默认为抽象方法,所有在接口中声明的方法,在派生类中都必须实现。
例如
interface GuestInterface
{
void GuestWelcomeMessage();
void NoofGuestes();
}
interface FriendsandRelationsInterface
{
void friendwelcomemessage();
void FriendName();
}
class HouseOwnerClass:GuestInterface,FriendsandRelationsInterface
{
public void GuestWelcomeMessage()
{
Console.WriteLine("All guests are well come to our home");
}
public void NoofGuestes()
{
Console.WriteLine("Total 15 Guestes has visited");
}
public void friendwelcomemessage()
{
Console.WriteLine("Welcome to our Home");
}
public void FriendName()
{
Console.WriteLine("Friend name is :Afraz");
}
static void Main(string[] args)
{
HouseOwnerClass obj = new HouseOwnerClass();
obj.GuestWelcomeMessage();
obj.NoofGuestes();
obj.friendwelcomemessage();
obj.FriendName();
Console.ReadLine();
}
}
运行结果