05 - object oriented Foundation (object oriented)

Thank you for passing by. I hope the student's notes can give you a trivial reference (1 / 100)
Java object-oriented mind map, [link to the complete Java Architecture]

1, Object oriented thought

1.1 general

    Object oriented is relative to process oriented.
    Process oriented to object-oriented is the change of programmer's thought from executor to commander.

1.2 examples

(1) How many steps does it take to put an elephant in the fridge
       Facing the process, answer: Step 3: 1 open the refrigerator door, 2 install the elephant, and 3 close the refrigerator door
       Object oriented answer: Step 2: 1 recruit a worker (object) who can operate the refrigerator, and 2 command the workers to load elephants
       Conclusion: the oriented process is rigid and difficult to adapt to change. Object oriented is more flexible and reusable.
(2) Let's describe another scene of life:
       Scene: when we live alone, we often struggle with how to eat three meals a day.
       Process oriented: the process of buying vegetables, cooking, eating and washing dishes.
       Object oriented: recruit a nanny and wait for food every day.
       Conclusion: process oriented, we need to pay attention to very cumbersome processes. Object oriented does not pay attention to specific details, but pays more attention to the overall architecture.

1.3 three thoughts

    Conceptually, object-oriented thinking can be divided into the following three kinds: OOA, OOD and OOP
       OOA: Object Oriented Analysis
       OOD: Object Oriented Design
       OOP: Object Oriented Programming

1.4 three characteristics

    Encapsulation: all content is invisible to the outside
    Inheritance: inherit other functions and continue to develop
    Polymorphism: method overloading itself is a manifestation of polymorphism

2, Classes and objects

2.1 relationship between class and object

    Class represents a common product, a comprehensive feature and abstract. Object is a product of personality and an individual feature. It is concrete (similar to the concepts of drawings and real objects in life). Class can only be used through object, and all operations of object are defined in class.
    Class consists of properties and methods:
       Attribute: it is equivalent to the characteristics of people. (state information of things)
       Method: it is equivalent to people's behavior one by one, such as talking, eating, singing and sleeping
    Example: pre class and post object
    Example: kitten.
       Attributes: name, weight, age, color.
       Behavior: walk, run and shout.
       In reality, an example of a kind of thing: a kitten.
       Example: a kitten.
       Attributes: tom, 5kg, 2 years, yellow.
       Behavior: sneaking around the wall, jumping and running, meowing.

2.2 definition format of class

    class name{
    Member properties
    Member method
   }

2.3 properties and methods

    Attribute definition format: data type attribute name;
    Format of attribute definition and assignment: data type attribute name = initialization value;
    Method definition format:
       Permission modifier return value type method name (formal parameter list){
      // Method body
       Return return value;
      }
code:

package LeiYuDuiXiang;
/**
 * class Class name{
 * 	Member properties;
 * 	Membership method;
 * }
 * 
 * Classes must be written in java files,.
 * In a. java file, there can be N classes, but only one public modified class can exist.
 * . java The file name of the file must be exactly the same as the class name modified by public;
 *
 */
public class Demo01_ChuangJian {
	/**
	 * Class creation and method supplement
	 * @param args
	 */
	public static void main(String[] args) {
		//Format for creating objects:
		//Class name object name = new class name ();
		Person p = new Person();
		//Object attribute assignment
		//Format: 	 Object name. Attribute name = value;
		p.name = "Zhang San";
		p.age = 18;
		p.sex = 'male';
		p.say();
		
		
		int s = p.sum(100, 200);
		System.out.println(s);
	}
}

/**
 * Classes are like drawings
 */
class Person{
	//Attributes - Characteristics
	String name;
	int age;
	char sex;
	//Method behavior
	/**
	 * Define format:
	 * Return value type method name (formal parameter list){
	 * 	Method body
	 * 	return Return value;
	 * }
	 * 
	 * Call format:
	 * Object name. Method name (actual parameter list);
	 */
	
	void say() {
		System.out.println("Self introduction: I am"+name+" , My age:"+age+",My gender:"+sex);
	}
	
	int sum(int x,int y) {
		int z = x+y;
		return z; 
	}
	
}

Supplement:

package LeiYuDuiXiang;

public class Demo02_ChuangJianBuChong {
	/**
	 * Object creation supplement
	 * @param args
	 */
	public static void main(String[] args) {
		Book b1 = new Book();
		Book b2 = new Book();
		b1.name = "Golden Apple";
		b1.info = "It tells the hard journey of fruit farmers planting golden apples.";
		b2.name = "Silver Apple";
		b2.info = "It tells the hard journey of fruit farmers planting silver apples.";
		
		b1.say();
		b2.say();
		
	}
}

class Book{
	String name;
	String info;
	
	void say() {
		System.out.println("title:"+name+" , Introduction:"+info);
	}
}

2.4 object memory diagram

    In the future, just pick it according to this picture.

code:

package LeiYuDuiXiang;

/**
 * Memory details for object creation
 */
public class Demo03_ChuangJianNeiCun1 {
	/**
	 * Two references point to an object memory map, which should be combined with notes
	 * @param args
	 */
	public static void main(String[] args) {
		Book b1 = new Book();
		b1.name = "Golden Apple";
		b1.info = "The process of fruit farmers' hard planting golden apples was described.";
		
		Book b2 = new Book();
		b2.name = "Silver Apple";
		b2.info = "The process of fruit farmers' hard planting silver apples was described.";
		
		b2 = b1;
		b2.name = "Copper apple";
		b1.say();
	}
}
class Book{
	String name;
	String info;
	
	void say() {
		System.out.println("title:"+name+" , Introduction:"+info);
	}
}

3, Construction method

3.1 general

    Function: used for object initialization.
       Execution timing: automatically called when an object is created
    Features: all Java classes will exist at least
       A constructor. If there is no explicit constructor in a class, the compiler will generate it automatically
       A parameterless constructor without any code! If you write any constructor, the compiler will not automatically generate parameterless constructors

3.2 definition format

    It is basically the same as ordinary methods, except that the method name must be the same as the class name, and there is no declaration of return value type!

3.3 construction method design

    It is recommended to customize the parameterless construction method and do not rely on the compiler to avoid errors.
    When there are non constant member variables in the class, it is recommended to provide two versions of construction methods, one is the nonparametric construction method, and the other is the construction method with all attributes as parameters.
    When all member variables in the class are constants or have no member variables, it is recommended not to provide any version of construction.
code:

public class Demo04_GouZaoFangFa {

	/**
	 * Construction method
	 * @param args
	 */
	public static void main(String[] args) {
		//System.out.println("1");
		Person2 p1 = new Person2("Zhang San");
		//System.out.println("2");
		//p1.name = "Zhang San";
		p1.age = 18;
		p1.say();
	}

}

class Person2{
	
	// Construction method
	Person2(String n){
		name = n;
	}

	/*
	 * // Parameterless constructor public person2() {system. Out. Println ("parameterless constructor executed!");}
	 * 
	 * // Full parameter constructor public Person2(String name, int age){
	 * System.out.println("The full parameter constructor method is executed! "); this.name = name; this.age = age;}
	 */
    
	String name;
	int age;
	
	void say() {
		System.out.println("Introduce yourself. I am:"+name+" , My age:"+age);
	}
}

4, Method overload

4.1 concept:

    If the method name is the same and the parameter type or parameter length or parameter order is different, the method overload can be completed! The method overload has nothing to do with the return value!
    Method overloading allows us to complete specific functions by passing different parameters to call methods under different requirements.
code:

package FangFaZhongZai;

public class Demo05_FangFa {
	/**
	 * Method overloading
	 * @param args
	 */
	public static void main(String[] args) {
		Math m = new Math();
		int num = m.sum(100, 500);
		System.out.println(num);
		
		double num2 = m.sum(10.5, 20.6);
		System.out.println(num2);
	}
}

// For naming rules, see the meaning of naming
class Math{
	/**
	 * Methods defined in a class are allowed to be overloaded (same method name)
	 * 
	 * 1,Same method name
	 * 2,Parameter list length or parameter list type (different order of parameter types)
	 * 
	 * Note: it is independent of the return value type
	 * 
	 */
	int sum(int x,int y) {
		int z = x+y;
		return z;
	}
	
	double sum(double x,double y) {
		double z = x+y;
		return z;
	}
	
	double sum(int x,double y) {
		double z = x+y;
		return z;
	}
	
	double sum(double y,int x) {
		double z = x+y;
		return z;
	}
	
}

4.2 rules:

    The overloaded method must change the parameter list;
    Overloaded methods can change the return type;
    The overloaded method can change the access modifier;
    Overloaded methods can declare new or broader check exceptions;
    Methods can be overloaded in the same class or in a subclass.

4.3 overloading of construction method

    For a class, there can be multiple construction methods: the overload of construction methods can be completed if the length or type of parameter list is different
    The overloading of construction methods allows us to call different methods to complete the initialization of objects under different requirements of creating objects!
code:

	package FangFaZhongZai;

public class Demo06_GouZaoFangFa {
	/**
	 * Construction method overload
	 * @param args
	 */
	public static void main(String[] args) {
		Person3 p = new Person3("Zhang San",18);
		p.say();
		
		Person3 p2 = new Person3("Li Si");
		p2.say();
	}
}

class Person3{
	
	Person3(String name2,int age2){
		name = name2;
		age = age2;
	}
	
	Person3(String name2){
		name = name2;
	}
	
	String name;
	int age;
	
	void say() {
		System.out.println("Self introduction: Name:"+name+", Age:"+age);
	}
}

5, Anonymous object

    An object without an object name is an anonymous object.
    Anonymous objects can only be used once. Because there is no object reference, they will be called garbage and wait to be recycled by GC. Objects used only once can be completed by anonymous objects, which will be often used in future development.
code:

```css
public class Demo07_NiMingDuiXiang {

	/**
	 * Anonymous object 	:	 No name
	 */
	public static void main(String[] args) {
		
		System.out.println(new Math2().sum(100, 200)); // Write this to better understand the concept of no name.
		
		 // If an object, we are ready to use it twice or more. Then be sure to create an object name for the object.
		new Person5().name = "Zhang San";//Negative example: three anonymous objects are created. Use the last created object when using.
		new Person5().age = 18;
		new Person5().say();
	}
}

class Math2{
	int sum(int x,int y) {
		return x+y;
	}
}

class Person5{
	String name;
	int age;
	
	void say() {
		System.out.println("Self introduction, name:"+name+" , Age:"+age);
	}
}

Reference: it's some time since I took notes. Please forgive me if my notes obviously learn from your content. (my memory is very poor. When I first wrote notes, the reference documents may not be complete)
Data download link (notes + code + others): Baidu online disk
Link: https://pan.baidu.com/s/1-qaQ1XCyyHRJTQpAo-uWAA
Extraction code: 1111
Thank you for reading. I wish you all the best from now on.

Keywords: Java Network Protocol

Added by rslnerd on Tue, 07 Dec 2021 20:36:07 +0200