java basic syntax -- object

Posted by carobee on Wed, 22 Sep 2021 17:47:07 +0200


The difference between process oriented and object-oriented

Process oriented: the main concerns are: the specific process of implementation, causality [development idea of integrated graphics card]
Advantages: for programs with simple business logic, rapid development can be achieved, and the early investment cost is low.

Disadvantages: it is difficult to solve very complex business logic by using process oriented development. In addition, process oriented
As a result, the "coupling degree" between software elements is very high. As long as one ring goes wrong, the whole system will be affected,
Resulting in poor "expansion force" of the final software. In addition, because there is no concept of independence, component reuse cannot be achieved.

Object oriented: the main focus is: what functions can be completed by the independent object. [development idea of independent graphics card]

Advantages: low coupling and strong expansion force. It is easier to solve the more complex business logic in the real world. Strong reusability of components.

Disadvantages: the investment cost in the early stage is high, which requires independent body extraction and a lot of system analysis and design.

C language is purely process oriented, C + + semi object-oriented and Java purely object-oriented

Most of the new programming languages are object-oriented. People understand the real world in an object-oriented way.

Object oriented is more in line with people's way of thinking.

Three characteristics of object oriented

All object-oriented programming languages have these three characteristics.

Develop a software in an object-oriented way. In the life cycle: [OO object-oriented method is used throughout the whole life cycle]
*Object oriented analysis: OOA
*Object oriented design: OOD
*Object oriented programming: OOP

Concepts of classes and objects

What is a class?
-Class does not exist in the real world. It is a template and a concept. It is the result of human brain thinking abstractly.
-A class represents a class of things.
-In the real world, object A and object B have common characteristics. They abstract and summarize A template, which is called class.

What is an object?
-An object is an actual individual. In the real world.

Describe the whole software development process:

Programmers first observe the real world and look for objects from the real world
After searching for N objects, it is found that all objects have common characteristics
The programmer forms a template [class] in his brain
Java programmers can express a class through Java code
There are class definitions in Java programs
Then you can create objects through classes
After having objects, you can make objects cooperate directly to form a system.
Class – [instantiation] - [object]

Object is also called instance / instance

Object – [Abstract] – > class

a key:
Class describes the common characteristics of objects.
Common features such as height
When accessing this height feature, you must first create an object to access this feature through the object. Because the value of this feature is different after it is on a specific object. Some objects are 1.80 in height and others are 2.80 in height.

What information does a class mainly describe?
A class mainly describes state + action.
Status information: name, height, gender, age
Action information: eating, singing, dancing, learning

Status – > Properties of a class
Actions – > methods of a class

Property; / / describes the status information of the object
Method; / / describes the action information of the object

be careful:
When the state and action are specific to an object, the final results may be different.
There are common features between objects, but there are data differences after objects.

	Syntax structure:
		[Modifier list] class Class name{
	Student class, which describes the common characteristics of all student objects:
		What status information does the student object have:
			* Student number[ int]
			* Name[ String]
			* Gender[ boolean]
			* Age[ int][Age is an attribute. Age is a data. If it is data, there should be a data type]
			* Address[ String]

		What action information does the student object have:
			* having dinner
			* sleep
			* study
			* play
			* sing
			* dance
	Important: attributes are usually defined in the form of a variable.
		int no;
		int age;
		String name;
		String address;
		boolean sex;

* java The language includes two data types:

	- Basic data type

	- Reference data type
		String.class SUN Provided
		System.class SUN Provided

		Student.class Programmer defined
		User.class Programmer defined
		Product.class Programmer defined
		Customer.class Programmer defined
	- java All in the language class All belong to the reference data type.

Object creation and use

public class Student{    //Student is a class and belongs to the reference data type. The type name is student
	     //Class body = attribute + method
	     //Attribute [stored data is in the form of variable]
	     //Since variables are defined in the class body and outside the method body, such variables are called member variables
	     //All students have student number information, but each student's student number is different, so to access this student number, you must first create an object to access the student number information through the object
	     //Student number information cannot be accessed directly through "class", so this member variable is also called instance variable
	     //Objects are also called instances, and instance variables are also called object variables. [object level variables]
	     //If the object is not created, the memory space of the no variable does not exist. The memory space of the no variable will not be created until the object is created
	     int no;
	     String name;    //full name
	     int age;            //Age
	     boolean sex;   //Gender
	     String addr;   //Address}

	//If the member variable is not assigned manually, the system assigns a default value
	//N objects can be instantiated through a class
	//Syntax of instantiated object: new class name ();
	//New is an operator in the java language. The new operator is used to create objects and open up new memory space in the JVM heap memory
	//Method area memory: when the class is loaded, the class bytecode code fragment is loaded into the memory space.
	//Stack memory (local variable): when a method code fragment is executed, it will allocate memory space to the method and press the stack in the stack memory.
	//Heap memory: new objects are stored in heap memory
     public   class   OOTest01
         public  static  void  main (String[] args) {
	//Student is a reference data type and s is a variable name
	//new Student () is a student object
	//Student s; is a local variable [stored in stack memory]
	//What is an object? The memory space opened up by the new operator in heap memory is called an object
	//What is a reference? A reference is a variable, but it holds the memory address of another java object.
	//In java language, programmers can't directly operate heap memory. There are no pointers in java, unlike c language.
	//In java language, programmers can only access instance variables inside objects in heap memory through "reference".
	Student s = new Student();  //s is both a reference and a local variable

	//Syntax format for accessing instance variables
	//Read data: reference. Variable name;
	//Modify data: reference. Variable name = value;}}
//Husband class
	public class Husband
	//full name
	String  name;
	//The husband object contains the wife reference
	Wife    w;

	//Wife class
	public class Wife
	//full name
	String  name;
	//Wife object contains husband reference
	Husband   h;	
	public  class  OOTest04
	public static void main(String[] args)
	//Create a husband object
	Husband  hm=new Husband();
	//Create a wife object
	Wife  by=new Wife();
	//[wife can be found through husband, and husband can also be found through wife]



  public  class  OOTest05
	 public  static  void  main (String[] args) {
	Customer  c = new Custermer();
	System.out.println(;  //0

	c=null ;
	//The following program can be compiled because it conforms to the syntax
	//Null pointer exception during operation
	// Null pointer exception must occur when null reference accesses data related to "instance"

Topics: Java