What is object-oriented? (Easy to understand)

The first chapter Object-Oriented

White: Java is a fully object-oriented programming language! Ok? What is process-oriented? What is object-oriented it?

Gangster: Before we officially enter this part of the study, look at the process-oriented and object-oriented these two concepts, we are going to learn a great advantage. Do not worry, I'll come and speak to you.

 

Process for the - steps of

Process-oriented analysis is a step towards achieving the requirements needed to achieve these functions step by step by step, then you can turn call

Object-oriented - the behavior of

Object-oriented demand is whole in accordance with the characteristics of functional division, the parts are packaged into objects with common attributes, the object is not created in order to complete certain steps, but describes the behavior of a thing in the problem-solving steps in

White: quite understand the process-oriented, object-oriented is also hard to understand it (crying)

Gangster: I come to give you a good example to talk about

 

Snooker example - to help understand

Let us start with a demand:

Design a billiard game (skip the kick-off, only consider the middle of the process)

 

Process-oriented way of thinking:

1.palyer1 shot

2. To achieve the effect of screen shots

3. To determine whether the goals and effective

4.palyer2 shot

The effect achieved screen shot

6. Goal is determined whether valid and

7. Return to step 1

8. Output game results

The above step by step by step to achieve the function , this demand is complete.

 

Object-oriented way of thinking:

Through observation, we can see that, in fact, there are many in the above process common place

So we have these common portions are confined together, made a common structure

  1. Players system (including palyer1 and palyer2)

  2. Batting effects system, screen when displayed to the user in charge of the game

  3. Rule system to determine whether foul, win or lose, etc.

 

We cumbersome steps, through behavioral, functional, modular , object-oriented it is, we can even use the program, and were quick to realize 8 different ball game snooker (only need to change the rules, a player system, strike ball effect of the system are the same)

Process-oriented and object-oriented advantages and disadvantages:

Process-oriented

Advantages : it is better than the performance ** object-oriented, because the class is instantiated call when required, the overhead is too large.

Disadvantages : easy to maintain, reuse, extension **

Uses: microcontroller, embedded development, Linux / Unix and other high performance requirements of local

Object-Oriented

Advantages: easy to maintain and reuse, easy to expand, since there are object-oriented encapsulation , inheritance , polymorphism characteristics, can be designed low coupling system, make the system more flexible and easier to maintain drawbacks: performance is lower than the process-oriented

Low coupling , that is simple to understand, as independent as possible between modules and the module, the relationship between them as simple as possible, try to make it into a number of separate complete functions, which avoids indeed affect the whole body problem. In this section we will be systematically organized and summarized in the object-oriented end of the study.

Only through the textbook example is unable to understand the problems facing the process, in some small routine, process-oriented feel it will be more simple, but when faced with larger projects, we need to write a function similar to N function, function more and more, more and more amount of code, Bug road and it has begun.

1.1 Classes and Objects

Object-oriented thinking in java is how to show it? It is through classes and objects

Class is a collection of related attributes and behavior of a group. It is an abstract concept. Object is a concrete manifestation of such things. The presence of specific individuals.

Member variable properties of things

Member method behavior of things

Above we said these concepts, it should in the end how to understand it?

Class that has some common features , and similar behavior of an individual described herein.

For example, Li and Zhang have a name, age, height, weight and some other property, and both were able to chat, sports and other similar behavior .

As the two men have in common these places, so we put it out of the abstract, it is defined as a class - human, and Li, Wang is this class of individuals (objects), while the individual is the real concrete existence , light allusions to human, you only know what behavioral attributes, but you do not know some of his specific value, for example, you know that he belongs to the "human" so he should have a name, age and other attributes, but you do not know him specific What, how old the. While Li and Wang both specific objects, but can actually know Wang this year 30 years old, height 175 equivalent.

 

Examples of binding to sum up the above:

Member variable is used to describe the properties of this class, such as human beings should have the name, age and other attributes

Member method is used to describe the behavior of things, such as human beings can chat, exercise and other behavior

Class Definition using 1.1_1

We understand the basic definitions and concepts of classes and objects

Here we come to understand in the end as defined by an instance of a class

Defining classes General: A

Member variables: define the format and the common variables, except a different location in the class, to the methods .

Member method: define the format and a regular method, just go out static (the latter to explain the reasons)

B: Creating and using classes

a: Creating Object Format

Object class name name = new class name ();

Eg:Person p = new Person();

b: How to use the member variables and member methods it

Object name. Member variables

Object name. Member method ()

Eg:p.reading();

p.sleeping();

其实我们可以看出来,类的定义还是很简单的,结合了我们前面的知识,只是一些位置上的不同罢了,至于创建对象格式中每部分的意义我们在下面马上就要讲解了(我们还需要补充一些知识点)

我们先来看这么一个话题

经常听说有一个词叫做局部变量,它和类中的成员变量有什么关系吗?

1.1_2 成员变量和局部变量的区别:

(1)在类中的位置不同

成员变量:类中方法外

局部变量:方法定义中或者方法声明上

(2)在内存中的位置不同

成员变量:在堆中

局部变量:在栈中

(3)生命周期不同

成员变量:随着对象的创建而存在,随着对象的消失而消失

局部变量:随着方法的调用而存在,随着方法的调用完毕而消失

(4)初始化值不同

成员变量:有默认值(下面会详讲这一点)

局部变量:没有默认值,必须定义,赋值,然后才能使用

关于初始化问题我们在下面详细讲解,但是我现在还有一个问题,在我们学习java中内存分配的时候,有这样一>句话,“堆内存用来存放new创建的对象和数组” 换句话说对象存在于堆中,而成员变量存在于类中而且对象是类>的个体,所以成员变量也存在于堆中,那么问题就来了,按照同样的方式推导的时候,则会发现方法也和成员变>量一样存在于对象中,岂不就是说,局部变量也存在于堆中呢? 这明显与我们上面的定义有区别

一个类可以创建n个不同的对象,当我们new一个对象后,这个对象实体,已经在堆上分配了内存空间,由于类>的成员变量在不同的对象中各不相同(例如,小李和老王的姓名不同),都需要自己各自的存储空间,所以类的成员变量会随着对象存储在堆中,而由于类的方法是所有对象通用的,所以创建对象时,方法里面的局部变量并没有被创建,只有等到对象使用方法的时候才会被压入栈。

1.1_3 形式参数的问题

我们知道堆中存放着new出来的对象以及数组,两者均为引用类型

在讲数组的相关知识的时候,我们已经讲过了基本类型和数组这一种引用类型,形式参数对实际参数的影响

跳转—第三章 3.3_1参数传递问题

在我们学习对象后,我们继续来看一下这个问题

基本类型:形式参数的改变不影响实际参数(值传递)引用类型:形式参数的改变直接影响实际参数(引用传递)

 

1.1_4 匿名对象(理解)

我们先来了解一下如何创建匿名对象

(1)简单的理解就是:没有名字的对象

(2)应用场景

A:调用方法,仅仅只调用一次的时候。

B:可以作为实际参数传递

好处:匿名对象调用完就是垃圾,可以被垃圾回收器回收,并且这样写比较简化。

注意:如果对一个对象的多个成员进行调用,就必须给这个对象起名字(即上图中的 s),即使用普通创建对象的方法

我们下面来看一个实例来看一下如何具体使用匿名对象

1.1_5 封装的概述和使用

首先我们先来简单举一个例子:

例如:夏天宿舍很热,我们(用户)只需要操作遥控器即可使用空调,并不需要了解空调内部是如何运行的

现在由于知识掌握较少,所以对于封装的概念理解不是很深,不要着急,先过一遍,针对封装的意义及问题我会写一篇具体的文章,现在只需要有一个印象即可。

封装概述:是指隐藏对象的属性和实现细节,仅对外提供公共访问方式

封装好处

· 隐藏实现细节,提供公共的访问方式

· 提高了代码的复用性

· 提高安全性

封装原则:

· 将不需要对外提供的内容都隐藏起来

· 把属性隐藏,提供公共方法对其访问

思考过程:

通过对象去给成员变量赋值,可以赋值一些非法的数据

这是不合理的。所以在赋值之前应该先对数据进行判断

StudenDemo是一个测试类,测试类一般只创建对象,调用方法

所以这个判断应该定义在Student类中。需要使用逻辑语句

逻辑语句应该定义在方法中。所以在Student类中提供一个方法来对数据进行校验

但是如果偏偏不调用方法来赋值,还是直接赋值

这样我们的方法就没有起作用

所以我们必须强制要求使用我的方法,而不能直接调用成员变量

针对这种情况 Java提供了一个关键字 private

Private:私有的,可以修饰成员变量和成员方法

被private修饰的成员只能在本类中访问,所以外界想要操作类中的成员变量就必须通过调用类中的方法来实现

1.1_6 访问修饰符

客户端程序员:即在其应用中使用数据类型的类消费者,他的目标是收集各种用来实现快速应用开发的类。

类创建者:即创建新数据类型的程序员,目标是构建类。  

访问控制存在的原因:

a、让客户端程序员无法触及他们不应该触及的部分 ;

b、允许库设计者可以改变类内部的工作方式而不用担心会影响到客户端程序员

java的四个关键字:public、protected、default、private

(他们决定了紧跟其后被定义的东西可以被谁使用)

适用范围<访问权限范围越小,安全性越高>

访问权限 子类 其他包  
public 对任何人都是可用的
protect   继承的类可以访问以及和private一样的权限
default     继承的类可以访问以及和private一样的权限
private       除类型创建者和类型的内部方法之外的任何人都不能访问的元素

1.1_7 private的应用标准案例

可用this关键字进行完善(一般都是使用完善后的)

this的内容在下面马上介绍

1.1_8 this关键字的概述和应用

这里的调用只能通过对象名,这里它应该代表的是student的一个对象

this:代表所在类的对象引用

记住: 方法被哪个对象调用,this就代表哪个对象

适用:局部变量隐藏成员变量(稍后补充)

1.2 构造方法

构造方法和它所在类的名字相同,但构造方法没有返回值。

通常会使用构造方法给一个类的实例变量赋初值,或者执行其它必要的步骤来创建一个完整的对象

怎么理解呢?

当一个对象被创建时候,构造方法用来初始化该对象。

那么什么叫做初始化呢?

我们要知道吗,构造函数又被叫做构造器,它就是为了初始化类,当调用该构造器,会用值去初始化成员,当使用带参构造时,会将参数中的值传递给成员,而使用无参构造时,即会用一些默认的值来进行成员的初始化

例如:

private String name;

private int age;

public bool flag;

上面的三个成员变量被无参构造进行默认初始化的时候,会被初始化

name = null; age = 0; flag = false

注意:

A:如果我们没有给出构造方法,系统将自动提供一个无参构造方法

B:如果我们给出了构造方法,系统将不再提供默认的无参构造方法

如果这个时候我们还想使用无参构造方法,就必须自己给出,建议永远 给出无参构造方法(所以我们习惯于在类中同时给出无参和带参构造方法)

给成员变量赋值的两种方法:

A:setXxx()

B:构造方法

讲到这里我们不得不提一下,get、set方法,在今后写代码的时候,我们需要频繁的用到,其实这里就体现了封装不让用户直接操作成员,可以起到安全的作用,具体内容可以看前封装部分的知识

构造方法是为了创建对象时,传入一些必要的参数用来初始化对象。

setter/getter是为了控制属性可不可以读写

两者不矛盾

之前在讲解类的时候,我们由于缺少一些知识的铺垫,所以我们将类的初始化过程讲一下:

类的初始化过程

Student s = new Student();在内存中做了哪些事情?

·加载Student.class文件进内存

·在栈内存为s开辟空间

·在堆内存为学生对象开辟空间

·对学生对象的成员变量进行默认初始化

·对学生对象的成员变量进行显示初始化

·通过构造方法对学生对象的成员变量赋值

·学生对象初始化完毕,把对象地址赋值给s变量

1.3 static关键字

Static关键字注意事项

A:在静态方法中是没有this关键字的

静态是随着类的加载而加载,this是随着对象的创建而 存在的 → 静态比对象先存在

B:静态方法只能访问静态的成员变量和静态的成员方法

静态方法:

A:成员变量:只能访问静态变量

B:成员方法:只能访问静态成员方法

非静态方法:

A:成员变量:可以是静态的,也可以是非静态的

B:成员方法:可以是静态的成员方法,也可以是非静态的成员方法

为什么静态方法不能访问非静态方法呢?

因为静态方法是随着类的加载而加载的,静态是优于对象存在的,你要访问非静态的东西,可是这时候可能它还不存在。

总结起来一句话:静态只能访问静态

1.4 静态变量和成员变量

趁热打铁,我们来对静态变量和成员变量做一些区分

所属不同

·静态变量属于类,所以也称为类变量

·成员变量属于对象。所以也称为实例变量(对象变量)

·内存中位置不同

·静态变量存储于方法区的静态区

·成员变量存储于堆内存

·内存出现时间不一样

·静态变量随着类的加载而加载,随着类的消失而消失

·成员变量随着对象的创建而存在,随着对象的消失而消失

·调用不同·

·静态变量可以通过类名调用,也可以通过对象调用

·成员变量只能通过对象名调用

方式一 是用对象调用成员方法

方式二 是用类调用成员方法(推荐方式二【需要将对应成员方法写为静态的】)

如果不想让用户创建对象调用成员方法:

只需要把构造方法私有,外界就不能创建对象了

在同一个文件夹下,类定义两个文件夹中和一个文件夹中是一样的

1.4 制作文档注释和说明书

(一)

制作文档注释,文档说明书工具解析文档注释javadoc工具D:格式javadoc -d 目录 -author -version ArrayTool.java目录:就可以写一个文件夹的路径制作帮助文档出错:找不到可以文档化的公共或受保护的类:告诉我们类的权限不够(解决办法: 在class前面加public)

(二)

API(Application Programming Interface)

应用程序编程接口(帮助文档)

Jdk可以帮助我们查阅一些类、方法的详细用法以及参数说明,学会查阅文档也是一项很重要的本领(网络上自行下载)

下面试着自己通过查阅文档使用一个类

Math类

A:是针对数学进行操作的类

B:没有构造方法,因为它的成员都是静态的

C:产生随机数

public static double random(): [0.0,1.0)

D:如何产生一个1-100之间的随机数

int number = (int)(Math.random()*100)+1;

E:猜数字小游戏

1.5 代码块

我们下面来讲解这一部分的最后一个知识点

(1)用{}括起来的代码。

(2)分类:

A:静态代码块

概念: 在java类中(方法中不能存在静态代码块)

使用static关 键字和{} 声明的代码块:

执行: 静态代码块在类被加载的时候就运行了,而且只运行一次,并且优 先于各种代码块以及构造函数。

作用: 一般情况下,如果有些代码需要在项目启动的时候就执行, 这时候 就需要静态代码块。比如一个项目启动需要加载的 很多配置文件等 资源,我们就可以都放入静态代码块中。

对类的数据进行初始化,仅仅只执行一次。

B:构造代码块

概念:在java类中使用{}声明的代码块(和静态代码块的 区 别是少了static关键字):

执行: 构造代码块在创建对象时被调用,每次创建对象都会调用一 次,但是优先于构造函数执行。

作用: 和构造函数的作用类似,都能对对象进行初始化,并且只要 创建一个对象,构造代码块都会执行一次。但是反过来,构 造函数则不一定每个对象建立时都执行(多个构造函数情况 下,建立对象时传入的参数不同则初始化使用对应的构造函 数)。

把多个构造方法中相同的代码可以放到这里,每个构造方法 执行前, 首先执行构造代码块。

C:局部代码块

用于限定变量的生命周期,及早释放,提高内存利用率。

静态代码块,构造代码块,构造方法的顺序问题

·静态代码块 > 构造代码块 > 构造方法

 

结尾:

如果内容中有什么不足,或者错误的地方,欢迎大家给我留言提出意见, 蟹蟹大家 !^_^

如果能帮到你的话,那就来关注我吧!

在这里的我们素不相识,却都在为了自己的梦而努力 ❤

一个坚持推送原创Java技术的公众号:理想二旬不止

Guess you like

Origin www.cnblogs.com/ideal-20/p/10929962.html