inherit
When multiple classes have a lot of similar data, such as in a game, the Boss class and the mobs Enemy class, they all have the hp attribute, but there are also different places, this time can be maintained by inheritance = this two classes.
Inherited types:
- implement inheritance
- interface inheritance
Monster base class BaseEnemy
class BaseEnemy
{
private float hp;
private float spped;
public float HP
{
set { hp = value; }
get { return hp; }
}
public float Speed
{
set { hp = value; }
get { return spped; }
}
public void AI()
{
Console.WriteLine("这是基类的AI方法");
}
public void Move()
{
Console.WriteLine("这是基类的移动方法");
}
}
Subclass Boss
class Boss:BaseEnemy //继承有怪物基类
{
public void Attack()
{
AI();
Move();
//hp = 100;
HP = 100;
//父类里面只有公有变量(public)和函数成员可以直接在子类访问
Console.WriteLine("这是Boss特有的攻击方法");
}
}
Main function:
class Program
{
static void Main(string[] args)
{
Boss boss = new Boss();
boss.Attack();
//父类里面所有的数据的数据成员和函数成员都会继承到子类里面
Console.ReadKey();
}
}
operation result:
这是基类的AI方法
这是基类的移动方法
这是Boss特有的攻击方法
The object declared by the parent class can be constructed by the subclass, but the object declared by the subclass cannot be constructed by the parent class.
Example:
BaseEnemy baseEnemy = new Boss();
Boss boss = (Boss)baseEnemy;
boss.Attack();
virtual method
Virtual functions do not have to be overridden
When multiple monster classes have different animation systems, the base class can use virtual methods for subclasses to call, and the subclasses are responsible for overriding the virtual methods and then improving their details
Parent class BaseEnemy:
public virtual void Animataion() //提供子类重写的虚方法
{
Console.WriteLine("这是基类的动画方法");
}
Subclass Boss:
public override void Animataion()
{
Console.WriteLine("这是Boss特有的动画");
}
Call result:
Boss boss = new Boss();
boss.Animataion();
//调用结果:
//这是Boss特有的动画
abstract method
Abstract methods must be overridden
An abstract class cannot be instantiated. An abstract class can contain an ordinary function and an abstract function. An abstract function has only a function definition without a function body.
this and base keywords
- this can access the fields, properties and methods that can only be defined in the current class. You can use this to give hints, and you can access it without this.
- base calls the method of the parent class
Sealed class and sealed method sealed
For classes, this means that the class cannot be inherited. For a method, indicates that the method cannot be overridden
Features:
Can prevent code confusion caused by overriding some classes
Derived class constructor
In the subclass, the constructor of the parent class will be called first, and then the constructor of the subclass will be used.
example:
Public class Boss{
public Boss():base(){
//Do something
}
}
modifier
protect protected methods, only derived classes can call
static static methods, which can only be accessed by class name
Defined and Implemented Interfaces
Each class can implement multiple interfaces, but can only inherit from one class
Interface class:
interface Ifly()
{
void Fly();
}
Implementation class:
class Brid : IFly
{
public void Fly()
{
//TODO
}
}
Generics
Defining a generic means defining a class in which the types of some fields in this class are uncertain, and these types can be determined at the time of the constructor
Define a generic class with a constructor:
class Generic<T> //定义一个泛型T的数据类型,在构造函数的时候确定T的类型
class Generic<T> //定义一个泛型T的数据类型,在构造函数的时候确定T的类型
{
private T a;
private T b;
public Generic(T a,T b)
{
this.a = a;
this.b = b;
}
public void GetSum() //参数的相连
{
Console.WriteLine( a + "" + b);
}
}
Main program:
class Program
{
static void Main(string[] args)
{
var v= new Generic<string>("www.","baidu.com");
v.GetSum();
Console.ReadKey();
}
}
operation result:
www.baidu.com
generic method
public static string GetSum<T1,T2>(T1 a,T2 b) //带有两个形参的泛型方法
{
return a + " " + b;
}
Main program:
static void Main(string[] args)
{
string s = GetSum<int, double>(1, 3.14159);
Console.WriteLine(s);
Console.ReadKey();
}
Output result:
1 3.14159