Java Final Review Crash (3)

Java Final Review Crash (3)


Object Oriented (1)


Let's discuss the idea of ​​object-oriented, the idea of ​​object-oriented has gradually replaced the idea of ​​process-oriented process, Java is an object-oriented high-level programming language, object-oriented language has the following characteristics

  • Object-oriented is a common idea, which is more in line with people's thinking habits
  • Object-oriented can simplify complex business logic and enhance code reusability
  • Object-oriented has the characteristics of abstraction, encapsulation, inheritance, polymorphism, etc.

Object-oriented programming languages ​​mainly include: C++, Java, C#, etc.
Therefore, you must be familiar with object-oriented thinking to write Java programs.

A class is also an object

Now let's get to know a new concept of object-oriented - class, what is a class, it is equivalent to the abstraction of a series of objects, just like a book, a class is equivalent to the cover of a book, most object-oriented languages ​​use class to Define a class, it tells you what the objects defined in it are like, we generally use the following to define a class

class ClassName {
    
    
	//body;
}

The code snippet involves a new concept // , which we'll talk about later. Above, you declared a class class, now, you can use new to create this object

ClassName classname = new ClassName();

In general, the naming of classes follows the camel case principle, which is defined as follows

Camel-Case, also known as camel-case, is a set of naming rules (conventions) used when writing computer programs. As its name CamelCase indicates, it refers to the use of a mix of upper and lower case letters to form variable and function names. In order to make it easier for programmers to communicate with their peers, programmers adopt a unified naming method with better readability.


object creation

In Java, everything is an object. I believe you are familiar with this sentence. Although everything is regarded as an object, what you manipulate is a reference to an object. Here's a nice metaphor: you can think of a car key and car as a set of object references and objects. When you want to drive, you first need to take out the car key and click the unlock option. When parking, you need to click lock to lock the car. The car key is equivalent to a reference, the car is the object, and the car key is used to drive the locking and unlocking of the car. And, even without the presence of the car, the car key is a separate entity, that is, you have an object reference, but you don't necessarily need an object to be associated with it, that is

Car carKey ;

This creates a reference, not an object, but if you try to use the reference s, an exception will be returned telling you that you need an object to associate with the reference. A safe practice is to assign an object to it when creating an object reference.

Car carkey = new Car();

In Java, once you create a reference, you want it to be associated with a new object, usually using the new operator for this purpose. New means, give me a new object, if you don't want to have a blind date, just create a new object yourself. I wish you happiness in the next life.


properties and methods

One of the most basic elements of a class is that it has properties and methods.
Properties, also known as fields, are an important part of a class, and properties can be objects of any type or primitive data types. For example as follows

class A{
    
    
	int a;
	Apple apple;
}

Classes should also include methods, which represent ways of doing something . Methods are actually functions, but Java is used to calling functions methods. This name also reflects the concept of object-oriented.
The basic composition of a method includes the method name, parameters, return value and method body , the following is an example of it

public int getResult(){
    
    
	// ...
	return 1;
}

Among them, getResult is the method name, () indicates the parameters received by the method, and return indicates the return value of the method. Note: The return value of the method must be consistent with the parameter type of the method. There is a special parameter type - void which means that the method has no return value. The code segment enclosed by {} is called the method body.

Construction method

In Java, there is a special method called constructor, also known as constructor, constructor, etc. In Java, every object is guaranteed to be initialized by providing this constructor. The constructor can only be called once during the creation of the object, ensuring the initialization of the object. The construction method is special, it has no parameter type and return value, its name should be consistent with the class name, and there can be multiple construction methods, the following is an example of a construction method

class Apple {
    
    
	int sum;
	String color;
	
	public Apple(){
    
    }
	public Apple(int sum){
    
    }
	public Apple(String color){
    
    }
	public Apple(int sum , String color){
    
    }
}

An Apple class is defined above. You will find that this Apple class has no parameter type and return value, and has multiple methods with the same name as Apple, and the parameter list of each Apple is different. This is actually a manifestation of polymorphism. We'll talk about that later. After defining the constructor, we can create the Apple object.

class createApple {
    
    

	public static void main(String[]args) {
    
    
		Apple apple1 - new Apple();
		Apple apple2 = new Apple(1);
		Apple apple3 = new Apple( "red");
		Apple apple4 = new Apple(2 , "color");
	}
}

As shown above, we have defined four Apple objects and called four different Apple construction methods. Among them, the construction method without any parameters is called the default construction method, which is

Apple apple1 = new Apple();

If no constructor is defined in the class, the JVM will automatically generate a constructor for you, as follows

class Apple {
    
    
	int sum;
	string color;
}
class createApple {
    
    
	public static void main(String[]args) {
    
    
	Apple apple1 = new Apple();
	}
}

The above code will not compile errors because the Apple object contains a default constructor.
The default constructor is also known as the default constructor or no-argument constructor.
One thing to note here is that even if the JVM will add a parameterless constructor for you by default, if you define any constructor manually, the JVM will no longer provide you with a default constructor, you must specify it manually, otherwise A compilation error will occur.

insert image description here
The error shown is that Apple's constructor with an int parameter must be provided, and the default no-argument constructor is not allowed.


method overloading

A very important concept in Java is method overloading, which is a different representation of a class name. We mentioned the constructor above, but the constructor is also a kind of overloading. Another one is the overloading of methods

public class Apple {
    
    

	int sum;
	string color;
	
	public Apple(){
    
    }
	public Apple(int sum){
    
    }
	public int getApple(int num){
    
    
		return 1;
	}
	
	public String getApple( String color){
    
    
		return "color" ;
	}
}

As shown above, there are two overloading methods, one is the overloading of the Apple constructor, and the other is the overloading of the getApple method.

But this involves a problem, if there are several with the same name, how does Java know which method you are calling? Just remember one thing here, each overloaded method has a unique parameter list. These include the type, order, number of parameters, etc. of the parameters. Satisfying one factor constitutes a necessary condition for overloading.

Remember the overloaded condition below

  • Method names must be the same
  • The parameter list must be different (different number, different types, different order of parameter types, etc.)
  • The return types of the methods may or may not be the same
  • Just having a different return type is not enough to be an overload of a method
  • Overloading happens at compile time because the compiler can choose which method to use based on the type of the parameter

method overriding

Overriding and overloading of methods have similar names, but are completely different things. The description of method overriding between the subclass and the superclass. And overloading refers to the same class. For example the following code

class Fruit {
    
    

	public void eat(){
    
    
		System.out.printl( 'eat fruit ');
	}
}

class Apple extends Fruit{
    
    

	@0verride
	public void eat(){
    
    
		System. out.printl( 'eat apple ' );
	}
}

The above code describes the rewritten code. You can see that the method in the subclass Apple has the same name as the method in the parent class Fruit, so we can infer the principle of rewriting

  • The overridden method must be consistent with the parent class, including the return value type, method name, and parameter list .
  • Overridden methods can be identified using the @override annotation
  • The access rights of the overridden method in the subclass cannot be lower than the access rights of the method in the superclass

Guess you like

Origin blog.csdn.net/ws15168689087/article/details/123120899