Java - 什么是实例变量? [英] Java -What is an instance variable?
问题描述
我的任务是使用一个应该由用户输入的实例变量String来创建一个程序。但我甚至不知道实例变量是什么。什么是实例变量?我该如何创建一个?它是做什么的?
My assignment is to make a program with an instance variable, a String, that should be inputed by user. But I don't even know what an instance variable is. What is an instance variable? How do I create one? What does it do?
推荐答案
实例变量是在类中声明的变量,但在方法之外:类似于:
Instance variable is the variable declared inside a class, but outside a method: something like:
class IronMan{
/** These are all instance variables **/
public String realName;
public String[] superPowers;
public int age;
/** Getters / setters here **/
}
<现在这个IronMan类可以在其他类中实例化以使用这些变量,例如:
Now this IronMan Class can be instantiated in other class to use these variables, something like:
class Avengers{
public static void main(String[] a){
IronMan ironman = new IronMan();
ironman.realName = "Tony Stark";
// or
ironman.setAge(30);
}
}
这就是我们使用实例的方式变量。无耻的插件:此免费电子书中的示例此处。
This is how we use the instance variables. Shameless plug: Example pulled from this free ebook here here.
这篇关于Java - 什么是实例变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!