Introduction to Java (9) - OO (object-oriented)

Introduction to Java (9) - OO (object-oriented)

Table of contents

Introduction to Java (9) - OO (object-oriented)

Object Oriented (OO)

The difference between process-oriented and object-oriented

Three major characteristics of facing objects

 life cycle

Classes and Objects

 development process

Class definition

Sample code


Object Oriented (OO)

The difference between process-oriented and object-oriented

           * Process-oriented:
                - Realize specific processes and causal relationships
                - Advantages: Programs with relatively simple business logic can be developed quickly, and the initial investment cost is low
                - Disadvantages: It is difficult to solve complex business logic, and the "coupling degree" between elements (between objects (dependence) is very high, has poor scalability, is difficult to be independent, and cannot achieve component compounding.
           * Object-oriented:
                - Mainly focus on what functions the object [independent body] can complete, which is more in line with human thinking
                - Advantages: low coupling, strong scalability, easy to solve complex business, strong component reusability
                - Disadvantages: initial investment cost High, it requires extraction of independent entities, a lot of system analysis and design
      - C language is purely process-oriented, C++ is semi-object-oriented, and Java is purely object-oriented


Three major characteristics of facing objects

           - Encapsulation
           - Inheritance
           - Polymorphism


 life cycle

           - Object-oriented analysis: OOA
           - Object-oriented design: OOD
           - Object-oriented programming: OOP

Classes and Objects

      - Class: template, concept, the result of brain abstraction. Represents a type of thing. Summarize common characteristics.
      - Object: an actual existing individual. Currently, the new operator is used to open up memory space in the heap memory.
      - Reference: It is a variable, not necessarily a local variable, it may also be a member variable. The reference saves the memory address and points to the object in the heap memory.

 development process

           - Observe the real world and find objects
           - Find common characteristics
           - Form a template (class)
           - Java expression class
           - Definition of class
           - Create objects
           - Object collaboration forms a system

Class definition

           - Syntax structure:
                [modifier list] class class name {                     property; //method describing the status information of the object                     ; //describing the action information of the object                 }            - When the status and action are specific to a certain object, there may be data differences  - all To access instance-related data, you need to access it through "reference.", because the object can only be found through reference - there is only a null reference, and a null pointer exception will occur when accessing instance-related data of the object.




         

Sample code

//定义一个公开的类
//Student是一个类,代表所有的学生
//访问类中的属性或方法,需要建立对象进行访问
class Student {
	
	//属性通常采用变量的方式来定义
	
	//状态【属性】
	//不创建对象,变量的内存空间是不存在的
	int no;//学号
	String name;//姓名
	boolean sex;//性别
	int age;//年龄
	String address;//地址
	
	//动作【方法】
	public static void a() {
		System.out.println("永远18岁!!!");
	}
}


public class OO {
	
public static void main(String[] args) {
		
		int i =10;//局部变量
		
		//通过一个类可以实例化N个对象
		//实例化对象语法:new 类名();
		//new运算符作用是创建对象,在JVM堆内存当中开辟新的内存空间
		
		/*
		 * 方法区内存:在类加载的时候,class字节码代码片段被加载到该内存空间中
		 * 栈内存(局部变量):方法代码片段执行的时候,会给方法分配内存空间,在栈内存中压栈
		 * 堆内存:new的对象在堆内存中储存
		 */
		
		Student s = new Student();
		//Student是一个引用类型
		//new Student() 是一个学生变量
		//Student s 是一个局部变量
		
		/*
		 * 访问实际变量的语法格式:
		      读取数据:引用.变量名
		      修改数据:引用.变量名 = 值
		 */
		int stuNo=s.no;
		String stuName=s.name;
		int stuAge=s.age;
		boolean stuSex=s.sex;
		String stuAddress=s.address;
		
		System.out.println("学号(未赋值):"+stuNo);
		System.out.println("名字(未赋值):"+s.name);
		
		System.out.println("------------------------------");
		
		stuNo=29;
		stuName="PDD";
		stuAge=18;
		stuSex=true;
		stuAddress="福建";
		
		System.out.println("学号:"+stuNo);
		System.out.println("姓名:"+stuName);
		System.out.println("年龄:"+stuAge);
		System.out.println("性别:"+stuSex);
		System.out.println("地址:"+stuAddress);
		
		Student.a();//方法调用
	}
		
	}

/*
 - 局部变量在栈内存中储存
 - 成员变量中实例变量在堆内存的java对象内存内部存储
 
 */

//分析图见笔记中 JVM内存结构.jpg

Guess you like

Origin blog.csdn.net/qq_61562251/article/details/135046239