JAVA Series -> Object Oriented

1.1 Overview

Java is an object-oriented programming language, object-oriented programming is an idea, under the guidance of our object-oriented thinking, the use of the Java language to design, develop computer programs. The object here refers to the reality of all things, every thing all have their own attributes and behaviors. Object-oriented computer programming is in process, referring to the reality of things, the properties of the characteristics of things, abstracted behavioral characteristics, describe the design ideas into computer events. It is different from the process-oriented thinking, the emphasis is achieved by calling the object's behavior, rather than trying to operate step by step to achieve.

For example

Example
Cleaning:
process-oriented: clothes off -> find a bowl -> put some detergent -> add water -> soak for 10 minutes -> rub -> Clothes washing -> wrung -> dry up
the object-oriented : clothes off -> turn on automatic washing machine -> throw clothes -> button -> dry up

Difference:
process-oriented: emphasis on steps.
Object-oriented: emphasis on object, the object here is washing machine

Feature

A more object-oriented thinking is in line with our thinking habits of thought, it can be complex things simple, and we became a commander from the performer. Object-oriented languages, comprising the three basic features *** ***, i.e. encapsulation, inheritance and polymorphism.

1.2 classes and objects

Look around and you will find many objects, such as tables, chairs, students, teachers and so on. Tables and chairs belonging to office supplies, teachers and students are all human beings. So what is it like? What is the object of it

What is the class

Class : is a group of related properties and behavior of the collection. It can be seen as a kind of template of things, use things attribute characteristics and behavioral characteristics to describe this kind of things.

In reality, we describe a class of things:
Attribute : Information is the state of the things.
Behavior : What is that thing can do.

Example: kittens.
Attributes: name, weight, age, color.
Behavior: walking, running, call

What is the object

Object : a class is a concrete manifestation of things. The object is a class instance (the object is not to find a girlfriend), you must have this kind of things the properties and behavior.

In reality, an instance of a class of things: a kitten.
For example: a cat.
Properties: tom, 5kg, 2 years, yellow.
Behavior: slip foot of the wall walk, jumping tap running, meow.

The relationship between classes and objects

Class is a description of a class of things, is abstract.
Objects are instances of a class of things, it is specific.
Class is a template object, the object is a class of entities.

1.3 Definition of the class

Compare things with class

The real world of a class of things:
attribute: status of things.
Behavior: what things can do.

Java class used to describe things is true:
member variables: the corresponding attribute of things
members of the Methods: The Thing and behavior

Class Definition Format

Here Insert Picture Description
The definition of class: is the definition of the class members, including member variables and member methods.
Member variables: variables previously defined and is almost the same. But the location has changed. Outside the class, method.
Member method: and the previously defined method is almost the same. Just get rid of the static, static in action again later explain in detail in an object-oriented curriculum.
For example:

public class Student { 
//成员变量 
String name;
//姓名 
int age;
//年龄
//成员方法 
//学习的方法 
  publicvoid study() { 
      System.out.println("好好学习,天天向上"); 
  }
//吃饭的方法
   publicvoid eat() { 
       System.out.println("学习饿了要吃饭");
   }
}

1.4 objects

The object using the format

Here Insert Picture Description

The default value of the member variables

Here Insert Picture Description

Exercise 1.5 Class and Object

Customize the phone categories:

public class Phone { 
// 成员变量 
String brand; //品牌 
int price; //价格 
String color; //颜色 
// 成员方法 
//打电话 
  public void call(String name) { 
    System.out.println("给"+name+"打电话"); 
  }
//发短信 
  public void sendMessage() { 
    System.out.println("群发短信"); 
  }
}

定义测试类:
public class Test {
public static void main(String[] args) {
//创建对象
Phone p = new Phone();
//输出成员变量值
System.out.println(“品牌:”+p.brand); //null
System.out.println(“价格:”+p.price);//0
System.out.println(“颜色:”+p.color);//null
System.out.println("‐‐‐‐‐‐‐‐‐‐‐‐");
//给成员变量赋值
p.brand = “锤子”;
p.price = 2999;
p.color = “棕色”;
//再次输出成员变量值
System.out.println(“品牌:”+p.brand);//锤子
System.out.println(“价格:”+p.price);//2999
System.out.println(“颜色:”+p.color);//棕色
System.out.println("‐‐‐‐‐‐‐‐‐‐‐‐");
//调用成员方法
p.call(“紫霞”);
p.sendMessage();
}
}

1.6 对象内存图

一个对象,调用一个方法内存图
Here Insert Picture Description
通过上图,我们可以理解,在栈内存中运行的方法,遵循"先进后出,后进先出"的原则。
先在方法区中写入两个class文件 首先执行的肯定是main方法,于是入栈,其中有新建Phone one 在栈区的main方法里,new Phone就在堆内存了,然后其属性在方法区的class中可以找到 三个成员变量和两个成员方法 值得注意的是,成员变量是参考方法区的成员变量在堆中创建的,将其赋值默认值。成员方法就不一样了 保存的其实是一个地址 ,是方法区的成员方法整体的地址给了堆内存中的成员方法.最后将堆中new Phone的地址给了栈中的one。

如何访问成员变量?先在栈的main里面one.变量名 然后根据one 的 地址 去寻找堆中的变量然后修改 。
如何访问成员方法?同样先在栈的main里面one.方法名(),然后根据one的地址去寻找堆中的方法地址,然后去寻找对应的方法区中的地址,找到后,要想运行,就要在栈中创建对应方法传参进去执行,执行完毕立即删除。(遵循"先进后出,后进先出")

但是,这里依然有问题存在。创建多个对象时,如果每个对象内部都保存一份方法信息,这就非常浪费内存 了,因为所有对象的方法信息都是一样的。那么如何解决这个问题呢?请看如下图解

两个对象,调用同一方法内存图

Here Insert Picture Description
这个和上一个很像,唯一不同的就是new了两个Phone对象 其余原理相同 不同的地方就是 新new的第二个对象在堆中另辟了一块空间,有了新地址0x999 但是成员方法的地址还是0x333

两个引用指向同一个对象的内存图

Here Insert Picture Description这个和之前的又很类似 不同之处在于圈起来的部分 是将one赋值给two 也就是one的地址给了two two具有了one的所有属性和方法

一个引用,作为参数传递到方法中内存图

Here Insert Picture Description

As a method of using the object type of the return value

Here Insert Picture Description

1.7 distinguish member variables and local variables

Variable depending on the location of the definition, we give the variables played a different name. As shown below:
Here Insert Picture Description
different positions in the class
member variable: class outer methods
of local variables: the process or method declarations (formal parameters)

Scope is not the same
member variables: class
local variables: the method

Different initial values of
member variables: have default values
of local variables: no default. You must define, assign, and finally use

Different locations in memory
member variables: heap
of local variables: Stack Memory

Lifecycle different
member variables: With the creation of the object exists, the object disappear with the disappearance of
local variables: With call a method exists, along with the method call is completed disappear

Published 37 original articles · won praise 24 · views 655

Guess you like

Origin blog.csdn.net/qq_16397653/article/details/103711890