java面向对象笔记

一,编程思想

1,什么是面向对象和面向过程

面向过程与面向对象都是我们编程中,编写程序的一种思维方式。 1)面向过程的程序设计方式,是遇到一件事时,思考“我该怎么做”,然后一步步实现的过程。

例如:公司打扫卫生(擦玻璃、扫地、拖地、倒垃圾等),按照面向过程的程序设计方式会思考“打扫卫生我该怎么做,然后一件件的完成”,最后把公司卫生打扫干净了。

2)面向对象的程序设计方式,是遇到一件事时,思考“我该让谁来做”,然后那个“谁”就是对象,他要怎么做这件事是他自己的事,反正最后一群对象合力能把事就好就行了。

例如,公司打扫卫生(擦玻璃、扫地、拖地、倒垃圾等),按照面向对象的程序设计方式会思考“我该让谁来做,如小明擦玻璃、让小丽扫地、让小郭拖地、让小强倒垃圾等”,这里的“小明、小丽、小郭、小强”就是对象,他们要打扫卫生,怎么打扫是他们自己的事,反正最后一群对象合力把公司卫生打扫干净了。

2,面向对象思维方式的好处

  1. 面向对象思维方式是一种更符合人们思考习惯的思想

  2. 面向过程思维方式中更多的体现的是执行者(自己做事情),面向对象中更多的体现是指挥者(指挥对象做事情)。

  3. 面向对象思维方式将复杂的问题简单化。

3,面向对象的特征

  1. 封装:保护内部的操作不被破坏;

  2. 继承:在原本的基础之上继续进行扩充;

  3. 多态:在一个指定的范围之内进行概念的转换。

二,类和对象

1,基本概念

类与对象时整个面向对象中最基础的组成单元。

:是抽象的概念集合,表示的是一个共性的产物,类之中定义的是属性和行为(方法); 对象:对象是一种个性的表示,表示一个独立的个体,每个对象拥有自己独立的属性,依靠属性来区分不同对象。

可以一句话来总结出类和对象的区别:类是对象的模板,对象是类的实例。类只有通过对象才可以使用,而在开发之中应该先产生类,之后再产生对象。类不能直接使用,对象是可以直接使用的。

2,类与对象的定义和使用

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n30" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class 类名{
//属性 (变量) ;
//行为 (方法) ;
}</pre>

范例:定义一个Person类

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n32" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class Person{ //类名首字母大写
String name ;
int age ;
public void tell() { // 没有static
System.out.println("姓名:" + name + ",年龄:" + age) ;
}
}</pre>

类定义完成之后,肯定无法直接使用。如果要使用,必须依靠对象,那么由于类属于引用数据类型,所以对象的产生格式(两种格式)如下:

(1)格式一:声明并实例化对象

类名称 对象名称 = new 类名称 () ;

(2)格式二:先声明对象,然后实例化对象:

类名称 对象名称 = null ; ​ 对象名称 = new 类名称 () ;

类属于引用数据类型

引用数据类型需要内存的分配和使用。所以,关键字new的主要功能就是分配内存空间

3,java内存分析图

image

3,类的实例化过程

简单类对象的实例化过程

1、在方法区加载类;

2、在栈内存申请空间,声明变量P;

3、在堆内存中开辟空间,分配对象地址;

4、在对象空间中,对对象的属性进行默认初始化,类成员变量显示初始化;

5、构造方法进栈,进行初始化;

6、初始化完成后,将堆内存中的地址赋给引用变量,构造方法出栈;

子类对象的实例化过程

1、在方法区先加载父类,再加载子类;

2、在栈中申请空间,声明变量P;

3、在堆内存中开辟空间,分配对象地址;

4、在对象空间中,对对象的属性(包括父类的属性)进行默认初始化;

5、子类构造方法进栈;

6、显示初始化父类的属性;

7、父类构造方法进栈,执行完毕出栈;

8、显示初始化子类的属性;

9、初始化完毕后,将堆内存中的地址值赋给引用变量P,子类构造方法出栈;

4,构造方法(Constructor)

构造方法作用: 是定义在java类中的一个用来初始化对象的方法,用new+构造方法,创建一个新的对象,并可以给对象中的实例进行赋值。

语法规则:

1.方法名必须与类名相同

2.无返回值类型,也不能用void修饰(有任何返回值类型的方法都不是构造方法)

3.可以指定参数,也可以不指定参数;分为有参构造方法和无参构造方法

构造方法的特点:

1.当没有指定构造方法时,系统会自动添加无参的构造方法。

2.构造方法可以重载:方法名相同,但参数不同的多个方法,调用时会自动根据不同的参数选择相应的方法。

3.构造方法是不被继承的

4.当我们手动的指定了构造方法时,无论是有参的还是无参的,系统都将不会再添加无参的构造方法。

5,方法重载(overload)

方法重载就是方法名称重复,加载参数不同。

在一个Java类中,定义多个同名的方法,如果方法名相同,方法参数不同,包括参数的类型和个数都不同,叫做方法的重载。调用重载方法时,Java 编译器通过检查调用的方法的参数类型和个数选择一个恰当的方法。方法重载常用于创建完成一组任务相似但参数的类型或参数的个数或参数的顺序不同的方法。

方法的重载,既可以发生在普通方法上,也可以发生在构造方法上。方法的重载,既可以发生在同类中,也可以发生在父子类中。

Java方法的重载:返回类型、修饰符可以相同,也可不同。要求同名的方法必须有不同的参数表,仅有返回类型不同是不足以区分两个重载的方法。

三,封装(Encapsulation)

1,概念

是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法。

封装可以被认为是一个保护屏障,防止该类的代码和数据被外部类定义的代码随机访问。

要访问该类的代码和数据,必须通过严格的接口控制。

封装最主要的功能在于我们能修改自己的实现代码,而不用修改那些调用我们代码的程序片段。

适当的封装可以让程式码更容易理解与维护,也加强了程式码的安全性。

2,封装的优点

  • . 良好的封装能够减少耦合。

  • . 类内部的结构可以自由修改。

  • . 可以对成员变量进行更精确的控制。

  • . 隐藏信息,实现细节。

3,实现java封装的步骤

  1. 修改属性的可见性来限制对属性的访问(一般限制为private),例如:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n99" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class Person {
private String name;
private int age;
}</pre>

  1. 对每个值属性提供对外的公共方法访问,也就是创建一对赋取值方法,用于对私有属性的访问,例如

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n101" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class Person{
private String name;
private int age;

public int getAge(){
return age;
}

public String getName(){
return name;
}

public void setAge(int age){
this.age = age;
}

public void setName(String name){
this.name = name;
}
}</pre>

  1. 在set方法中可以加入控制语句
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。