
class Gameobject { } class Player :Gameobject { public void Attk() { } } class Master : Gameobject { public void Attk() { } } class Boss : Gameobject { public void Attk() { } } internal class Program { static void Main(string[] args) { Gameobject master = new Master(); } }
但有个问题,我在用的时候没法用master.Attk("攻击") 因为master是属于Gameobject的
于是就有了is 和as;

static void Main(string[] args) { Gameobject master = new Master(); if (master is Master) //判断是不是这个类型的 { Master m = new Master();//转化类型 m = master as Master; master.Attk() } }
还有更简单的写法,不用中间值 m,直接(master as Master).Attk()
看起来我们不需要判定,因为我们一眼就看出来,是不是这个类的,但
Gameobject[] objs = { new Player(), new Master(), new Boss() }; for (int i = 0; i < objs.Length; i++) { if (objs[i] is Master) { (objs[i] as Master).Attk(); } else if(...) { ... } ... }
在数组里数量多了就不好看出来,节省时间就可以用判断.


题目二using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 继承 { class Master { public void Attk() { } } class Boss : Master { public void Skill() { } } class Goblin : Master { } internal class Program { static void Main(string[] args) { Master[] master = new Master[10]; for (int i = 0; i < master.Length; i++) { Random r = new Random(); int num = r.Next(0, 2); if (num == 0) { master[i] = new Boss(); } else { master[i] = new Goblin(); } } for (int i = 0; i > master.Length; i++) { if (master[i] is Boss) { (master[i] as Boss).Skill(); } else { (master[i] as Goblin).Attk(); } } } } }

class Teacher //父类,基类 { public string name; public int number; public void SpeakName() { } } class TeachingTeacher : Teacher { public string sbject; //子类,派生类有自己的成员 public void SeakSubject() { } } internal class Program { static void Main(string[] args) { TeachingTeacher tt = new TeachingTeacher(); tt.number = 1; tt.name = "Leemoon"; } }



使用要点出来:


一个类分成两部分来写.


只能在分部类里面呢用,说简单点就是方法的声明和使用,分到两个分部类里面罢了.前面要加关键字,必须没有返回值.没有out和ref.


class Point { public int x; public int y; public static Point operator +(Point p1, Point p2)//可以让+重载运算. { Point p = new Point(); p.x = p1.x + p2.x; p.y = p1.y + p2.y; return p; }//不重载运算符,下面就不能直接运算. Point p1 = new Point(); p1.x = 1; p1.y = 2; Point p2 = new Point(); p2.x = 2; p2.y = 3; Point p3 = new Point(); p3 = p1+p2;
重载必须和静态类相关,至少一个:
class Point { public int x; public int y; public static Point operator +(int p1, Point p2)//第二个参数和类是一样的 { Point p = new Point(); p.x = p1.x + p2.x; p.y = p1.y + p2.y; return p; }
看起来就像一个和名字是"+"的函数一样
使用顺序位置要对:
Point p1 = new Point(); p1.x = 1; p1.y = 2; Point p2 = new Point(); p2.x = 2; p2.y = 3; Point p3 = new Point(); p3 = p1+2; //写反了,上面参数是先int后Point.会直接报错
从上面总结来看,参数不一样,也可以重载.
参数数量要看运算符要求:
p3=p1+p2+2 其实是先算的p1+p2,和在+2.没有连加的说法.
可重载的运算符太多,我们讲不可重载:
有个重点,条件运算符重载必须要成对出现,比如有<就要有>,有==就会有!=


相关信息
注意是为非静态类方法,拓展.静态类无法拓展.

static class Tools { //为int拓展了一个成员方法 //成员方法需要实例化 //实例化的成员是value public static void SpeakValue (this int value) { //拓展逻辑 Console.WriteLine("为int拓展方法"+value); } }
int本身是一个叫int32的结构体.
使用:
int t = 0; t.SpeakValue();
输出结果:

相关信息
如果拓展方法和原本的方法重名,就会先调用原本的方法.