The Java™ Tutorials - Learning the Java Language - Summary of Creating and Using Classes and Objects

Summary of Creating and Using Classes and Objects 创建、使用类和对象的总结

A class declaration names the class and encloses the class body between braces. The class name can be preceded by modifiers. The class body contains fields, methods, and constructors for the class. A class uses fields to contain state information and uses methods to implement behavior. Constructors that initialize a new instance of a class use the name of the class and look like methods without a return type.

类声明命名类并将类主体括在大括号中。类名之前可以加上修饰符。类主体包含类的字段、方法和构造方法。类使用字段来包含状态信息,并使用方法来实现行为。初始化类的新实例的构造方法使用类的名称,看起来像没有返回类型的方法。

You control access to classes and members in the same way: by using an access modifier such as public in their declaration.

您可以同样的方式控制对类和成员的访问:在它们的声明中使用访问修饰符,比如 public

You specify a class variable or a class method by using the static keyword in the member’s declaration. A member that is not declared as static is implicitly an instance member. Class variables are shared by all instances of a class and can be accessed through the class name as well as an instance reference. Instances of a class get their own copy of each instance variable, which must be accessed through an instance reference.

通过在成员声明中使用 static 关键字指定类变量或类方法。未声明为 static 的成员是隐式的实例成员。类变量被类的所有实例共享,可以通过类名和实例引用来访问。类的实例获得每个实例变量的副本,必须通过实例引用访问该副本。

You create an object from a class by using the new operator and a constructor. The new operator returns a reference to the object that was created. You can assign the reference to a variable or use it directly.

您可以使用 new 操作符和构造方法从类创建对象。new 操作符返回对已创建对象的引用。你可以将引用赋值给一个变量,或直接使用它。

Instance variables and methods that are accessible to code outside of the class that they are declared in can be referred to by using a qualified name. The qualified name of an instance variable looks like this:

objectReference.variableName

The qualified name of a method looks like this:

objectReference.methodName(argumentList)

or:

objectReference.methodName()

The garbage collector automatically cleans up unused objects. An object is unused if the program holds no more references to it. You can explicitly drop a reference by setting the variable holding the reference to null.

垃圾回收器会自动清理未使用的对象。如果程序不再保存对该对象的引用,则该对象未被使用。您可以通过将持有引用的变量设置为 null 显式地删除引用。

原文

The Java™ Tutorials - Learning the Java Language - Summary of Creating and Using Classes and Objects

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值