[chapter 04 Java object-oriented programming] the first experience of everything being an object

Posted by webhamster on Tue, 11 Jan 2022 21:07:27 +0100

❀ Write in front
❀ Blog home page: Hard working Naruto
❀ Series column: Java basic learning πŸ˜‹
❀ Welcome, friends, praise πŸ‘ follow πŸ”Ž Collection πŸ” Learning together!
❀ If there are mistakes, please correct them! 🌹

🚩 In recent java learning, I found that many basic knowledge of Java are forgotten and fuzzy. I plan to sort out all [Java], and successive articles will be put here. Welcome to subscribe and learn together > > > Java basic learning πŸ˜‹

πŸ”₯ Series portal:
[Appendix 1 common algorithms in Java arrays] the ten sorting algorithms are illustrated and explained in detail, giving you endless aftertaste
[chapter 03 Java arrays] programmers must see the detailed explanation of arrays
[chapter 02 basic Java syntax] the detailed explanation enables you to re understand the basic Java syntax and process control
[chapter 01 overview of Java language] Java has been studied for a long time. Come back and get familiar with it (detailed)

1, Process oriented and object oriented

  1. Both of them are a kind of thought, and object-oriented is relative to process oriented.
    Process oriented, emphasizing the functional behavior, taking the function as the minimum unit, consider how to do it
    Object oriented, encapsulating functions into objects, emphasizing the objects with functions, taking class / object as the minimum unit, and considering who will do it
    Β 
  2. Object oriented emphasizes the use of human thinking methods and principles in daily thinking logic, such as abstraction, classification, inheritance, aggregation, polymorphism and so on

πŸ‘Œ Three characteristics of object oriented

1) Encapsulation
2) Inheritance
3) Polymorphism

2, Classes and objects

Class and object are the core concepts of object-oriented

🎁 Note: class is the description of a class of things, which is an abstract and conceptual definition
An object is every individual of the kind of thing that actually exists

  1. Object oriented programming focuses on class design
  2. The design of a class is actually the design of its members

πŸ‘Œ Class membership

The code is as follows:

class project{
		//Property, or member variable
		String name;
		boolean isflag;
		//constructor 
		public project(){
		}
		public project(String a,boolean is){
			name = a;
			isflag = is;
		}
		//Method, or function
		public void run(){
		System.out.println("Operation engineering project")
		}
		public String fix(){
		return "Project Name:" + name + ",isflag : " + isflag;
		}
		//Code block
		{
		name = "Hard working Naruto";
		time = 13;
		isflag = true;
		
		}
		//Inner class
		class car{
		String name;
		double number;
		}
}

🎁 Note: class = abstract concept of people
Object = a real person

πŸ‘Œ Object creation and use

  1. Syntax: class name object name = new class name ();
  2. Use "object name. Object member" to access object members (including properties and methods)
    Code examples are as follows:
public class num{
		public static void main(String args[]){
		//create object
		Animal ab=new Animal();
		ab.legs=4;//Access properties
		System.out.println(ab.legs);
		ab.eat();//Access method
		ab.run();//Access method
	}
}
  1. Access mechanism of class:
    Access mechanism in a class: Methods in a class can directly access member variables in a class
    Access mechanism in different classes: first create the object to access the class, and then use the object to access the members defined in the class

3, Object creation and used memory parsing

πŸ‘Œ heap

Heap, the only purpose of this memory area is to store object instances. Almost all object instances allocate memory here. This is described in the Java virtual machine specification: all object instances and arrays should be allocated on the heap

πŸ‘Œ Stack

Stack refers to the virtual machine stack, which is used to store local variables, etc. The local variable table stores various basic data types (boolean, byte, char, short, int, float, long, double) of known length at compilation time, and object references (reference type, which is not equivalent to the object itself and is the first address of the object in heap memory). After the method is executed, it is automatically released

πŸ‘Œ Method area

Method Area: used to store class information, constants, static variables, code compiled by the real-time compiler and other data that have been loaded by the virtual machine

πŸ‘Œ Anonymous object

Anonymous object: for example: new person() shout()

4, Attributes

  1. Syntax format: modifier data type attribute name = initialization value
  2. Modifier:
    ● common permission modifiers include: private, default, protected and public
    ● other modifiers: static, final
  3. Data type: any basic data type (such as int, Boolean) or any reference data type
  4. The attribute name belongs to the identifier, which can comply with the naming rules and specifications
public class Person{
		//Declare the private variable age
		private int age;
		//Declare public variable name
		public String name = "Hard working Naruto";
}

πŸ‘Œ Member variables and local variables

Member variables: variables declared outside the method and inside the class
Local variable: a variable declared inside a method body

  1. Similarities and differences between the two in initialization values:
    ● same: all have a life cycle
    ● different: all local variables except formal parameters need to be explicitly initialized

πŸ‘Œ Default initialization assignment of property

5, Method

  1. Definition: a method is an abstraction of the behavior characteristics of a class or object, which is used to complete a function operation
  2. The purpose of encapsulating functions into methods is to realize code reuse and simplify code
  3. Methods in Java cannot exist independently. All methods must be defined in classes
  4. Format:
    Modifier return value type method name (parameter type parameter 1, parameter type parameter 2,...)
    Method body program code
    Return return value;
    }

πŸ‘Œ Method overload

  1. Concept: more than one method with the same name is allowed in the same class, as long as their parameter number or parameter type are different
  2. Features: it has nothing to do with the return value type. It only depends on the parameter list, and the parameter list must be different. (number of parameters or parameter type). When calling, it is distinguished according to different method parameter lists
  3. Code example:
//Returns the sum of two integers
int add(int x,int y){
	return x+y;
}
//Returns the sum of three integers
int add(int x,int y,int z){
	return x+y+z;
}
//Returns the sum of two decimals
double add(double x,double y){
	return x+y;
}

πŸ‘Œ Value Passing Mechanism of method parameters

  1. Introduction: a method must be called by its class or object to be meaningful
    -If the method contains parameters:
    ● formal parameter: parameter in method declaration
    ● actual parameter: the parameter value actually passed to the formal parameter during method call
  2. Code example: parameter passing of basic data type
public static void main(String[] args) {
		int x = 5;
		System.out.println("Before modification x = " + x);// 5
		// **x is an argument**
		change(x);
		System.out.println("After modification x = " + x);// 5
	}
	public static void change(int x) {
		System.out.println("Before modification x = " + x);
		x = 3;
		System.out.println("After modification x = " + x);
	}
  1. Code example: parameter passing of reference data type
public static void main(String[] args) {
		Person per = new Person();
		per.age = 5;
		System.out.println("Before modification age = " + per.age);// 5
		// x is an argument
		change(per);
		System.out.println("After modification age = " + per.age);// 3
	}
	public static void change(Person per) {
		System.out.println("Before modification age = " + per.age);
		per.age = 3;
		System.out.println("After modification age = " + per.age);
	}

6, One of the object-oriented features: Encapsulation

  1. Encapsulation is to combine the member properties and member methods of an object into an independent same unit, and hide the internal details of the object as much as possible
    It includes the following two meanings:
    ● combine all member attributes and all member methods of the object to form an indivisible independent unit (i.e. object)
    ● information concealment refers to concealing the internal details of the object as much as possible, forming a boundary to the outside, and retaining only limited external interfaces, which refers to contact with the outside
  2. In Java, data is declared private, and then public is provided
    Methods: getXxx() and setXxx() implement the operation on this attribute,
    To achieve the following purposes:
    ● hide the implementation details in a class that do not need to be provided externally;
    ● users can only access data through pre-defined methods, and can easily add control logic to limit unreasonable operation of attributes;
    ● easy to modify and enhance the maintainability of the code

πŸ‘Œ Four access modifiers

7, Constructor / construction method

  1. Role: create objects; Initialize object
  2. features:
    ● have the same name as the class
    ● return value type is not declared
    ● cannot be modified by static, final, synchronized, abstract, native, and cannot have a return statement return value
  3. Format:
    Modifier class name (parameter list){
      initialization statement;
    }
    The code is as follows:
public class Animal {
		private int legs;
		// constructor 
	public Animal() {
		legs = 4;
	}
	public void setLegs(int i) {
		legs = i;
	}
	public int getLegs() {
		return legs;
	}
}

πŸ‘Œ Constructor Overload

Constructor overloading makes the creation of objects more flexible and convenient to create various objects
Code example:

public class Person{
	public Person(String name, int age, Date d) {
		this(name,age);
		...
	}
	public Person(String name, int age) {
		...
	}
	public Person(String name, Date d) {
		...
	}
	public Person(){
		...
	}
}

🎁 Note: if the constructor is overloaded, the parameter list must be different

🎁 Conclusion: This article first talks about Java object-oriented programming ideas and some basic knowledge, which is easy to understand and will be continuously updated!

πŸ‘Œ The author is a Java beginner. If there are errors in the article, please comment and correct them in private letters and learn together~~
😊 If the article is useful to the friends, praise it πŸ‘ follow πŸ”Ž Collection πŸ” Is my biggest motivation!
🚩 Step by step, nothing to a thousand miles, book next time, welcome to see you again 🌹

Topics: Java Back-end