using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication3
{
public abstract class Book
{
//抽象方法,不含主体,抽象方法所在类必须为抽象类,派生类必须实现该方法
public abstract void Introduce();
}
public class JavaBook : Book
{
//实现抽象方法,必须实现,注意!必须添加override关键字
public override void Introduce()
{
Console.WriteLine("I'm Java");
}
}
public class test
{
public test()
{
JavaBook javaBook = new JavaBook();
javaBook.Introduce(); //将调用JavaBook中Introduce()
Book book = new JavaBook();
book.Introduce(); //将调用JavaBook中Introduce()
}
public static void Main()
{
test t = new test();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
{
public abstract class Book
{
public virtual void Introduce()
{
Console.WriteLine("I'm book");
}
public virtual void SayHi()
{
Console.WriteLine("Hi, I'm book");
}
}
public class JavaBook : Book
{
public override void Introduce()
{
Console.WriteLine("I'm Java");
}
//注意,此方法没有override父类的方法
public void SayHi()
{
Console.WriteLine("Hi, I'm Java");
}
}
public class test
{
public test()
{
JavaBook javaBook = new JavaBook();
Book book = new JavaBook();
javaBook.Introduce(); //将调用JavaBook中Introduce()
book.Introduce(); //将调用JavaBook中Introduce()
javaBook.SayHi(); //将调用JavaBook中SayHi()
book.SayHi(); //将调用Book中SayHi()
}
public static void Main()
{
test t = new test();
}
}
}
(6)、abstract方法必须重写,virtual方法必须有实现(即便它是在abstract类中定义的方法).