.net 程序设计:猫大叫一声,所有的老鼠都开始逃跑,主人被惊醒。

.net 程序设计:猫大叫一声,所有的老鼠都开始逃跑,主人被惊醒。,第1张

这个问题用委托和事件来处理,首先定义一只猫:

    public class Cat

    {

        public delegate void DeleMiao()

        public event DeleMiao EventMiao

        public void Miao()

        {

            Console.WriteLine("猫叫了")

            EventMiao.Invoke()

        }

    }

然后定义主人、老鼠和其它任何猫叫的接收类:

    public class Master

    {

        public void Wake()

        {

            Console.WriteLine("主人醒了")

        }

    }

    public class Rat

    {

        public void Run()

        {

            Console.WriteLine("老鼠跑了")

        }

    }

    //扩展

    public class Others

    {

        public void Anything()

        {

            Console.WriteLine("anything")

        }

    }

最后在主函数里把猫和这几个接收者关联起来:

    class Program

    {

        static void Main(string[] args)

        {

            Cat cat=new Cat()

            Master master=new Master()

            Rat rat=new Rat()

            cat.EventMiao += master.Wake

            cat.EventMiao += rat.Run

            Others others=new Others()

            cat.EventMiao += others.Anything

            cat.Miao()

            Console.ReadKey()

        }

    }

扩展性方面,以后添加任何新的猫叫接收者的时候,都不需要修改猫和所有已有接收者,只需要在主函数里绑定猫叫事件和新接收者的关系即可。

一写就写多了,不过包含的东西比较多,java的重要知识点里面也有

这个结构是简单的处理的,有兴趣的话你可以想象下写成一个小的文字游戏

public class D {

public static void main(String[] args){

Cat cat1 = new Cat("Tom" , 10)

Cat cat2 = new Cat("Sun" , 15 , "苹果树")

Dog dog1 = new Dog("lufy" , 5)

Dog dog2 = new Dog("nan" , 20 , "泳池")

cat1.start()

cat2.start()

dog1.start()

dog2.start()

}

}

abstract class Creature extends Thread

{

protected int life//生物的寿命

protected String name = ""

protected String objname = ""

public void call(){}

public void eat(){}

public void active(int state){}

}

class Cat extends Creature

{

public Cat(String name , int life)

{

this.name = name

this.life = life

System.out.println("小猫" + name + "诞生了 , 寿命" + life)

}

public Cat(String name , int life ,String objname)

{

this.name = name

this.life = life

this.objname = objname

System.out.println("小猫" + name + "诞生了 , 寿命" + life + "喜欢" + objname )

}

public void climb()

{

System.out.println("小猫" + name + "在爬树" )

}

public void climb(String objname)

{

System.out.println("小猫" + name + "在爬" + objname + "树")

}

@Override

public void call() {

System.out.println("小猫" + name + "停下来在喵喵的叫" )

}

@Override

public void eat() {

System.out.println("小猫" + name + "饿了,它在吃东西" )

}

@Override

public void active(int state) {

switch(state)

{

case 1:

if(objname.length() >0)

climb(objname)

else

climb()

break

case 2:

call()

break

case 3:

eat()

break

}

}

@Override

public void run() {

//你可以尝试在这里做一个状态机,控制动物的行为

int i = 0

int state = 0

while(i <life)

{

double rnd = Math.random()

state = (int)(rnd * 4)//随机出状态

active(state)

i++

}

System.out.println("小猫" + name + "它的生命结束了,享年" + life)

}

}

class Dog extends Creature

{

public Dog(String name , int life)

{

this.name = name

this.life = life

System.out.println("小狗" + name + "诞生了 , 寿命" + life )

}

public Dog(String name , int life , String objname)

{

this.name = name

this.life = life

this.objname = objname

System.out.println("小狗" + name + "诞生了 , 寿命" + life + "喜欢" + objname )

}

public void swim()

{

System.out.println("小狗" + name + "在游泳" )

}

public void swim(String objname)

{

System.out.println("小狗" + name + "在" + objname + "游泳")

}

@Override

public void call() {

System.out.println("小狗" + name + "停下来在喵喵的叫" )

}

@Override

public void eat() {

System.out.println("小狗" + name + "饿了,它在吃东西" )

}

@Override

public void active(int state) {

switch(state)

{

case 1:

if(objname.length() >0)

swim(objname)

else

swim()

break

case 2:

call()

break

case 3:

eat()

break

}

}

@Override

public void run() {

//你可以尝试在这里做一个状态机,控制动物的行为

int i = 0

int state = 0

while(i <life)

{

double rnd = Math.random()

state = (int)(rnd * 4)//随机出状态

active(state)

i++

}

System.out.println("小狗" + name + "它的生命结束了,享年" + life)

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12016489.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存