是的,它们称为对象,由类定义。这基本上是你在学习Java时学到的第一件事。
//The definition of an object and it's members
public class Car {
String type, model, color;
}
然后,您可以将它们设为公开,以便在类外部访问和更改它们
public class Car {
public String type, model, color;
}
并像这样访问它们
//Create an instance of a Car, point to it with variable c and set one of it's properties/members
Car c = new Car();
c.type = "Fiesta";
但是,在Java中,允许从外部编辑类的变量被认为是不好的形式,通常你会添加方法来访问每个变量,称为访问器。
public class Car {
private String type, model, color;
//Return the type of this object
public String getType(){
return type;
}
//Set the type of this object
public void setType(String type){
this.type = type;
}
//etc
}
然后像这样访问它们
Car c = new Car();
c.setType("Fiesta");
类是您编写的用于创建对象的模板,这些对象是类的运行时实例。