对象/类

Posted live-for-learning

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对象/类相关的知识,希望对你有一定的参考价值。

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 

以上是关于对象/类的主要内容,如果未能解决你的问题,请参考以下文章

多态创建的是父类对象还是子类对象

JAVA中如何对父类对象强制转换子类对象的引用

JAVA中,类、对象、实体、实体类、实体对象之间存在怎样的联系??请高手做形象的解释。

python中类对象实例对象类属性实例属性类方法实例方法静态方法

java中可以将父类对象强制转换为子类对象吗?直接将父类对象强制转换为子类对象,与将上转型对象强制

父类对象与子类对象相互转化的条件是啥?如何实现它们的相互转化?