前言
本文是橙子出于兴趣爱好对Java官方教程的尝试翻译,几乎每日更新,感兴趣的朋友可以关注一下橙子;翻译过程中尽可能多的对一些关键词保留了英文原文,如果你想看最纯正的英文原版教材却又看不懂,可以试着来看一下橙子的翻译版啊,欢迎大家留言讨论,冲鸭!
更多相关文章点击阅读
Java官方教程目录2021最新版
Summary of Creating and Using Classes and Objects
可以在类名称之前加 modifier (修饰符)。class body 包括 fields, methods, and constructors。class使用field来包含 state information 状态信息,用method来implement behavior实现行为。Constructors (构造方法)用来 initialize (初始化)类的 instance (实例)。
我们在 declaration (声明) 的时候,通过public等访问修饰符来控制classes and members的访问。
在声明 class variable or a class method 的时候可以使用static关键字。一个没有被static修饰的成员是一个隐式的instance member。所有的类的 instances (实例) 共享 Class variable。Class variable也能够通过class name 和 instance reference (实例引用) 来访问。 Instances of a class (类实例) 必须通过 instance reference (实例引用) 来获取每一个实例变量的 (copy ) 副本。
可以使用new operator 和 constructor 通过class 创建一个 object 。new operator 返回一个被创建对象的 reference (引用) 。我们可以给 variable (变量) 的 reference (引用) 赋值,或者直接使用变量。
Instance variables and methods 可以通过 qualified name (全限定名) 被class外部的代码访问, instance variable (实例变量) 的 qualified name (全限定名) 如下所示:
objectReference.variableName
method (方法) 的 qualified name (全限定名) 如下所示:
objectReference.methodName(argumentList)
或者:
objectReference.methodName()
垃圾回收会自动的清理不用的对象,当 program (程序) 不再引用一个 object (对象) 的时候,对象就不再被使用了。可以通过将 variable (变量) 的引用设置为null,来显示的删除一个对象的引用。
Questions and Exercises: Classes
Questions and Exercises: Objects
写到这里第8章也就结束了,一起加油鸭!