Java 简明教程
Java - Class Attributes
Java Class Attributes
Java class attributes 是绑定在类中的 variables,也就是说,用于定义类的变量是类属性。
Java class attributes are the variables that are bound in a class i.e., the variables which are used to define a class are class attributes.
类属性在程序执行期间定义类的状态。类属性在类方法中可以默认访问。
A class attribute defines the state of the class during program execution. A class attribute is accessible within class methods by default.
例如,有一个类“Student”,它有一些数据成员(变量),比如 roll_no、age 和 name。这些数据成员被视为类属性。
For example, there is a class "Student" with some data members (variables) like roll_no, age, and name. These data members are considered class attributes.
Creating (Declaring) Java Class Attributes
要创建(声明)一个类属性,请使用 access modifier 后跟 data type 和属性名称。这与声明一个变量类似。
To create (declare) a class attribute, use the access modifier followed by the data type and attribute name. It’s similar to declaring a variable.
Accessing Java Class Attributes
要访问类属性,您需要首先创建一个 object,然后对对象名称使用点 (.) 运算符。类属性也可以直接在类方法中调用。
To access the class attribute, you need to create an object first and then use the dot (.) operator with the object name. Class attributes can be also called within the class methods directly.
Example: Accessing Java Class Attributes
试想一下这个例子,演示如何访问类属性。
Consider this example, demonstrating how to access the class attributes.
class Dog {
// Declaring and initializing the attributes
String breed = "German Shepherd";
int age = 2;
String color = "Black";
}
public class Main {
public static void main(String[] args) {
// Creating an object of the class Dog
Dog obj = new Dog();
// Accessing class attributes & printing the values
System.out.println(obj.breed);
System.out.println(obj.age);
System.out.println(obj.color);
}
}
German Shepherd
2
Black
Modifying Java Class Attributes
若要修改类属性,访问属性并使用赋值 (=) 运算符分配新值。
To modify a class attribute, access the attribute and assign a new value using the assignment (=) operator.
Syntax
使用以下语法修改类属性:
Use the below syntax to modify a class attribute:
object_name.attribute_name = new_value;
Example: Modifying Java Class Attributes
试想一下这个例子,演示如何修改类属性。
Consider this example, demonstrating how to modify the class attributes.
class Dog {
// Declaring and initializing the attributes
String breed = "German Shepherd";
int age = 2;
String color = "Black";
}
public class Main {
public static void main(String[] args) {
// Creating an object of the class Dog
Dog obj = new Dog();
// Accessing class attributes & printing the values
System.out.println("Before modifying:");
System.out.println(obj.breed);
System.out.println(obj.age);
System.out.println(obj.color);
// Modifying class attributes
obj.breed = "Golden Retriever";
obj.age = 3;
obj.color = "Golden";
// Printing
System.out.println("\nAfter modifying:");
System.out.println(obj.breed);
System.out.println(obj.age);
System.out.println(obj.color);
}
}
Before modifying:
German Shepherd
2
Black
After modifying:
Golden Retriever
3
Golden
Making Java Class Attributes Read Only
还可以在声明属性时,使用访问修饰符后使用 final 关键字将类属性设为只读。
You can also make the class attributes read-only by using the final keyword after the access modifier while declaring an attribute.
Syntax
使用以下语法将类属性设为只读:
Use the below syntax to make class attribute read-only:
access_modifier final data_type attribute_name;
Example: Making Java Class Attributes Read Only
在下面的例子中,name 属性使用 final 关键字设为只读。现在,此属性不可修改,并且如果我们尝试修改此属性,JVM 将发出抱怨。
In the below example, the name attribute is set to read-only using the final keyword. Now this attribute can not be modified and JVM will complain if we try to modify this attribute.
package com.tutorialspoint;
class Dog {
final String name = "Tommy";
}
public class Tester {
public static void main(String[] args) {
Dog dog = new Dog();
dog.name = "Tommy"; // Error while modifying name
System.out.println(dog.name);
}
}
编译并运行 Tester。这将产生以下结果:
Compile and run Tester. This will produce the following result −
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The final field Dog.name cannot be assigned
at com.tutorialspoint.Tester.main(Tester.java:10)