在类中声明整数并在应用程序中使用它 [英] declare integer in class and use it in application

查看:114
本文介绍了在类中声明整数并在应用程序中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在类文件中输入以下代码

i type the code bellow in my class file

public class enemy
    {
        public int enemylife;
        public string enemyname = "myenemy";
        
        public int hit()
        {
            enemylife--;
            return (enemylife);
        }        
    }



但我在应用程序代码中看不到敌人的生命和敌人的名字.



but i cannot see enemylife and enemyname in my application code.

this.Text = enemy.enemylife.tostring();


给出:


gives:

An object reference is required for the non-static field, method, or property





how can i have ''enemylife'' in my application code?

推荐答案

public class enemy
    {
        public static int enemylife;
        public string enemyname = "myenemy";

        public int hit()
        {
            enemylife--;
            return (enemylife);
        }
    }



将其设置为静态,然后可以通过以下方式访问:



make it as static then you can access as:

this.Text = enemy.enemylife.tostring();



否则,您需要创建对象并访问enemylife



otherwise you need to create object and access enemylife

enemy enemyObject = new enemy();
this.Text = enemyObject.enemylife.tostring();


因为您将"enemylife"声明为正常类级变量,它需要一个类实例才能访问它.它类似于汽车:你不能问
Because you declared "enemylife" as a normal class level variable, it needs a class instance in order to access it. It''s similar to a car: you can''t ask
"how much petrol has a car got?"


因为每辆车的油箱数量不同.您必须询问特定的汽车:


because each car had a different amount in it''s tank. You have to ask about a specific car:

"How much petrol has this car got?"


"How much petrol has that car got?"


"How much petrol has my car got?"


"How much petrol has the blue car got?"



有一种方法可以使您可以提出有关所有汽车的问题:它称为静态变量. 汽车有几个车轮?"是一个静态问题,因为所有汽车的车轮数都相同:4

由于您可能希望每个单独的敌人都拥有不同的生命,(或者当他们击中一个敌人时,它们都会死掉),因此您需要查看敌人"类的实例:



There is a way to make it so that you can ask a question about all cars: it is called a static variable. "How many wheels has a car got?" is a static question, because all cars have the same number of wheels: 4

Since you presumably want each separate enemy to have a different amount of life, (or they would all die when you hit a single one of them) you need to look at the instance of the "enemy" class:

enemy robot = new enemy();
Console.WriteLine(robot.enemylife);



顺便说一句:公开变量是一个坏主意-改用属性:



BTW: It''s a bad idea to expose varaiables - use a property instead:

public int EnemyLife { get; set; }


BTW2:类通常以大写字母开头.


为什么将" {get; set;}"放在句子的末尾?"

因为它声明了一个自动属性-这看起来像是外界的普通变量,但是可以在您的类中将其实现为一对方法.这意味着将来,您可以给敌人一个装甲等级,该装甲等级会影响对他们的伤害,而无需更改敌人等级以外的任何物品来应对.

你可以让外面的世界写作


BTW2: Classes normally start with a capital letter.


"why did you put '' {get; set;} '' at the end of your sentence ?"

Because it declares an automatic property - this looks like a normal variable to the outside world, but it can be implemented within your class as a pair of methods. Which means that in future, you can give your enemies an armour class which affects the damage done to them, without having to change anything outside the Enemy class to deal with it.

You can have the outside world writing

robot.Health -= 50;


并在内部计算出装甲对实际损坏有什么影响:


and internally have it work out what affect the armour has on the actual damage:

private health;
public Health
   {
   get { return health; }
   set
       {
       damage = health - value;
       damage = damage - armour;
       health -= damage;
       }

这里发生的是该机器人.生命值降低50,因此伤害=旧生命值(100)-新的潜在生命值(50),但是护甲吸收了40点,因此总伤害实际上是完成只有10点.

值得习惯-将所有变量设为私有,这会迫使您考虑外界需要了解的内容.然后,您可以为这些对象提供公共属性,以便您可以更改它们的工作方式,而不会影响课堂外的任何事物.从一开始就很容易做到这一点,因为这样您就可以控制正在发生的事情.如果您只允许在类外访问变量,则它们可能会以您未曾预料到的方式使用,并且您对类进行的任何更改都可能破坏类外的代码.

听起来可能需要更多工作,但将来可以节省大量工作!

What happens here is that robot.Health is reduced by 50, so damage = old health (100) - new would-be health (50), but the armour absorbs 40 points of that, so the total damage actually done is only 10 points.

It''s worth getting used to this - make all your variables private and it forces you to think about what teh outside world needs to know. You then provide public properties for those so you can change the way they work without affecting anything outside the class. It''s a lot easier to do this at the start, because then you control what is going on. f you just give access to your variables outside the class, they could be used in ways you didn''t anticipate, and any changes you make to your class could wreck code outside it.

It may sound like a bit more work, but it saves a huge amount of effort in the future!


这篇关于在类中声明整数并在应用程序中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆