Java winter vacation learning Day6: object oriented

Posted by sandbudd on Fri, 21 Jan 2022 02:59:41 +0100

1. What is object-oriented

Programmers have transformed from process oriented executors to object-oriented commanders

1.1 ideas and steps of analyzing problems with object-oriented analysis method:

① select the real-world entity targeted by the problem according to the needs of the problem

② find the attributes and functions related to solving problems from the entity, and these attributes and functions form the class in the conceptual world

③ describe the abstract entity with computer language to form the definition of class in the computer world. That is, with the help of some program
Language constructs classes into data structures that computers can recognize and process

④ instantiate the class into an object in the computer world. Object is the ultimate tool to solve problems in the computer world

2. The difference between POP and OOP

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.

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.

3.Java language elements: classes and objects

3.1 Class: instantiate a Class into an object in the computer world. Object is the ultimate tool to solve problems in the computer world

3.2 Object: an Object is each individual of the kind of thing that actually exists, so it is also called an instance

package OOP1;
/*
 * Java Three main lines of object-oriented learning (Chapter 4-6)
 * 1.Java Classes and class members: properties, methods, constructors; Code block, inner class
 * 2.Three characteristics of object-oriented: encapsulation, inheritance, polymorphism, (abstraction)
 * 3.Other keywords: this, super, static, final, abstract, interface, package, import
 */

/*
 * Class and object are the core concepts of object-oriented.
 * Class is the description of a class of things, which is an abstract and conceptual definition
 * An object is each individual of such things that actually exist, so it is also called an instance.
 */
public class OOPTest {
	 public static void main(String[] args) {
		  
	}
}

3.3 Java instance

package OOP1;
/*
 * 1.Create a class and design the members of the class
 * 2.Create an instance of a class
 * 3.Call the properties (object. Properties) and methods (object. Methods) of the class
 */
/*
   * If multiple objects of a class are created, each object independently has a class property (non Static)
 * 
 */

//Test class
public class PersonTest {
	public static void main(String[] args) {
		//Create an instance of a class
		Person per1 = new Person();
		//Call the properties and methods of the class
		per1.name = "xu";//Attribute assignment
		System.out.println(per1.age);//Look at properties
		
		//Call the method of the class
		per1.eat();
		per1.think();
		per1.talk("Chinese");
		
		//new second object
		Person per2 = new Person();
		System.out.println(per2.name);
		//The result is null, indicating that it is valid for per1 Name = "Xu" does not affect per1
		
		
		//Direct assignment
		Person per3 = per1;
		System.out.println(per3.name);
		//The result is xu we set at the beginning
		//The reason is that this is directly assigned to the same object
	}
}

class Person{
	//Define properties
	String name;
	int age = 1;
	boolean isMale;
	
	//Definition method
	public void eat() {
		System.out.println("People can eat");
	}
	
	public void think() {
		System.out.println("People can think");
	}
	public void talk(String languages) {
		System.out.println("People can talk"+languages);
	}
}

Memory parsing

3.3.1 properties

package OOP1;
/*
 *  Use of properties in class
 * Attribute vs local variable
 * 1.Same point
 *     1.1 Definition format: data type variable name = variable value;
 *     1.2 They are declared first and then used
 *     1.3 Each variable has its corresponding scope
 * 2.difference
 *     2.1 The positions declared in the class are different
 *                        Attribute: directly defined in a pair of {} of the class
 *                        Local variables: declared within methods, method parameters, code blocks
 *     2.2 Permission modifier
 *                         Attribute: when declaring an attribute, you can specify its permission and use modifiers
 *                         Common permission modifiers: private,public,protected, default
 *                         Local variables: permission modifiers are not allowed   
 *     2.3 Default initial value
 *                         Property: has an initialization value
 *                         Integer (byte,short,int,long) 0
 *                         float (double) 0.0
 *                         Character type (char) 0
 *                         boolean false
 *                         Reference data type (class, interface, array) null
 *                         Local variable: no default initialization value
 *                         We must assign a value to a local variable before calling it
 *                         Special: the formal parameter can be assigned when calling
 *     2.4 Different locations in memory (non static)
 *                           Attributes: loading in heap
 *                           Local variables: loading into stack space
 */
public class UserTest {

}
class User{
	public String name = "xu";//attribute
	private int age = 1;//attribute
	boolean isMale = false;//attribute
	
	public void eat(String food) {//Formal parameter, local variable
		food = "watermelon";//local variable
		System.out.println("People can eat"+food);
	}
	
}

3.3.2 method

 

package OOP1;
/*
 * Declaration and use of methods
 * Method: to realize the function
 * 1.The declared permission modifier of the method returns the value type method name (formal parameter){
 *             Method body
 *   }
 *   Note: static, final and abstract will be discussed later
 * 2.Return value problem
 * 2.1 If there is a return value, be sure to declare the return value when declaring
 *           And be sure to use return to return the value of the variable
 * 2.2 On the contrary (void), you do not need to use return,
 *           But if you use it, you can only return it directly; Indicates a direct end method
 * 
 */
public class MethodTest {
	public static void main(String[] args) {
		Method m = new Method();
		//m.eat(); An error is reported because the eat permission is private;
		m.getName();
	}
}

class Method{
	
	String name = "xu";
	private void eat() {//private
		System.out.println("ate");
	}
	public void sleep(int hour) {//Formal parameter
		System.out.println("Sleep"+hour+"hour");
	}
	public String getName(){//No formal parameters
		return name;
	}
}

Topics: Java Back-end