Detailed explanation of object-oriented in Java (easy to understand)

object-oriented

1. The concept of objects

In the programming world, objects are equivalent to substances in philosophy.

Everything is an object, everything is an object.

Objects have characteristics and behaviors.

Characteristics: Use nouns to represent what the object has, also called attributes. For example: size, color, etc.

Behavior: Use verbs to represent what the object can do, also called methods. For example: eating, walking, running, barking, etc.

2. Object-oriented

Object Oriented Programming (OOP).

Procedure-oriented programming, C language

Put the elephant in the refrigerator problem.

Using process orientation, consider the steps:

1. Open the refrigerator door

2. Stuff the elephant inside

3. Close the refrigerator door

When using object-oriented programming, what objects need to be considered? What properties and methods do they have? How are the relationships between objects structured?

Objects: refrigerator, elephant, pusher (an object with the function of putting the elephant into the refrigerator)

Properties and methods: (properties and methods related to the current business)

refrigerator:

  • Properties: size, door
  • Method: On, Off

elephant:

  • Properties: Size

推:

  • Method: (Put the elephant) in

Process-oriented thinking is generally used to solve relatively simple problems, because its process is relatively simple and suitable for simple projects.

Object-oriented thinking is generally more complex than process-oriented. It requires analysis of the properties and methods of various participating objects. It is suitable for complex project business and has good scalability and maintainability.

3. Category

Among objects with the same characteristics and behaviors, extracting common characteristics and behaviors is called a class.

4. Definition of class

Define a class:

Composed of properties and methods. Properties are variables, and properties and methods exist side by side in a class.

Note: Do not add the static keyword to the method first.

public class Dog {
    
    
	String breed; // 品种
	int age; // 年龄
	String sex; // 性别
	String furColor; // 毛色
	public void eat() {
    
    
		System.out.println("正在吃...");
	}
	public void sleep() {
    
    
		System.out.println("正在睡...");
	}
}

5. Creation and use of objects

public class TestMain {
    
    
	public static void main(String[] args) {
    
    
		// 创建对象
		Dog dog = new Dog();
		// 给属性赋值
		dog.breed = "哈士奇";
		dog.age = 1;
		dog.sex = "公";
		dog.furColor = "黑白";
		// 取值
		System.out.println("狗的品种为:" + dog.breed + ",年龄为:" + dog.age + ",性别为:" + dog.sex
				+ ",毛色为:" + dog.furColor);
		// 调用对象的方法
		dog.eat();
		dog.sleep();
	}
}

6. Relationship between objects and classes

Class is a relatively abstract concept and is the template for all objects. A class can create multiple objects.

An object is a specific instance created based on a template.

7. Instance variables and temporary variables

Ordinary variables defined in a class are properties, also known as instance variables. The variables defined in the method are called temporary variables, also known as local variables.

The scope of instance variables is available throughout the class. Instance variables can be used even without assignment, and the default value is the same as the array default value.

public class Dog {
    
    
	String breed; // 品种
	int age; // 年龄
	String sex; // 性别
	String furColor; // 毛色
	
	public void eat() {
    
    
		System.out.println("狗的品种为:" + breed + ",年龄为:" + age + ",性别为:" + sex
				+ ",毛色为:" + furColor + "正在吃...");
	}
	
	public void sleep() {
    
    
		System.out.println("狗的品种为:" + breed + ",年龄为:" + age + ",性别为:" + sex
				+ ",毛色为:" + furColor + "正在睡...");
	}
}
public class TestMain2 {
    
    
	public static void main(String[] args) {
    
    
		// 创建对象
		Dog dog1 = new Dog();
		dog1.breed = "哈士奇";
		dog1.age = 1;
		dog1.sex = "公";
		dog1.furColor = "黑白";
		
		Dog dog2 = new Dog();
		dog2.breed = "泰迪";
		dog2.age = 1;
		dog2.sex = "公";
		dog2.furColor = "棕";
		
		dog1.eat();
		dog1.sleep();
		
		dog2.eat();
		dog2.sleep();
		
		Dog dog3 = new Dog();
		dog3.eat();
		dog3.sleep();
	}
}

The use of temporary variables and instance variables in methods:

public class Dog {
    
    
	String breed; // 品种
	int age; // 年龄
	String sex; // 性别
	String furColor; // 毛色
	
	public void eat() {
    
    
		int age = 20; // 临时变量,默认使用
		System.out.println("狗的品种为:" + breed + ",年龄为:" + this.age + ",性别为:" + sex
				+ ",毛色为:" + furColor + "正在吃..."); // 可以使用this调用属性
	}
	
	public void sleep() {
    
    
		System.out.println("狗的品种为:" + breed + ",年龄为:" + age + ",性别为:" + sex
				+ ",毛色为:" + furColor + "正在睡...");
	}
}

the difference:

local variables instance variables
Define location inside method Inside the class, outside the method
default value No, you need to assign the value first and then use it. Yes, same as array
Scope of use Starts when the method is defined and ends with the curly brace Used inside a class
naming conflict They cannot be the same, and an error will be reported. It can be the same as local variables. Local variables are used first in methods. You can use this. attribute to access instance variables.

8. Instance methods

Note: Methods that are not modified with static are called instance methods. Need to use instance (object) call.

Methods consist of method declaration and method implementation.

The declaration is the name of the method, parameter list, etc.

The implementation of the method is {the content in curly brackets}.

public void sleep()  // 声明
// 实现   
{
    
    
    System.out.println("狗的品种为:" + breed + ",年龄为:" + age + ",性别为:" + sex
                       + ",毛色为:" + furColor + "正在睡...");
}

9. Overloading of methods (overload)

In a class, multiple methods with the same name but different parameter lists are defined, which is called method overloading.

Notice:

  • The different parameter lists include the type, number, and order of parameters.
  • Method overloading has nothing to do with return value type and access modifiers

Function: It will automatically find the corresponding method and call it based on the parameters passed in. Methods corresponding to the same function do not need to be defined with different names to avoid confusion. For example: the println method provided by the system can print multiple types of data. There is no need to define methods with different names. You only need to remember the printing method.

public class ClassA {
    
    
	public void a() {
    
    
		System.out.println("a()");
	}
	
	public void a(int i) {
    
    
		System.out.println("a(int i)");
	}
	
	public void a(double i) {
    
    
		System.out.println("a(double i)");
	}
	
	public void a(String s, int i) {
    
    
		System.out.println("a(String s, int i)");
	}
	
	public void a(int i, String s) {
    
    
		System.out.println("a(int i, String s)");
	}
}

10. Construction method

Method used to create objects. Called using the new keyword.

Features:

  • The method name is exactly the same as the class name
  • There is no return value type. Note: There is no return value type. It does not mean that the return value type is void.
  • Can only be called through the new keyword

Note: If a class does not define a constructor, a default parameterless constructor will be automatically generated. If the constructor is manually defined, a default no-argument constructor will not be generated.

public class ClassB {
    
    
	// 不是构造方法,因为有void返回值类型
//	public void ClassB() {
    
    
//		
//	}
	
	public ClassB() {
    
    
		System.out.println("构造方法被调用");
	}
}
public class TestClassB {
    
    
	public static void main(String[] args) {
    
    
		ClassB c = new ClassB();
	}
}

When a class has multiple attributes, and all attributes must be assigned values ​​when creating an object, multiple lines of code are required to assign values ​​to the attributes, and it is easy to forget the attributes. You can use parameterized constructors to simplify the process of creating objects and assign values ​​to all properties.

The function of the parameterized constructor is:

  • Simplify the process of attribute assignment
  • Set necessary attributes as parameters, and they must be assigned values ​​when calling to avoid forgetting.

Notice:

  • When a parameterized constructor is provided, the parameterless constructor is invalid.
  • Constructors can be overloaded
public class Cat {
    
    
	String breed; // 品种
	int age; // 年龄
	String sex; // 性别
	String furColor; // 毛色
	// 方法重载
	// 无参构造方法
	public Cat() {
    
    
		
	}
	// 有参构造方法
	public Cat(String b, int a, String s, String f) {
    
    
		breed = b;
		age = a;
		sex = s;
		furColor = f;
	}
	
	public void eat() {
    
    
		System.out.println("猫的品种为:" + breed + ",年龄为:" + this.age + ",性别为:" + sex
				+ ",毛色为:" + furColor + "正在吃..."); // 可以使用this调用属性
	}
	public void sleep() {
    
    
		System.out.println("猫的品种为:" + breed + ",年龄为:" + age + ",性别为:" + sex
				+ ",毛色为:" + furColor + "正在睡...");
	}
}
public class TestCat {
    
    
	public static void main(String[] args) {
    
    
		Cat cat1 = new Cat(); // 无参构造
		Cat cat = new Cat("蓝猫", 1, "公", "黑色"); // 有参构造
		cat.eat(); // 方法调用
	}
}

11. this keyword

Used in a class to represent the current object.

effect:

  • Call instance properties
  • Call instance method
  • Call other constructor methods

Call the instance attribute. When the name of the local variable is the same as the attribute name, use this to call the attribute.

public Bird(String breed, int age, String sex, String furColor) {
    
    
    this.breed = breed;
    this.age = age;
    this.sex = sex;
    this.furColor = furColor;
}

Calling instance methods: You can use this in one instance method to call another instance method. Of course, you can also not write this.

public void eat() {
    
    
    System.out.println("鸟的品种为:" + breed + ",年龄为:" + this.age + ",性别为:" + sex
                       + ",毛色为:" + furColor + "正在吃..."); // 可以使用this调用属性
    this.sleep();
}

public void sleep() {
    
    
    System.out.println("鸟的品种为:" + breed + ",年龄为:" + age + ",性别为:" + sex
                       + ",毛色为:" + furColor + "正在睡...");
}

Call other constructors: simplify the code of the constructor

public Bird(String breed, String furColor, int age) {
    
    
    this.breed = breed;
    this.furColor = furColor;
    this.age = age;
}

// 有参构造方法
public Bird(String breed, int age, String sex, String furColor) {
    
    
    this(breed, furColor, age); // 调用其他构造方法
    this.sex = sex;
}

Guess you like

Origin blog.csdn.net/asdf12388999/article/details/127117057