对象/类

1. Objects&Class--对象&类

Object − Objects have states and behaviors. An object is an instance of a class.

    Example: A dog has states - color, name, breed as well as behaviors – wagging the tail, barking, eating. 

Class − A class can be defined as a template/blueprint that describes the behavior/state that the object of its type support.(抽象描述了某一类事物具有的属性、行为等)

2. Variables--变量

Local variables − Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed. (方法内、构造函数内、代码块中,方法调用结束后就没了)

Instance variables − Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.(成员变量、实例变量,创建对象的时候被初始化)

Class variables − Class variables are variables declared within a class, outside any method, with the static keyword. (类变量,静态static修饰)

3. Constructors--构造函数

When discussing about classes, one of the most important sub topic would be constructors. Every class has a constructor. If we do not explicitly write a constructor for a class, the Java compiler builds a default constructor for that class.(如果不显示声明构造函数,虚拟机默认提供一个空参数的构造函数)

Each time a new object is created, at least one constructor will be invoked. The main rule of constructors is that they should have the same name as the class. A class can have more than one constructor.

4. Source File Declaration Rules--源文件(.java文件)定义的一些规则

As the last part of this section, let's now look into the source file declaration rules. These rules are essential when declaring classes, import statements and package statements in a source file.

  • There can be only one public class per source file.

  • A source file can have multiple non-public classes.

  • The public class name should be the name of the source file as well which should be appended by .java at the end. For example: the class name is public class Employee{} then the source file should be as Employee.java.

  • If the class is defined inside a package, then the package statement should be the first statement in the source file.

  • If import statements are present, then they must be written between the package statement and the class declaration. If there are no package statements, then the import statement should be the first line in the source file.

  • Import and package statements will imply to all the classes present in the source file. It is not possible to declare different import and/or package statements to different classes in the source file.

  • 上面这一堆总结成下面一张图

【引文】https://www.tutorialspoint.com/java/java_object_classes.htm 

原文地址:https://www.cnblogs.com/live-for-learning/p/12253874.html