The third day to learn Java - Classes and Objects

Today is January 26th 2020, and today it is the first month two days, calculated, fifty-six days have not read, and learning Java today's schedule was brought up, the virus had to learn so I can not go out, I'm too hard up! !

1.Java程序的分类:
(1)Application(应用程序):可独立运行的应用程序
(2)Applet(小应用程序):嵌入在网页中的Java程序
(3)Servelt(服务器端应用程序)

2.Java的特点
(1)简单:Java语法类似于C++:Java摒弃了C++中很少使用、难理解、容易引发错误的地方,比如指针、结构、联合、操作符重载、内存管理等等
(2):面向对象:Java是真正面向对象的语言,Java语言的设计集中于对象及其接口,它提供了简单的类机制以及动态的接口模型。
(3)分布式
(4)健壮性:较强的查错能力和安全的指针模型
(5)安全:Java的bug越来越少
(6)可移植性

Objects and classes

1. The relationship between class

(1) dependent
If the operation method of a class to another object class, then the class to another class depends on
(2) Polymerization
A class object contains objects of another class
(3) Inheritance
General and special relationship

2. Use the existing class

(1) to create objects
Date birthday = new Data (); // create such a class can only be used once
Data deadline = birthday; // deadline birthday and co-management section of memory, similar to the use of arrays and strings

3. Create your own class

Definitions 3.1 category

3.1.1 Definition Example

Here Insert Picture Description

3.1.2 Using Multiple Source Files

Each class can be placed in a separate source file, e.g.
Employee class member is placed in the file Employee.java
EmployeeTest member of the class in a file EmployeeTest.java

3.1.3 compile multiple source files of a program

Compiled executable class, other classes will be called automatically compile ==

When the compiler EmployeeTest.java found when using the
Employee class find, it will look for Employee.class, if not found, the compiler will automatically compile Employee.java

When compiling EmployeeTest.java, you can not source files Employee.java want, as long Employee.class

3.2 Analysis of the Employee class

3.2.1 a constructor

public Employee(String n,e double s,t int year,t int month,t int day);

Constructor:

(1) constructor is a special method used to construct objects, typically for example in the field given initial constructor
(2) have the same structure and the class name
(3) a class can have multiple constructors
(4) some configurations may have 0, 1, or more parameters
(5) does not return value structure
(6) to return void not because the void is also a type
(7) and configured to always be invoked with the new operator

3.2.2. Four methods

Four methods are marked as public

 public String getName ();
 public double getSalary ();
 public Date getHireDay ();
 public void raiseSalary(double byPercent);

Each method, the keyword this pointing to the implicit parameter, use:. This method name
Here Insert Picture Description

3.2.3. Examples of three fields

Examples of three fields are marked private

private String name;
private double salary;
private Date hireDay;

(1)final instance fields
Examples of modified with final field can not be modified, and this field must be initialized at configuration.
(2)static static fields
Each instance of the class static fields are shared

3.2.4.main method

Here Insert Picture Description

3.2.5 Permissions modifier

3.2.5.1 Field Modifiers

Here Insert Picture Description

3.2.5.2 class modifier
类的访问控制符或为public,或为默认,如果为public,则该类可以被其他类所访问,若
为默认,则只能被同包中的类所访问

3.3 Object structure

3.3.1 Default field initialization

If not given a field assignment is displayed in the constructor, then it will be automatically assigned to the default value
Here Insert Picture Description

3.3.2 initialization block

Here Insert Picture Description

3.4 Object destructor

3.4.1finalize method

1. You can add any class finalize method will clearly be called before the object in the garbage collector
2. In this method, you can release some system resources, such as: file handles, database connections, network connections, etc.
3. Do not The method of recovering any dependent resource shortage finalize
4 method can be used to force the garbage collector to work: System.gc ();
Here Insert Picture Description

3.5 The other two modifiers (static, final)

Here Insert Picture Description

3.5.1 static

3.5.1.1 static field

1.用static修饰符修饰的字段最本质的特征是它们是类的字段,而不是属于一个对象实例

2.它不保存在某个对象实例的内存区间,二是保存在内存区域的公共存储单元

3.类变量可以通过类名直接访问,也可以通过实例对象来访问,两种方法的结果是相同的

4.在一定意义上,它可以表示一个全局变量

3.5.1.2 static method

1.用static修饰符修饰的方法仅仅属于类的静态方法,又称为类方法,不用static修饰的
方法称为实例方法

2.类方法的本质是该方法是属于整个类的、而不是属于某个实例的。

3.深刻含义:非static的方法是属于某个对象的方法,在这个对象创建时,对象的方法在
内存中拥有自己的专属片段。而static方法是属于整个类的,它在内存中的代码将随着类
的定义而进行分配和重载,不被任何一个对象专有。

4.由于static方法是属于整个类的,所以它不能操纵和处理属于某个对象的成员变量,而
只能处理属于整个类的成员变量,即static方法只能处理本类中的static域或调用
static方法

5.在static方法中,不能访问实例变量,不能使用this和super

6.调用这个方法时,应该使用类名直接调用,也可以使用某一个具体的对象名
如Math.random()

3.5.2 final

3.5.2.1 final class

如果一个类被final修饰符所修饰和限定,说明这个类不能被继承,即不可能有子类

3.5.2.2 final Methods

final修饰符所修饰的方法,是不能被子类所覆盖的方法

3.5.2.3 final fields and local variables

1.它们的值一旦给定,将不能更改

2.是只读量,它们能且只能被赋值一次,而不能被赋值多次

3.如果一个字段被static final 两个修饰符所限定时,它可以表示常量
比如Interger.MAX_VALUE(表示最大整数)、Math.PI(表示圆周率)就是这种常量

4.关于赋值:在定义static final 域时,如果不给定初始值,则按默认值进行初始化
	   在定义final字段时,若不是static域,则必须只能赋值一次,不能缺省
	   在定义final局部变量时,也必须只能赋值一次,它的值可能不是常量,但它
	   的取值在变量存在期间不会改变

Here Insert Picture Description

Released eight original articles · won praise 1 · views 198

Guess you like

Origin blog.csdn.net/weixin_45406155/article/details/104086115