我开始更多地了解 Java 的概念,以及类、类的对象和实例变量的概念。虽然我理解类代表一个概念,但对象是这些概念的体现,而实例变量应该是这些概念的属性和行为——为什么专门称为实例变量呢?对象不是类的“实例”吗? 这是一个示例:考虑 Person 类
class Person {
private int age;
private String name;
public Person(String userName, int userAge) {
age = userAge;
name = userName;
}
}
public class Tester {
public static void main(String[] args) {
Person maya = new Person("Maya", 14);
Person jack = new Person("Jack", 16);
}
}
在这个类中,我们有两个对象:maya 和 jack。这两个对象是类的实例,因为尽管它们属于同一概念(人),但它们在姓名和年龄方面拥有不同的属性,这些属性通过实例变量反射(reflect)。每个人都有名字和年龄(其行为),但有不同的变体。
最佳答案
引用自 Oracle 的 Java 教程,地址 https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words); the currentSpeed of one bicycle is independent from the currentSpeed of another.
关于java - 为什么我们把存储对象属性的变量称为实例变量呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54140198/