Quickly understand and master Java classes and objects

Approximate content

First put a mind map of this chapter made by yourself. I think classes and objects mainly talk about the following six points.
Insert picture description here

Understand classes and objects in one sentence

If boys and girls are classes, then each person is an object of that class
Insert picture description here

Simple understanding of the characteristics of object-oriented programming

Encapsulation:
Encapsulation is the core idea of ​​object-oriented programming.
The Java encapsulation I understand is to pack data and methods in a class with the help of permission modifiers, which can be easily called when needed.

Inheritance:
Inheritance mainly uses the common attributes between specific objects.
The inheritance format of the class is as follows.
In Java, the extends keyword can be used to declare that a class is inherited from another class.

class 父类 {
    
    
}
 
class 子类 extends 父类 {
    
    
}

Inherited characteristics:

  1. The subclass has non-private properties and methods of the parent class.
  2. The subclass can have its own attributes and methods, that is, the subclass can extend the parent class.
  3. Subclasses can implement the methods of the parent class in their own way.

Polymorphism:
Polymorphism is the ability to have multiple different manifestations or forms of the same behavior.
Polymorphism is the same interface, using different instances to perform different operations.

Specific learning-class

Class is the carrier of attributes and behavior of encapsulated objects.
Member variables
are the attributes of the object.
Exist in the class, but outside the method (function).
Form: permission modifier + data type + variable name. example:

public class Book{
    
    
	private String name;//name 就是一个成员变量
	public static void main(String[] args) {
    
       //这是个方法
		// TODO Auto-generated method stub
	}
}

Member methods
Member methods are actually the behavior of objects. The method is actually similar to the function in the C language.
The syntax format for defining member methods:

Permission modifier return value type method name (parameter type parameter name) { ·// method body return return value; }


Permission modifiers are
used to control the invocation of classes, class member variables and member methods.
public: (Public authority) The
most powerful, different packages and different classes can be used.
Used to modify classes, member variables, member methods and construction methods.

protected: (protected authority)
in this class, other classes or subclasses in the same package are visible.
Used to modify member variables, member methods, and construction methods, and cannot modify classes (external classes, internal classes are not considered).

private: (private authority) the
least authority. Only visible in this category.
Used to modify member variables, construction methods, member methods, and cannot modify classes (external classes, internal classes are not considered).

default (default permission-same package permission)
when the above permission modifier is not written, it is the default permission.
Classes, member variables, member methods, and construction methods can all use the default permissions, that is, do not write any keywords.
Only classes in the same package can be called.

Local variables Variables in
member methods. Assignment operation or initialization must be performed during use.
Two local variables with the same name and type can be declared at the same time in non-nested scopes.

This keyword
is specified in the Java language to use this keyword to represent the reference of this class of objects.
The following is how to use it.
1. Call member variables or member methods
2. As the return value of the method

public class Book{
    
    
	private String name;//name 就是一个成员变量
	public  void setName(String name) {
    
       //这是个方法
		this.name = name;   //将参数值付给类中的成员变量
	}
	public Book getBook(){
    
    
		return this;  //返回Book引用
	}
}

Class construction method

The constructor is a method with the same name as the class and has no return value (no need to write void before)

Static variables, constants and methods

Variables, constants, and methods modified by the static keyword are called static variables, constants, and methods, collectively referred to as static members.
Static members belong to the class and are different from individual objects. You can use the class name and the "·" operator to call static members in this class or other classes.
1. Static members also follow the permission modifier.
2. This keyword cannot be used
in static methods. 3. Non-static methods cannot be called directly in static methods.

public class StaticTest{
    
    
	final static double PI = 3.1415;   //在类中定义静态常量
	ststic int id;    //在类中定义静态变量
	public static void method1(){
    
        //在类中定义静态方法
		//do Sonething
	}
	public void method2(){
    
    
		System.out.println(StaticTest.PI);  //调用静态常量
		System.out.println(StaticTest.id); //调用静态变量
		StaticTest.method1();  //调用静态方法
	}
}

Main method of the class

The main method is the entry point of the class and provides control over the flow of the program.

public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
	}
  1. The main method is static. If you want to call other methods directly in the main method, the method must also be static.
  2. The main method has no return value

Object

Creating objects
Java creates objects through the new operator. The syntax is as follows:

Test test = new Test();
Test test = new Test("a");

Test: Class name.
test: create an object of the Test class
new: create an object operator
"a": parameters of the construction method to
access the properties and behavior of the
object Use "object. class member" to get the properties and behavior of the object.
Reference
of objects

Point point;//Point是类,point是对象
point=new Point();//对象的引用

Object comparison
Use the equals() method of the String class for comparison. It is used to compare whether the contents of two object references are equal. How to use, for example:

public class Compare{
    
    
	public ststic void main(String[] args){
    
    
		String c1 = new String("abc");
		String c2 = new String("abc");
		System.out.println("c1与c2的比较结果为:"+(c1.equals(c2)));
	}
}

Object destruction
Java has a garbage collection mechanism, only objects created by new can be recycled.
1. If the object reference exceeds its scope, the object will be regarded as garbage.
2. Assign the object to null

Guess you like

Origin blog.csdn.net/qq_45884783/article/details/106105087