代码1:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 抽象类_抽象方法
{
class Program
{
static void Main(string[] args)
{
//Person p = new Person();抽象类不能实例化
Student s = new Student();
s.name = "Henry";
s.ID = "123";
s.selfintroduce();
s.saySomething();
s.move();
Console.WriteLine();
StringBuilder conclusion = new StringBuilder();
conclusion.Append("小结:\r\n");
conclusion.Append("1.抽象类和抽象方法用abstract修饰\r\n");
conclusion.Append("2.抽象类可以包含非抽象方法\r\n");
conclusion.Append("3.抽象方法必须包含在抽象类中\r\n");
conclusion.Append("4.抽象类的派生类(该派生类可以也是抽象类)必须实现(override)抽象方法\r\n");
conclusion.Append("5.抽象方法不能有方法体\r\n");
conclusion.Append("6.抽象类不能实例化\r\n");
conclusion.Append("7.抽象类可以继承自抽象类\r\n");
Console.WriteLine(conclusion.ToString());
Console.ReadLine();
}
}
}
代码2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 抽象类_抽象方法
{
abstract class Animal
{
public void move()
{
Console.WriteLine("I am coming");
}
public abstract void f();
}
}
代码3:
using System;
namespace 抽象类_抽象方法
{
abstract class Person : Animal
{
public string name;
public abstract void selfintroduce();
public void saySomething()
{
Console.WriteLine("are you ok ?");
}
public override void f()
{
//
}
}
}
代码4:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 抽象类_抽象方法
{
class Student : Person
{
public string ID;
public override void selfintroduce()
{
Console.WriteLine("I am " + name);
}
}
}
调试结果: